Skip to content

FluentValidatorWrapper TOptions,TValidator

NexusLabs.Needlr.FluentValidation

FluentValidatorWrapper<TOptions,TValidator> Class

Wraps a FluentValidation FluentValidation.IValidator<> to implement Needlr's NexusLabs.Needlr.Generators.IOptionsValidator<> interface.

public class FluentValidatorWrapper<TOptions,TValidator> : NexusLabs.Needlr.Generators.IOptionsValidator<TOptions>
    where TOptions : class
    where TValidator : FluentValidation.IValidator<TOptions>

Type parameters

TOptions

The options type being validated.

TValidator

The FluentValidation validator type.

Inheritance System.Object 🡒 FluentValidatorWrapper\<TOptions,TValidator>

Implements NexusLabs.Needlr.Generators.IOptionsValidator<TOptions>

Remarks

This wrapper allows FluentValidation validators to be used with the [Options(Validator = typeof(...))] attribute by implementing the NexusLabs.Needlr.Generators.IOptionsValidator<> interface.

Example usage:

// The FluentValidation validator
public class DatabaseOptionsValidator : AbstractValidator<DatabaseOptions>
{
    public DatabaseOptionsValidator()
    {
        RuleFor(x => x.ConnectionString).NotEmpty();
    }
}

// Wrap it for use with Needlr
public class DatabaseOptionsNeedlrValidator 
    : FluentValidatorWrapper<DatabaseOptions, DatabaseOptionsValidator>
{
    public DatabaseOptionsNeedlrValidator() : base(new DatabaseOptionsValidator()) { }
}

// Use with [Options]
[Options(ValidateOnStart = true, Validator = typeof(DatabaseOptionsNeedlrValidator))]
public class DatabaseOptions { ... }

Constructors

FluentValidatorWrapper(TValidator) Constructor

Initializes a new instance wrapping the specified FluentValidation validator.

public FluentValidatorWrapper(TValidator validator);

Parameters

validator TValidator

The FluentValidation validator to wrap.

Methods

FluentValidatorWrapper<TOptions,TValidator>.Validate(TOptions) Method

Validates the specified options instance using FluentValidation.

public System.Collections.Generic.IEnumerable<NexusLabs.Needlr.Generators.ValidationError> Validate(TOptions options);

Parameters

options TOptions

The options instance to validate.

Implements Validate(T)

Returns

System.Collections.Generic.IEnumerable<NexusLabs.Needlr.Generators.ValidationError>
An enumerable of validation errors.