data annotations
Attributes from System.ComponentModel.DataAnnotations, such as [Required] and [Range(1, 200)], that declare simple validation rules on properties.
In more detail
The same attributes validate request models in controllers and options classes at startup. Rules that involve two properties need code instead.
Example
[Range(1, 200)] public int MaxPageSize { get; set; } = 100;Related terms
- attribute — A tag in square brackets, such as [Required] or [HttpGet], that attaches extra information to code for the compiler or framework to read.
- model validation — Checking that incoming data meets the rules, such as required fields and ranges, before the application acts on it.
- ValidateDataAnnotations — The options-builder call that checks a bound options object against the data annotation attributes on its properties.
- FluentValidation — A .NET library for writing validation rules as classes with a fluent API, such as RuleFor(x => x.Price).GreaterThan(0).
Taught in
- The Options Pattern: Strongly Typed Configuration — Modern .NET Core, C# & Enterprise Microservices
- Model Validation and FluentValidation — Modern .NET Core, C# & Enterprise Microservices