A custom validation attribute adds an application-specific rule when the built-in DataAnnotations attributes are not enough. Derive from ValidationAttribute, override IsValid, and apply the attribute to binding-model properties. section 32.3 section 32.5
Build a property-level validation rule in three steps:
- Define a ValidationAttribute subclass and accept rule configuration, such as an array of allowed currency codes. section 32.3
Restrict currency codes
A currency converter can annotate CurrencyFrom and CurrencyTo with CurrencyCode("GBP", "USD", "CAD", "EUR"). An input such as "£$%" makes ModelState invalid and produces the message "Not a valid currency code".
When validation depends on application data, obtain a service from ValidationContext inside IsValid. Constructor dependency injection is unavailable for custom validation attributes, so the service-locator approach uses GetRequiredService or GetService instead. section 32.5


