Skip to content

oubaichmed/FlexValidator

Repository files navigation

FlexValidator

A flexible and powerful validation library for .NET applications with fluent API support.

Build Status NuGet License

πŸš€ Features

  • 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

πŸ“¦ Installation

Install via NuGet Package Manager:

Install-Package FlexValidator

Or via .NET CLI:

dotnet add package FlexValidator

🎯 Quick Start

Basic Usage

using 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}");
    }
}

Async Validation

var result = await validator.ValidateAsync(customer);

Inline Validation

var validator = new InlineValidator<Customer>();
validator.RuleFor(x => x.Name).NotEmpty();
validator.RuleFor(x => x.Age).GreaterThan(0);

var result = validator.Validate(customer);

πŸ”§ Advanced Usage

Custom Validators

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("@");
    }
}

Conditional Validation

RuleFor(x => x.Age)
    .GreaterThan(18)
    .When(x => x.IsAdult);

Child Object Validation

RuleFor(x => x.Address)
    .ChildRules(address =>
    {
        address.RuleFor(x => x.Street).NotEmpty();
        address.RuleFor(x => x.City).NotEmpty();
    });

πŸ—οΈ Integration

ASP.NET Core

// 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();
    }
}

Blazor

@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
    }
}

πŸ“š Documentation

For comprehensive documentation, please visit our Wiki.

🀝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for your changes
  5. Submit a pull request

πŸ“ License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Inspired by FluentValidation
  • Thanks to all contributors who have helped improve this library

πŸ“Š Performance

FlexValidator is designed for high-performance scenarios with:

  • Compiled expressions for fast property access
  • Minimal memory allocations
  • Efficient async validation pipeline

πŸ”— Links


Made with ❀️ by Mohamed OUBAICH

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages