Validation Attributes
On this page
Nalix provides AOT-safe validation attributes in Nalix.Abstractions.Validation as replacements for System.ComponentModel.DataAnnotations. These attributes are read by the ConfigurationGenerator source generator at compile time — no runtime reflection or DataAnnotations dependency is required.
Source Mapping
src/Nalix.Abstractions/Validation/ValueRangeAttribute.cssrc/Nalix.Abstractions/Validation/DurationRangeAttribute.cssrc/Nalix.Abstractions/Validation/LengthAttribute.cssrc/Nalix.Abstractions/Validation/RequiredAttribute.cssrc/Nalix.Abstractions/Validation/AllowedEnumAttribute.cssrc/Nalix.Abstractions/Exceptions/ValidationException.cs
Attributes
ValueRangeAttribute
Specifies the numeric inclusive range that a property value must fall within.
-
Namespace:
Nalix.Abstractions.Validation -
Target: Properties
-
Constructors:
ValueRangeAttribute(double minimum, double maximum) ValueRangeAttribute(long minimum, long maximum) -
Properties:
Minimum,Maximum,MinimumInt64,MaximumInt64,UseInt64 -
Use the
(long, long)overload when the range bound includesint.MaxValueorlong.MaxValue.
DurationRangeAttribute
Specifies the inclusive TimeSpan range that a property value must fall within.
-
Namespace:
Nalix.Abstractions.Validation -
Target: Properties
-
Constructor:
DurationRangeAttribute(string minimum, string maximum) -
Properties:
Minimum,Maximum(parseable time strings, e.g."00:00:01","1.00:00:00")
RequiredAttribute
Specifies that a property value must not be null.
- Namespace:
Nalix.Abstractions.Validation - Target: Properties
For string properties that must also be non-empty, prefer LengthAttribute with a minimum of 1.
LengthAttribute
Specifies the minimum length for a string or collection property.
-
Namespace:
Nalix.Abstractions.Validation -
Target: Properties
-
Constructor:
LengthAttribute(int minimum) -
Properties:
Minimum -
Applies to
string, arrays, and collection types.
AllowedEnumAttribute
Specifies that a property value must be a defined member of its enum type.
- Namespace:
Nalix.Abstractions.Validation - Target: Properties
ValidationException
Thrown when a configuration or data validation check fails. This is the AOT-safe replacement for System.ComponentModel.DataAnnotations.ValidationException.
-
Namespace:
Nalix.Abstractions.Exceptions -
Constructors:
ValidationException() ValidationException(string message) ValidationException(string message, Exception innerException) ValidationException(string propertyName, object? invalidValue, string reason)
Usage in Options
Nalix option types use these attributes and call ValidateDataAnnotations() in their Validate() method:
using Nalix.Abstractions.Validation;
using Nalix.Environment.Configuration.Binding;
public sealed partial class MyOptions : ConfigurationLoader, IValidatableConfiguration
{
[ValueRange(1, 10_000)]
public int MaxItems { get; set; } = 100;
[DurationRange("00:00:01", "1.00:00:00")]
public TimeSpan Timeout { get; set; } = TimeSpan.FromMinutes(5);
public void Validate() => this.ValidateDataAnnotations();
}