liveBook · Manning

Validation attribute

A validation attribute is metadata attached to a class or member that applies a rule for determining whether data satisfies specified requirements.

How 3 Manning books explain it

Building Web APIs with ASP.NET Core

A custom validation attribute is an application-defined attribute that implements a domain-specific input-validation rule.

How it is used
Guards client-supplied sort strings before a board-game API uses them in dynamic ordering.
How deep it goes
Works through an attribute subclass, its validation result, parameter placement, and OpenAPI filters.
Examples
Uses sort-direction and sort-column validators on a GET endpoint, then moves them into a request DTO.
What you can do after
Build it from scratch by subclassing ValidationAttribute, applying the rule to an input, and checking its error response.

Read how Building Web APIs with ASP.NET Core explains validation attribute →

ASP.NET Core in Action, 3e

A custom validation attribute is an application-defined attribute that implements a specialized validation rule, typically by deriving from ValidationAttribute.

How it is used
Extends property-level DataAnnotations validation with domain-specific rules in Razor Pages and MVC binding models.
How deep it goes
Explains derivation, IsValid, error results, reuse, and service lookup, while deferring complex cross-property business rules.
Examples
Uses a currency converter that accepts only GBP, USD, EUR, or CAD and rejects an invalid value such as £$%.
What you can do after
Build it from scratch by defining, applying, and testing a reusable ValidationAttribute for application-specific property rules.

Read how ASP.NET Core in Action, 3e explains validation attribute →

Pro ASP.NET Core 7, Tenth Edition

A custom validation attribute is an application-defined .NET attribute derived from ValidationAttribute that supplies validation logic for model data.

How it is used
Centralizes application-specific model rules so bound data receives consistent validation across controllers and Razor Pages.
How deep it goes
Works through property- and model-level IsValid implementations, service access, attribute placement, and a Razor Pages error-display workaround.
Examples
Uses a database-backed primary-key check and a class-level name-and-price rule on a product form.
What you can do after
Build it from scratch to replace duplicated controller and Razor Page checks with reusable property- or model-level validation.

Read how Pro ASP.NET Core 7, Tenth Edition explains validation attribute →

Related concepts

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.