Entensions to support validation of new types using fluent validations
$ dotnet add package Oneiro.NewType.FluentValidationPlease reference FluentValidation for more information on how to use the library.
Oneiro.NewType.FluentValidation provides extensions to support validation of new types using FluentValidation.
To install the package, use the following command:
dotnet add package Oneiro.NewType.FluentValidation
First, create a new type by inheriting from NewType<T, TNewType> or use one
of the many built in new types:
Next, create a validator for your new type using FluentValidation.
var validator = Validations.CreateValidatorFor<int, NewInt>(
builder => builder.GreaterThan(0)
);
You can now use the validator to validate instances of your new type:
var newInt = NewInt.From(100);
var validator = Validations.CreateValidatorFor<int, NewInt>(
builder => builder.GreaterThan(0)
);
ValidationResult result = validator.Validate(newInt);
if (result.IsValid)
Console.WriteLine("Validation succeeded.");
else {
Console.WriteLine("Validation failed:");
foreach (var error in result.Errors)
Console.WriteLine($"- {error.ErrorMessage}");
}
ValidatedNewType and IValidatorAny ValidatedNewType<T, TNewType> rules defined can be used and converted into
an IValidator<TNewType> using the Validations.CreateValidatorFor method.
sealed record ValidatedNewInt : ValidatedNewType<int, NewInt> {
public ValidatedNewInt(int value) : base(value) {
RuleFor(x => x).GreaterThan(0);
}
}
var validator = Validations.CreateValidatorFor<int, ValidatedNewInt>();
This project is licensed under the MIT License. See the LICENSE file for more details.