Class RangeValidator<T>
- Namespace
- SteamNetworkLib.Sync
- Assembly
- SteamNetworkLib.dll
Validator for numeric ranges with inclusive or exclusive bounds.
public class RangeValidator<T> : ISyncValidator<T> where T : IComparable<T>
Type Parameters
TThe numeric type to validate. Must implement IComparable<T>.
- Inheritance
-
RangeValidator<T>
- Implements
- Inherited Members
Remarks
Supported Types: Any type implementing IComparable<T> including int, float, double, decimal, long, etc.
Bounds: By default, the range is inclusive (min and max are valid values).
Use inclusive: false for exclusive bounds.
Use Cases:
- Health/energy values (0-100)
- Position coordinates within map bounds
- Percentage values (0.0-1.0)
- Count limits (0-maxItems)
// Health must be between 0 and 100 (inclusive)
var healthValidator = new RangeValidator<int>(0, 100);
// Percentage must be 0.0 to 1.0 (inclusive)
var percentValidator = new RangeValidator<float>(0f, 1f);
// Exclusive range - value must be strictly between min and max
var exclusiveValidator = new RangeValidator<int>(0, 100, inclusive: false);
Constructors
RangeValidator(T, T, bool)
Creates a new range validator.
public RangeValidator(T min, T max, bool inclusive = true)
Parameters
minTMinimum allowed value.
maxTMaximum allowed value.
inclusiveboolIf true (default), min and max are valid values. If false, values must be strictly between min and max.
Remarks
When inclusive is true, the valid range is [min, max].
When inclusive is false, the valid range is (min, max).
Exceptions
- ArgumentException
Thrown when min is greater than max.
Methods
GetErrorMessage(T)
Gets a human-readable error message describing why validation failed.
public string? GetErrorMessage(T value)
Parameters
valueTThe invalid value that failed validation.
Returns
- string
An error message describing why the value is invalid, or null if no specific message is available.
Remarks
This message is used in SyncValidationException and logged when validation fails.
Include the invalid value and expected constraints in the message for easier debugging.
IsValid(T)
Validates a value before it is synchronized.
public bool IsValid(T value)
Parameters
valueTThe value to validate.
Returns
- bool
True if the value is valid and should be synchronized; false otherwise.
Remarks
This method is called synchronously before each sync operation.
Keep validation logic fast and non-blocking as it runs on the main thread.