Language

Validator.TryValidateObjectAsync Method

Definition

Overloads

Name Description
TryValidateObjectAsync(Object, ValidationContext, ICollection<ValidationResult>, CancellationToken)

Asynchronously tests whether the given object instance is valid.

TryValidateObjectAsync(Object, ValidationContext, ICollection<ValidationResult>, Boolean, CancellationToken)

Asynchronously tests whether the given object instance is valid.

TryValidateObjectAsync(Object, ValidationContext, ICollection<ValidationResult>, CancellationToken)

Source:
Validator.cs

Asynchronously tests whether the given object instance is 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<bool> TryValidateObjectAsync(object instance, System.ComponentModel.DataAnnotations.ValidationContext validationContext, System.Collections.Generic.ICollection<System.ComponentModel.DataAnnotations.ValidationResult>? validationResults, 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 TryValidateObjectAsync : obj * System.ComponentModel.DataAnnotations.ValidationContext * System.Collections.Generic.ICollection<System.ComponentModel.DataAnnotations.ValidationResult> * System.Threading.CancellationToken -> System.Threading.Tasks.Task<bool>
Public Shared Function TryValidateObjectAsync (instance As Object, validationContext As ValidationContext, validationResults As ICollection(Of ValidationResult), Optional cancellationToken As CancellationToken = Nothing) As Task(Of Boolean)

Parameters

instance
Object

The object instance to test. It cannot be null.

validationContext
ValidationContext

Describes the object to validate and provides services and context for the validators.

validationResults
ICollection<ValidationResult>

An optional collection that receives validation failures as ValidationResult instances.

cancellationToken
CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

A Task<TResult> that is true if the object is valid, false if any validation errors are encountered.

Attributes

Exceptions

When instance is null.

When validationContext is null.

When instance doesn't match the ObjectInstance on validationContext.

The validation operation was canceled.

Remarks

This method evaluates all ValidationAttributes attached to the object instance's type. It also checks to ensure all properties marked with RequiredAttribute are set. It does not validate the ValidationAttributes on individual properties of the object unless TryValidateObjectAsync(Object, ValidationContext, ICollection<ValidationResult>, Boolean, CancellationToken) is called with validateAllProperties 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

TryValidateObjectAsync(Object, ValidationContext, ICollection<ValidationResult>, Boolean, CancellationToken)

Source:
Validator.cs

Asynchronously tests whether the given object instance is 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<bool> TryValidateObjectAsync(object instance, System.ComponentModel.DataAnnotations.ValidationContext validationContext, System.Collections.Generic.ICollection<System.ComponentModel.DataAnnotations.ValidationResult>? validationResults, 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 TryValidateObjectAsync : obj * System.ComponentModel.DataAnnotations.ValidationContext * System.Collections.Generic.ICollection<System.ComponentModel.DataAnnotations.ValidationResult> * bool * System.Threading.CancellationToken -> System.Threading.Tasks.Task<bool>
Public Shared Function TryValidateObjectAsync (instance As Object, validationContext As ValidationContext, validationResults As ICollection(Of ValidationResult), validateAllProperties As Boolean, Optional cancellationToken As CancellationToken = Nothing) As Task(Of Boolean)

Parameters

instance
Object

The object instance to test. It cannot be null.

validationContext
ValidationContext

Describes the object to validate and provides services and context for the validators.

validationResults
ICollection<ValidationResult>

An optional collection that receives validation failures as ValidationResult instances.

validateAllProperties
Boolean

If true, also evaluates all properties of the object (this process is not recursive over properties of the properties).

cancellationToken
CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

A Task<TResult> that is true if the object is valid, false if any validation errors are encountered.

Attributes

Exceptions

When instance is null.

When validationContext is null.

When instance doesn't match the ObjectInstance on validationContext.

The validation operation was canceled.

Remarks

This method evaluates all ValidationAttributes attached to the object instance's type. It also checks to ensure all properties marked with RequiredAttribute are set. If validateAllProperties is true, this method also evaluates the ValidationAttributes for all the immediate properties of this object. This process is not recursive.

When validateAllProperties is true, validation is started for each property in sequence. Synchronous attributes for a property run before that property's asynchronous attributes are awaited. Asynchronous validation that remains in progress can overlap with asynchronous validation for other properties.

When validationResults is null, validation stops after the first property error (cross-property short-circuit). Any in-flight async validators on other properties are cancelled cooperatively. When validationResults is non-null, all properties complete and all errors are collected.

Returns Task<TResult> for interoperability with standard async composition patterns such as Task.WhenAll and Task.WhenAny.

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