5.30Enabling Parameter Validation
When invoking HTTP declarative interface methods, the framework supports validating the legality of the passed parameter data.
HTTP declarative requests enable parameter validation through attributes derived from ValidationAttribute. The corresponding HTTP declarative extractor is implemented as the ValidationDeclarativeExtractor type, which is responsible for parsing attributes derived from ValidationAttribute and validating the legality of the passed parameter data.
- Validating individual values and object data
public interface IHttpService : IHttpDeclarative{ // No parameter validation is required [Get("https://furion.net/")] Task<string> GetStringAsync(string str, object obj); // Validate parameter legality; supports validating validation attributes inside the object model [Get("https://furion.net/")] Task<string> GetStringAsync([Length(10, 20)] string str, [Required] ValidationModel obj); // Supports adding multiple validation rules to a parameter [Get("https://furion.net/")] Task<string> GetStringAsync( [Required] [MinLength(2)] [MaxLength(5)] string str, [Range(0, 10)] int age); // Frozen parameter types will be ignored [Get("https://furion.net/")] Task<string> GetStringAsync([Required] CancellationToken cancellationToken);}// Object property validationpublic class ValidationModel{ public int Id { get; set; } [Required] [MinLength(3)] public string? Name { get; set; }}- Validating object data that implements the
IValidatableObjectinterface
public interface IHttpService : IHttpDeclarative{ [Get("https://furion.net/")] Task<string> GetStringAsync(ValidationObject obj);}// Implement IValidatableObject for complex validationpublic class ValidationObject : IValidatableObject{ public int Id { get; set; } [Required] [MinLength(3)] public string? Name { get; set; } public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { if (Id < 0) { yield return new ValidationResult("Id must be greater than or equal to 0.", [nameof(Id)]); } }}- Validating custom
ValidationAttributeattributes
public interface IHttpService : IHttpDeclarative{ [Get("https://furion.net/")] Task<string> GetStringAsync([StringEqual("Furion")] string str);}// Custom validation attributepublic class StringEqualAttribute : ValidationAttribute{ public StringEqualAttribute(string value) => Value = value; public string Value { get; } /// <inheritdoc /> protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) { if (value?.ToString() != Value) { return new ValidationResult($"Value is not equal to {Value}."); } return ValidationResult.Success; }}Parameters marked with attributes derived from ValidationAttribute and parameters implementing the IValidatableObject interface are validated through the underlying Validator.ValidateValue and Validator.ValidateObject methods, and support any non-frozen parameter type.
- Disabling parameter validation
The framework provides the SuppressValidationAttribute attribute, which can be used to disable parameter validation for HTTP declarative requests. When applied to an interface, it disables parameter validation for all methods under that interface; when applied to a specific method, it disables parameter validation only for that method.
public interface IHttpService : IHttpDeclarative{ [SuppressValidation] // Disable parameter validation only for the GetStringAsync method [Get("https://furion.net/")] Task<string> GetStringAsync([Length(10, 20)] string str, [Required] ValidationModel obj);}[SuppressValidation] // Disable parameter validation for all methods defined on the interfacepublic interface IHttpService : IHttpDeclarative{ [Get("https://furion.net/")] Task<string> GetStringAsync([Length(10, 20)] string str, [Required] ValidationModel obj); [Get("https://furion.net/")] [SuppressValidation(false)] // Enable parameter validation for the GetStringAsync method Task<string> GetStringAsync([Length(10, 20)] string str, [Required] ValidationModel obj);}