Table of Contents

Class DefaultValidatorExtensions

Namespace
Arc4u.Validation
Assembly
Arc4u.Validation.dll

FluentValidation extensions for Arc4u: rules on DateTime properties, and the bridge between a FluentValidation validation and a FluentResults FluentResults.Result<TValue>.

public static class DefaultValidatorExtensions
Inheritance
DefaultValidatorExtensions
Inherited Members

Examples

public class OrderValidator : AbstractValidator<Order>
{
    public OrderValidator()
    {
        RuleFor(o => o.CreatedOn).IsUtcDateTime();
    }
}

Result<Order> result = await new OrderValidator().ValidateWithResultAsync(order);

Methods

IsDateOnly<T, TProperty>(IRuleBuilder<T, TProperty>)

Requires the property to be a DateTime with no time part (TimeOfDay is zero).

public static IRuleBuilderOptions<T, TProperty> IsDateOnly<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder) where T : class where TProperty : struct

Parameters

ruleBuilder IRuleBuilder<T, TProperty>

The rule builder.

Returns

IRuleBuilderOptions<T, TProperty>

The rule builder options, to chain further configuration.

Type Parameters

T

The type of the validated object.

TProperty

The type of the property.

Remarks

The TProperty is constrained to structs, so a nullable DateTime property cannot use this rule; the rule fails when the value is not a DateTime. The Kind is not checked.

IsUtcDateTime<T, TProperty>(IRuleBuilder<T, TProperty>)

Requires the property to be a DateTime whose Kind is Utc.

public static IRuleBuilderOptions<T, TProperty> IsUtcDateTime<T, TProperty>(this IRuleBuilder<T, TProperty> ruleBuilder) where T : class where TProperty : struct

Parameters

ruleBuilder IRuleBuilder<T, TProperty>

The rule builder.

Returns

IRuleBuilderOptions<T, TProperty>

The rule builder options, to chain further configuration.

Type Parameters

T

The type of the validated object.

TProperty

The type of the property.

Remarks

The TProperty is constrained to structs, so a nullable DateTime property cannot use this rule; the rule fails when the value is not a DateTime.

ToMetadata(ValidationFailure)

Builds a dictionary describing a FluentValidation failure with the keys Code, State, PropertyName and Severity.

public static Dictionary<string, object> ToMetadata(this ValidationFailure failure)

Parameters

failure ValidationFailure

The failure to describe.

Returns

Dictionary<string, object>

The dictionary, filled from the error code, custom state, property name and severity of the failure.

ToResultErrors(IEnumerable<ValidationFailure>)

Converts FluentValidation failures to Arc4u ValidationErrors.

public static List<ValidationError> ToResultErrors(this IEnumerable<ValidationFailure> failures)

Parameters

failures IEnumerable<ValidationFailure>

The failures to convert.

Returns

List<ValidationError>

One ValidationError per failure, converted by ToValidationError(ValidationFailure).

ToValidationError(ValidationFailure)

Converts a FluentValidation failure to an Arc4u ValidationError.

public static ValidationError ToValidationError(this ValidationFailure failure)

Parameters

failure ValidationFailure

The failure to convert.

Returns

ValidationError

The validation error.

Remarks

The message, the error code and the severity are carried over; FluentValidation Info severities map to Info.

ValidateWithResultAsync<T>(IValidator<T>, T)

Validates a value asynchronously and returns the outcome as a FluentResults.Result<TValue>.

public static ValueTask<Result<T>> ValidateWithResultAsync<T>(this IValidator<T> validator, T value)

Parameters

validator IValidator<T>

The validator to run.

value T

The value to validate.

Returns

ValueTask<Result<T>>

A successful result carrying value when it is valid; otherwise a failed result holding one ValidationError per failure.

Type Parameters

T

The type of the validated value.

ValidateWithResultAsync<T>(IValidator<T>, T, CancellationToken)

Validates a value asynchronously and returns the outcome as a FluentResults.Result<TValue>.

public static ValueTask<Result<T>> ValidateWithResultAsync<T>(this IValidator<T> validator, T value, CancellationToken cancellation)

Parameters

validator IValidator<T>

The validator to run.

value T

The value to validate.

cancellation CancellationToken

A token to cancel the validation.

Returns

ValueTask<Result<T>>

A successful result carrying value when it is valid; otherwise a failed result holding one ValidationError per failure.

Type Parameters

T

The type of the validated value.

ValidateWithResult<T>(IValidator<T>, T)

Validates a value synchronously and returns the outcome as a FluentResults.Result<TValue>.

public static Result<T> ValidateWithResult<T>(this IValidator<T> validator, T value)

Parameters

validator IValidator<T>

The validator to run.

value T

The value to validate.

Returns

Result<T>

A successful result carrying value when it is valid; otherwise a failed result holding one ValidationError per failure.

Type Parameters

T

The type of the validated value.