Convex.Shared.Validation 1.0.0

dotnet add package Convex.Shared.Validation --version 1.0.0
                    
NuGet\Install-Package Convex.Shared.Validation -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Convex.Shared.Validation" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Convex.Shared.Validation" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Convex.Shared.Validation" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Convex.Shared.Validation --version 1.0.0
                    
#r "nuget: Convex.Shared.Validation, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Convex.Shared.Validation@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Convex.Shared.Validation&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Convex.Shared.Validation&version=1.0.0
                    
Install as a Cake Tool

Convex.Shared.Validation

Validation utilities for Convex microservices.

Features

  • FluentValidation Integration: Built on FluentValidation
  • Base Validators: Common validation patterns
  • Email Validation: Email format validation
  • Phone Validation: Phone number format validation
  • Password Strength: Password strength validation
  • Custom Validators: Easy to create custom validators

Installation

<PackageReference Include="Convex.Shared.Validation" Version="1.0.0" />

Quick Start

1. Register Services

// In Program.cs
services.AddConvexValidation();

2. Create Validators

public class UserValidator : BaseValidator<User>
{
    public UserValidator()
    {
        RuleFor(x => x.Name)
            .NotEmpty().WithMessage("Name is required")
            .Length(2, 50).WithMessage("Name must be between 2 and 50 characters");

        RuleFor(x => x.Email)
            .NotEmpty().WithMessage("Email is required")
            .Must(IsValidEmail).WithMessage("Invalid email format");

        RuleFor(x => x.Phone)
            .Must(IsValidPhoneNumber).WithMessage("Invalid phone number format");

        RuleFor(x => x.Password)
            .Must(IsStrongPassword).WithMessage("Password must be strong");
    }
}

3. Use Validators

public class UserService
{
    private readonly IValidator<User> _validator;

    public UserService(IValidator<User> validator)
    {
        _validator = validator;
    }

    public async Task<ApiResponse<User>> CreateUserAsync(User user)
    {
        var validationResult = await _validator.ValidateAsync(user);
        
        if (!validationResult.IsValid)
        {
            var errors = validationResult.Errors
                .ToDictionary(e => e.PropertyName, e => e.ErrorMessage);
            return ApiResponse<User>.ErrorResult(errors);
        }

        // Create user...
        return ApiResponse<User>.SuccessResult(user);
    }
}

Built-in Validators

Email Validation

RuleFor(x => x.Email)
    .Must(IsValidEmail).WithMessage("Invalid email format");

Phone Validation

RuleFor(x => x.Phone)
    .Must(IsValidPhoneNumber).WithMessage("Invalid phone number format");

Password Strength

RuleFor(x => x.Password)
    .Must(IsStrongPassword).WithMessage("Password must be strong");

Custom Validators

Creating Custom Validators

public class BetValidator : BaseValidator<Bet>
{
    public BetValidator()
    {
        RuleFor(x => x.Amount)
            .GreaterThan(0).WithMessage("Amount must be greater than 0")
            .LessThan(10000).WithMessage("Amount cannot exceed $10,000");

        RuleFor(x => x.Odds)
            .GreaterThan(1).WithMessage("Odds must be greater than 1")
            .LessThan(1000).WithMessage("Odds cannot exceed 1000");

        RuleFor(x => x.EventId)
            .NotEmpty().WithMessage("Event ID is required");
    }
}

Async Validators

public class UserValidator : BaseValidator<User>
{
    private readonly IUserRepository _userRepository;

    public UserValidator(IUserRepository userRepository)
    {
        _userRepository = userRepository;

        RuleFor(x => x.Email)
            .MustAsync(async (email, cancellation) =>
            {
                var existingUser = await _userRepository.GetByEmailAsync(email);
                return existingUser == null;
            }).WithMessage("Email already exists");
    }
}

Validation Results

Handling Validation Results

var validationResult = await _validator.ValidateAsync(user);

if (!validationResult.IsValid)
{
    foreach (var error in validationResult.Errors)
    {
        Console.WriteLine($"{error.PropertyName}: {error.ErrorMessage}");
    }
}

Converting to Dictionary

var errors = validationResult.Errors
    .GroupBy(e => e.PropertyName)
    .ToDictionary(g => g.Key, g => g.Select(e => e.ErrorMessage).ToArray());

Best Practices

  1. Use BaseValidator: Inherit from BaseValidator for common patterns
  2. Async Validation: Use async validators for database checks
  3. Clear Messages: Provide clear, user-friendly error messages
  4. Reusable Rules: Create reusable validation rules
  5. Performance: Consider performance for complex validations

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 164 10/17/2025

Initial release of Convex.Shared.Validation