liveBook · Manning

ValidationContext

As explained in ASP.NET Core in Action, 3e

ValidationContext is a .NET object that provides contextual information about the object and member being validated.

ValidationContext supplies contextual information to a custom ValidationAttribute while a property is being validated. Its ObjectInstance property exposes the top-level model, allowing rules to compare related properties. section 32.3

Use ValidationContext to:

  • Access the complete model when validating a nested or individual property. section 32.3
  • Compare properties, such as rejecting identical source and destination currencies. section 32.3

Currency validation

A CurrencyCodeAttribute can inspect validationContext.ObjectInstance as CurrencyConverterModel, compare CurrencyFrom and CurrencyTo, and return a ValidationResult when the combination is invalid. section 32.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains pipeline timing, ObjectInstance access, comparisons, and service resolution without detailing framework internals.
Examples
Uses currency-code validation, cross-property currency comparison, and an injected currency provider.
What it assumes
Assumes familiarity with model properties and ValidationAttribute while demonstrating contextual validation through a custom attribute.
What you can do after
Build custom validation attributes that inspect the complete model or resolve services during validation.
How it connects
Connects model binding, ValidationAttribute, ModelState, and dependency injection.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.