A flexible and powerful validation library for .NET applications with fluent API support.
- Fluent API: Easy-to-use and readable validation rules
- Async Support: Full async/await support for validation
- Extensible: Easy to extend with custom validators
- Performance: Optimized for high-performance scenarios
- Localization: Support for custom error messages and localization
- Integration: Works seamlessly with ASP.NET Core, Blazor, and other .NET frameworks
Install via NuGet Package Manager:
Install-Package FlexValidatorOr via .NET CLI:
dotnet add package FlexValidatorusing FlexValidator;
public class Customer
{
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
public class CustomerValidator : AbstractValidator<Customer>
{
public CustomerValidator()
{
RuleFor(x => x.Name)
.NotEmpty()
.WithMessage("Name is required");
RuleFor(x => x.Age)
.GreaterThan(0)
.WithMessage("Age must be greater than 0");
RuleFor(x => x.Email)
.NotEmpty()
.WithMessage("Email is required");
}
}
// Usage
var validator = new CustomerValidator();
var customer = new Customer { Name = "John", Age = 30, Email = "john@example.com" };
var result = validator.Validate(customer);
if (result.IsValid)
{
Console.WriteLine("Customer is valid!");
}
else
{
foreach (var error in result.Errors)
{
Console.WriteLine($"Error: {error.ErrorMessage}");
}
}var result = await validator.ValidateAsync(customer);var validator = new InlineValidator<Customer>();
validator.RuleFor(x => x.Name).NotEmpty();
validator.RuleFor(x => x.Age).GreaterThan(0);
var result = validator.Validate(customer);public class CustomerValidator : AbstractValidator<Customer>
{
public CustomerValidator()
{
RuleFor(x => x.Email)
.Must(BeValidEmail)
.WithMessage("Please enter a valid email address");
}
private bool BeValidEmail(string email)
{
// Custom email validation logic
return !string.IsNullOrEmpty(email) && email.Contains("@");
}
}RuleFor(x => x.Age)
.GreaterThan(18)
.When(x => x.IsAdult);RuleFor(x => x.Address)
.ChildRules(address =>
{
address.RuleFor(x => x.Street).NotEmpty();
address.RuleFor(x => x.City).NotEmpty();
});// Program.cs
builder.Services.AddScoped<IValidator<Customer>, CustomerValidator>();
// Controller
[ApiController]
public class CustomerController : ControllerBase
{
private readonly IValidator<Customer> _validator;
public CustomerController(IValidator<Customer> validator)
{
_validator = validator;
}
[HttpPost]
public async Task<IActionResult> CreateCustomer([FromBody] Customer customer)
{
var result = await _validator.ValidateAsync(customer);
if (!result.IsValid)
{
return BadRequest(result.Errors);
}
// Process valid customer
return Ok();
}
}@using FlexValidator
<EditForm Model="@customer" OnValidSubmit="@HandleValidSubmit">
<FlexValidatorValidator />
<ValidationSummary />
<InputText @bind-Value="customer.Name" />
<ValidationMessage For="@(() => customer.Name)" />
<button type="submit">Submit</button>
</EditForm>
@code {
private Customer customer = new();
private void HandleValidSubmit()
{
// Handle valid form submission
}
}For comprehensive documentation, please visit our Wiki.
We welcome contributions! Please see our Contributing Guide for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for your changes
- Submit a pull request
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
- Inspired by FluentValidation
- Thanks to all contributors who have helped improve this library
FlexValidator is designed for high-performance scenarios with:
- Compiled expressions for fast property access
- Minimal memory allocations
- Efficient async validation pipeline
Made with β€οΈ by Mohamed OUBAICH