Files
DP44/enriched-qwen3-coder-next/Common/DTS.Common/Validators.md

54 lines
5.2 KiB
Markdown
Raw Normal View History

2026-04-17 14:55:32 -04:00
---
source_files:
- Common/DTS.Common/Validators/SensitivityValidator.cs
- Common/DTS.Common/Validators/UARTBAUDRateValidator.cs
- Common/DTS.Common/Validators/CANArbBaseBitrateValidator.cs
generated_at: "2026-04-16T02:55:46.278352+00:00"
model: "Qwen/Qwen3-Coder-Next-FP8"
schema_version: 1
sha256: "9e68239f51c0c198"
---
# Validators
## SensitivityValidator, UARTBAUDRateValidator, CANArbBaseBitrateValidator
### 1. Purpose
This module provides WPF `ValidationRule` implementations for validating user input in UI-bound numeric fields. Specifically, `SensitivityValidator` ensures sensitivity values are non-zero doubles, `UARTBAUDRateValidator` validates UART baud rates against embedded sensor constraints, and `CANArbBaseBitrateValidator` validates CAN arbitration/base bitrates using the widest applicable min/max range (to support both FD and non-FD CAN configurations). These validators are intended for use in XAML data binding scenarios to enforce input correctness before data propagation to the underlying model.
### 2. Public Interface
All classes inherit from `System.Windows.Controls.ValidationRule` and override the `Validate` method.
- **`SensitivityValidator.Validate(object value, CultureInfo cultureInfo)`**
Validates that `value` is a non-empty string parseable as a `double`, and that the parsed value is non-zero. Returns `ValidationResult.IsValid = true` only if both conditions hold; otherwise returns `false` with an appropriate localized error message (e.g., `"Invalid format"` or `"Sensitivity can not be zero"`).
- **`UARTBAUDRateValidator.Validate(object value, CultureInfo cultureInfo)`**
Validates that `value` is a string parseable as a `long`, and that the parsed value lies within `[EmbeddedSensors.BAUD_RATE_MIN, EmbeddedSensors.BAUD_RATE_MAX]` (inclusive). Returns `ValidationResult.IsValid = true` only if both conditions hold; otherwise returns `false` with a message indicating the invalid value and the allowed range (e.g., `"Maximum value is 921600"`).
- **`CANArbBaseBitrateValidator.Validate(object value, CultureInfo cultureInfo)`**
Validates that `value` is a string parseable as a `long`, and that the parsed value lies within `[EmbeddedSensors.NON_CANFD_ARB_BASE_BITRATE_MIN, EmbeddedSensors.CANFD_ARB_BASE_BITRATE_MAX]` (inclusive). Returns `ValidationResult.IsValid = true` only if both conditions hold; otherwise returns `false` with a message indicating the invalid value and the allowed range (e.g., `"Minimum value is 10000"`).
### 3. Invariants
- Input must be a `string`; all non-string inputs (including `null`) are rejected with `"Invalid format"`.
- All numeric parsing uses `long.TryParse`/`double.TryParse` with default `CultureInfo` (invariant culture), meaning only standard decimal notation (e.g., `"123"`, `"-45.6"`) is accepted—no locale-specific separators (e.g., commas) are supported.
- `SensitivityValidator` explicitly rejects zero (`0D`) as invalid, even though `double.TryParse("0", out var d)` succeeds.
- `UARTBAUDRateValidator` and `CANArbBaseBitrateValidator` enforce inclusive bounds: values equal to `*_MIN` or `*_MAX` are valid.
- Error messages for range violations are constructed using `StringResources.MinValueIs`/`StringResources.MaxValueIs` concatenated with the respective constant value.
### 4. Dependencies
- **Direct dependencies**:
- `System.Windows.Controls.ValidationRule` (base class)
- `System.Globalization.CultureInfo` (used in `Validate` signature)
- `DTS.Common.SharedResource.Strings.StringResources` (for localized error strings)
- `DTS.Common.Constant.EmbeddedSensors` (for `BAUD_RATE_MIN`, `BAUD_RATE_MAX`, `NON_CANFD_ARB_BASE_BITRATE_MIN`, `CANFD_ARB_BASE_BITRATE_MAX`)
- **Inferred consumers**:
- XAML UI elements (e.g., `TextBox`) using these validators in `ValidationRules` collections.
- Likely used in conjunction with `DTS.Common.SharedResource.Strings` resource files (e.g., `StringResources.InvalidFormat`, `StringResources.SensitivityCanNotBeZero`, etc.).
- **No internal usage**: These classes are not referenced internally in the provided source; they are standalone validators.
### 5. Gotchas
- **Culture sensitivity**: Parsing uses `CultureInfo.InvariantCulture` implicitly (via `TryParse` without specifying `IFormatProvider`), so inputs like `"1,234"` or `"1.234,56"` will fail even in cultures where they are valid. Users must enter plain decimal digits (e.g., `"1234.56"`).
- **Range logic in `CANArbBaseBitrateValidator`**: The comment indicates the validator uses the *widest* possible range (min from non-FD, max from FD) to support both CAN types. This means valid values for one CAN type (e.g., FD-only min) may be rejected if outside the combined range.
- **Zero rejection in `SensitivityValidator`**: A value of `"0"` or `"0.0"` is rejected despite being a valid `double`. This is likely intentional (sensitivity must be non-zero), but could be surprising if zero is a meaningful default elsewhere.
- **No support for scientific notation**: Inputs like `"1e5"` will fail `double.TryParse`/`long.TryParse` and return `"Invalid format"`.
- **Acronym naming**: `UARTBAUDRateValidator` and `CANArbBaseBitrateValidator` violate PascalCase conventions (per `S101` suppression), but this is intentional for acronym clarity.