Skip to content

IOptionsValidator T

NexusLabs.Needlr.Generators

IOptionsValidator<T> Interface

Interface for validating options types.

public interface IOptionsValidator<in T>
    where T : class

Type parameters

T

The options type to validate.

Example

[Options(ValidateOnStart = true, Validator = typeof(StripeOptionsValidator))]
public class StripeOptions
{
    public string ApiKey { get; set; } = "";
}

public class StripeOptionsValidator : IOptionsValidator<StripeOptions>
{
    public IEnumerable<ValidationError> Validate(StripeOptions options)
    {
        if (!options.ApiKey.StartsWith("sk_"))
            yield return new ValidationError
            {
                Message = "API key must start with 'sk_'",
                PropertyName = nameof(StripeOptions.ApiKey)
            };
    }
}

Remarks

Implement this interface to create an external validator for an options type. The validator is specified using the Validator property on the OptionsAttribute.

Methods

IOptionsValidator<T>.Validate(T) Method

Validates the specified options instance.

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

Parameters

options T

The options instance to validate.

Returns

System.Collections.Generic.IEnumerable<ValidationError>
An enumerable of validation errors. Return an empty enumerable if validation succeeds.