Validator.ValidateObjectAsync Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
| Name | Description |
|---|---|
| ValidateObjectAsync(Object, ValidationContext, CancellationToken) |
Asynchronously throws a ValidationException if the given |
| ValidateObjectAsync(Object, ValidationContext, Boolean, CancellationToken) |
Asynchronously throws a ValidationException if the given object instance is not valid. |
ValidateObjectAsync(Object, ValidationContext, CancellationToken)
- Source:
- Validator.cs
Asynchronously throws a ValidationException if the given instance is not valid.
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Constructing a ValidationContext without a display name is not trim-safe because it uses reflection to discover the type of the instance being validated in order to resolve the DisplayNameAttribute when a display name is not provided.")]
public static System.Threading.Tasks.Task ValidateObjectAsync(object instance, System.ComponentModel.DataAnnotations.ValidationContext validationContext, System.Threading.CancellationToken cancellationToken = default);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Constructing a ValidationContext without a display name is not trim-safe because it uses reflection to discover the type of the instance being validated in order to resolve the DisplayNameAttribute when a display name is not provided.")>]
static member ValidateObjectAsync : obj * System.ComponentModel.DataAnnotations.ValidationContext * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Shared Function ValidateObjectAsync (instance As Object, validationContext As ValidationContext, Optional cancellationToken As CancellationToken = Nothing) As Task
Parameters
- instance
- Object
The object instance to test. It cannot be null.
- validationContext
- ValidationContext
Describes the object being validated and provides services and context for the validators.
- cancellationToken
- CancellationToken
A CancellationToken to observe while waiting for the task to complete.
Returns
A Task representing the asynchronous validation operation.
- Attributes
Exceptions
When instance is null.
When validationContext is null.
When instance doesn't match the ObjectInstance on validationContext.
When instance is found to be invalid.
The validation operation was canceled.
Remarks
This method checks the RequiredAttribute attributes on the object's properties. If property validation succeeds, the method evaluates the ValidationAttribute attributes attached to the object's type.
This overload does not evaluate other property-level validation attributes. To evaluate those attributes, call the overload that accepts validateAllProperties with that argument set to true.
Validation runs in three stages: property validation, type-level attribute validation, and object-level validation. Each stage runs only if the preceding stages succeed. Object-level validation prefers invoking ValidateAsync(ValidationContext, CancellationToken) when implemented, with Validate(ValidationContext) as a fallback for objects that implement only the synchronous interface. It does not invoke both.
Applies to
ValidateObjectAsync(Object, ValidationContext, Boolean, CancellationToken)
- Source:
- Validator.cs
Asynchronously throws a ValidationException if the given object instance is not valid.
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Constructing a ValidationContext without a display name is not trim-safe because it uses reflection to discover the type of the instance being validated in order to resolve the DisplayNameAttribute when a display name is not provided.")]
public static System.Threading.Tasks.Task ValidateObjectAsync(object instance, System.ComponentModel.DataAnnotations.ValidationContext validationContext, bool validateAllProperties, System.Threading.CancellationToken cancellationToken = default);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Constructing a ValidationContext without a display name is not trim-safe because it uses reflection to discover the type of the instance being validated in order to resolve the DisplayNameAttribute when a display name is not provided.")>]
static member ValidateObjectAsync : obj * System.ComponentModel.DataAnnotations.ValidationContext * bool * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Shared Function ValidateObjectAsync (instance As Object, validationContext As ValidationContext, validateAllProperties As Boolean, Optional cancellationToken As CancellationToken = Nothing) As Task
Parameters
- instance
- Object
The object instance to test. It cannot be null.
- validationContext
- ValidationContext
Describes the object being validated and provides services and context for the validators.
- validateAllProperties
- Boolean
If true, also validates all the instance's properties.
- cancellationToken
- CancellationToken
A CancellationToken to observe while waiting for the task to complete.
Returns
A Task representing the asynchronous validation operation.
- Attributes
Exceptions
When instance is null.
When validationContext is null.
When instance doesn't match the ObjectInstance on validationContext.
When instance is found to be invalid.
The validation operation was canceled.
Remarks
This method checks the RequiredAttribute attributes on the object's properties even when validateAllProperties is false. If property validation succeeds, the method evaluates the ValidationAttribute attributes attached to the object's type.
If validateAllProperties is true, the method also evaluates other validation attributes on the object's immediate properties. This process does not recursively validate property values.
Validation runs in three stages: property validation, type-level attribute validation, and object-level validation. Each stage runs only if the preceding stages succeed. Object-level validation prefers invoking ValidateAsync(ValidationContext, CancellationToken) when implemented, with Validate(ValidationContext) as a fallback for objects that implement only the synchronous interface. It does not invoke both.