Rapid.Validation
1.0.5
dotnet add package Rapid.Validation --version 1.0.5
NuGet\Install-Package Rapid.Validation -Version 1.0.5
<PackageReference Include="Rapid.Validation" Version="1.0.5" />
paket add Rapid.Validation --version 1.0.5
#r "nuget: Rapid.Validation, 1.0.5"
// Install Rapid.Validation as a Cake Addin #addin nuget:?package=Rapid.Validation&version=1.0.5 // Install Rapid.Validation as a Cake Tool #tool nuget:?package=Rapid.Validation&version=1.0.5
Rapid.Validation
Rapid.Validation is a comprehensive validation library for .NET that provides a set of common validation rules, custom validation rules, and advanced features to help developers validate user input easily and efficiently.
Features
BatchValidator: Allows batch validation of multiple inputs at once, returning a collection of validation results.
ValidationServiceCollectionExtensions: Provides integration with dependency injection frameworks.
ValidationResult: Represents the result of a validation operation, including whether it was successful and any error messages.
UrlValidator: Validates URLs.
ValidationMetadata: Provides metadata about validation rules, such as descriptions and categories.
PhoneNumberValidator: Validates phone numbers, supporting different formats and countries.
PasswordValidator: Validates password strength based on length, complexity, and common passwords.
LoggingValidator: Integrates logging to track validation activities and errors.
IValidator: Interface for all validators.
FluentValidator: Offers a fluent API for defining validation rules.
EmailValidator: Validates email addresses.
DateTimeValidator: Validates date and time values within a specified range.
CustomValidator: Allows users to define custom validation rules using lambda expressions or delegate functions.
CreditCardValidator: Validates credit card numbers using the Luhn algorithm.
ConfigurableValidator: Allows users to define validation rules in configuration files and load them at runtime.
ValidationPipeline: Implements a validation pipeline that allows users to add pre- and post-validation steps.
Installation
Install the package via NuGet:
Install-Package Rapid.Validation
## Usage
### Email Validation
var emailValidator = new EmailValidator();
var result = emailValidator.Validate("test@example.com");
if (result.IsValid)
{
Console.WriteLine("Valid email.");
}
else
{
Console.WriteLine(result.ErrorMessage);
}
### Password Validation
var passwordValidator = new PasswordValidator();
var result = passwordValidator.Validate("P@ssw0rd");
if (result.IsValid)
{
Console.WriteLine("Valid password.");
}
else
{
Console.WriteLine(result.ErrorMessage);
}
### Phone Number Validation
var phoneNumberValidator = new PhoneNumberValidator();
var result = phoneNumberValidator.Validate("+1234567890");
if (result.IsValid)
{
Console.WriteLine("Valid phone number.");
}
else
{
Console.WriteLine(result.ErrorMessage);
}
### URL Validation
var urlValidator = new UrlValidator();
var result = urlValidator.Validate("https://example.com");
if (result.IsValid)
{
Console.WriteLine("Valid URL.");
}
else
{
Console.WriteLine(result.ErrorMessage);
}
### Date and Time Validation
var dateTimeValidator = new DateTimeValidator(DateTime.Now.AddYears(-1), DateTime.Now.AddYears(1));
var result = dateTimeValidator.Validate(DateTime.Now);
if (result.IsValid)
{
Console.WriteLine("Valid date.");
}
else
{
Console.WriteLine(result.ErrorMessage);
}
### Credit Card Validation
var creditCardValidator = new CreditCardValidator();
var result = creditCardValidator.Validate("4111111111111111");
if (result.IsValid)
{
Console.WriteLine("Valid credit card number.");
}
else
{
Console.WriteLine(result.ErrorMessage);
}
### Custom Validation
var customValidator = new CustomValidator<int>(x => x > 0 ? ValidationResult.Success() : ValidationResult.Failure("Value must be positive."));
var result = customValidator.Validate(-1);
if (result.IsValid)
{
Console.WriteLine("Valid value.");
}
else
{
Console.WriteLine(result.ErrorMessage);
}
### Fluent API
var fluentValidator = new FluentValidator<string>()
.AddRule(new EmailValidator())
.AddRule(new UrlValidator());
var result = fluentValidator.Validate("test@example.com");
if (result.IsValid)
{
Console.WriteLine("Valid input.");
}
else
{
Console.WriteLine(result.ErrorMessage);
}
### Batch Validation
var batchValidator = new BatchValidator<string>(new EmailValidator());
var results = batchValidator.Validate(new List<string> { "test@example.com", "invalid-email" });
foreach (var result in results)
{
if (result.IsValid)
{
Console.WriteLine("Valid input.");
}
else
{
Console.WriteLine(result.ErrorMessage);
}
}
### Dependency Injection
public void ConfigureServices(IServiceCollection services)
{
services.AddValidators();
}
### Logging
var logger = LoggerFactory.Create(builder => builder.AddConsole()).CreateLogger<LoggingValidator<string>>();
var loggingValidator = new LoggingValidator<string>(new EmailValidator(), logger);
var result = loggingValidator.Validate("test@example.com");
if (result.IsValid)
{
Console.WriteLine("Valid email.");
}
else
{
Console.WriteLine(result.ErrorMessage);
}
### Configuration-Based Validation
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var configurableValidator = new ConfigurableValidator<string>(configuration);
var result = configurableValidator.Validate("test@example.com");
if (result.IsValid)
{
Console.WriteLine("Valid input.");
}
else
{
Console.WriteLine(result.ErrorMessage);
}
### Validation Pipeline
var pipeline = new ValidationPipeline<string>()
.AddStep(input => input.Length > 5 ? ValidationResult.Success() : ValidationResult.Failure("Input is too short."))
.AddStep(input => input.Contains("@") ? ValidationResult.Success() : ValidationResult.Failure("Input must contain '@'."));
var result = pipeline.Execute("test@example.com");
if (result.IsValid)
{
Console.WriteLine("Valid input.");
}
else
{
Console.WriteLine(result.ErrorMessage);
}
## License
This project is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. |
-
net9.0
- Microsoft.Extensions.Configuration (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
- Microsoft.Extensions.Logging (>= 9.0.0)
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.5 | 2,388 | 12/16/2024 |