Validator.TryValidatePropertyAsync 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.
Asynchronously tests whether the given property value is valid.
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("The Type of validationContext.ObjectType cannot be statically discovered.")]
public static System.Threading.Tasks.Task<bool> TryValidatePropertyAsync(object? value, System.ComponentModel.DataAnnotations.ValidationContext validationContext, System.Collections.Generic.ICollection<System.ComponentModel.DataAnnotations.ValidationResult>? validationResults, System.Threading.CancellationToken cancellationToken = default);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("The Type of validationContext.ObjectType cannot be statically discovered.")>]
static member TryValidatePropertyAsync : obj * System.ComponentModel.DataAnnotations.ValidationContext * System.Collections.Generic.ICollection<System.ComponentModel.DataAnnotations.ValidationResult> * System.Threading.CancellationToken -> System.Threading.Tasks.Task<bool>
Public Shared Function TryValidatePropertyAsync (value As Object, validationContext As ValidationContext, validationResults As ICollection(Of ValidationResult), Optional cancellationToken As CancellationToken = Nothing) As Task(Of Boolean)
Parameters
- value
- Object
The value to test.
- validationContext
- ValidationContext
Describes the property member 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 value is valid, false if any validation errors are encountered.
- Attributes
Exceptions
validationContext is null, or
MemberName is null or empty.
MemberName does not identify a valid public, non-indexer property,
or value cannot be assigned to that property.
The validation operation was canceled.
Remarks
This method tests each ValidationAttribute associated with the property identified by validationContext. If validationResults is non-null, this method adds a ValidationResult to it for each validation failure.
If there is a RequiredAttribute found on the property, it is evaluated before all other validation attributes. If the required validator fails, validation stops, adding that single failure to the validationResults collection when applicable and returning false.
Any AsyncValidationAttribute instances are evaluated asynchronously after all synchronous validation attributes have passed.