Skip to content

Supported Attributes Reference

Sannr provides a robust set of validation attributes designed for Native AOT scenarios. While many mirror the standard System.ComponentModel.DataAnnotations, Sannr includes additional "Power Attributes" for conditional logic and data sanitization.

All attributes are located in the Sannr namespace.

Table of Contents


Standard Validation

[Required]

Specifies that a data field is required.

Behavior:

  • Checks if the value is null.
  • If the value is a string, it also checks if it is empty or consists only of whitespace.
ParameterTypeDescription
ErrorMessagestringCustom error message.
Groupstringvalidation group (context) this rule applies to.
SeveritySeverityError (Default), Warning, or Info.

Example:

csharp
[Required(ErrorMessage = "Username is mandatory.")]
public string Username { get; set; }

[StringLength]

Specifies the minimum and maximum length of characters that are allowed in a data field.

ParameterTypeDescription
MaximumLengthint(Constructor) The maximum allowed length.
MinimumLengthintThe minimum required length (Optional).

Example:

csharp
[StringLength(50, MinimumLength = 3, ErrorMessage = "Must be between 3 and 50 chars.")]
public string DisplayName { get; set; }

[Range]

Specifies the numeric range constraints for the value of a data field.

ParameterTypeDescription
Minimumdouble | int(Constructor) The minimum allowed value.
Maximumdouble | int(Constructor) The maximum allowed value.

Example:

csharp
[Range(18, 120, ErrorMessage = "Age must be valid.")]
public int Age { get; set; }

[Range(0.01, 1000.00)]
public double Price { get; set; }

Format & Patterns

[EmailAddress]

Validates that the property matches a valid email address format.

Performance Note: Sannr uses a pre-compiled, source-generated Regex for maximum throughput.

Example:

csharp
[EmailAddress]
public string ContactEmail { get; set; }

[CreditCard]

Validates that the property contains a valid credit card number structure (13-19 digits, allowing dashes/spaces).

Example:

csharp
[CreditCard]
public string PaymentCard { get; set; }

[Url]

Validates that the property is a well-formed URL (starting with http:// or https://).

Example:

csharp
[Url]
public string PortfolioUrl { get; set; }

[Phone]

Validates that the property contains valid phone characters (digits, spaces, dashes, parentheses, +).

Example:

csharp
[Phone]
public string PhoneNumber { get; set; }

[FileExtensions]

Validates that a string (representing a file name) ends with one of the specified extensions.

ParameterTypeDescription
ExtensionsstringComma-separated list of extensions (default: "png,jpg,jpeg,gif").

Example:

csharp
[FileExtensions(Extensions = "pdf,docx,txt")]
public string ResumeFileName { get; set; }

[FutureDate]

Validates that the property (DateTime or DateTimeOffset) is in the future.

Example:

csharp
[FutureDate]
public DateTime DeliveryDate { get; set; }

Advanced Logic

[RequiredIf]

Marks a property as required only if another property has a specific value.

ParameterTypeDescription
OtherPropertystring(Constructor) Name of the property to check against.
TargetValueobject(Constructor) The value that triggers the requirement.

Example:

csharp
public string Country { get; set; }

// State is strictly required only when Country is "USA"
[RequiredIf(nameof(Country), "USA")]
public string State { get; set; }

[AllowedValues]

Restricts a property to a predefined set of values.

ParameterTypeDescription
Valuesstring[](Constructor) Array of allowed string values.

Example:

csharp
[AllowedValues("active", "inactive", "pending")]
public string Status { get; set; }

[ConditionalRange]

Applies numeric range validation only when another property matches a specific value.

ParameterTypeDescription
OtherPropertystring(Constructor) Name of the property to check against.
TargetValueobject(Constructor) The value that triggers the range check.
Minimumdouble(Constructor) The minimum allowed value.
Maximumdouble(Constructor) The maximum allowed value.

Example:

csharp
public string Currency { get; set; }

[ConditionalRange("Currency", "USD", 1, 1000)]
public decimal Amount { get; set; }

[CustomValidator]

Delegates validation to a custom static method. This is the primary way to implement complex business logic or database checks.

ParameterTypeDescription
ValidatorTypeType(Constructor) The class containing the static method.
MethodNamestringThe name of the method (Default: "Check").
IsAsyncboolSet to true if the method returns Task<ValidationResult>.

Method Signature Requirements:

  • Sync: public static ValidationResult Check(T value, IServiceProvider sp)
  • Async: public static Task<ValidationResult> Check(T value, IServiceProvider sp)

Example:

csharp
[CustomValidator(typeof(UserRules), nameof(UserRules.IsUniqueAsync), IsAsync = true)]
public string Username { get; set; }

public static class UserRules
{
    public static async Task<ValidationResult> IsUniqueAsync(string username, IServiceProvider sp)
    {
        // ... database check logic ...
        return ValidationResult.Success();
    }
}

Data Sanitization

[Sanitize]

Automatically modifies the input data before validation rules are executed. This is useful for normalizing data (e.g., standardizing casing or removing accidental whitespace).

ParameterTypeDescription
TrimboolIf true, executes .Trim() on the string.
ToUpperboolIf true, converts string to Uppercase.
ToLowerboolIf true, converts string to Lowercase.

Example:

csharp
// Input: "  john.doe  "
// Result: "JOHN.DOE"
[Sanitize(Trim = true, ToUpper = true)]
public string UserId { get; set; }

Metadata

[Display]

Provides a user-friendly name for the property, which is used in formatted error messages.

ParameterTypeDescription
NamestringThe friendly display name.

Example:

csharp
[Display(Name = "User's Age")]
[Range(18, 99)]
public int Age { get; set; }
// Error: "The field User's Age must be between 18 and 99."

Released under the MIT License.