Frognar.ValidDotNet
0.1.0
See the version list below for details.
dotnet add package Frognar.ValidDotNet --version 0.1.0
NuGet\Install-Package Frognar.ValidDotNet -Version 0.1.0
<PackageReference Include="Frognar.ValidDotNet" Version="0.1.0" />
paket add Frognar.ValidDotNet --version 0.1.0
#r "nuget: Frognar.ValidDotNet, 0.1.0"
// Install Frognar.ValidDotNet as a Cake Addin #addin nuget:?package=Frognar.ValidDotNet&version=0.1.0 // Install Frognar.ValidDotNet as a Cake Tool #tool nuget:?package=Frognar.ValidDotNet&version=0.1.0
ValidDotNet Library
The ValidDotNet library provides a lightweight validation framework for .NET applications, allowing you to define and apply validation rules to instances of specified types.
Table of Contents
Overview
The library consists of two main classes:
ValidationResult: Represents the result of a validation operation, containing a collection of errors. Instances are immutable, and a static 'valid' instance is provided for successful validations.
Validator<T>: A generic class for validating instances of a specified type using a set of rules. You can define custom validation rules and easily validate objects of type T.
Installation
To use ValidDotNet in your project, you can install it via NuGet Package Manager:
dotnet add package Frognar.ValidDotNet
Usage
ValidationResult
The 'ValidationResult' struct represents the outcome of a validation operation. It contains a collection of errors and provides methods to work with validation results.
Validator<T>
The 'Validator<T>' class allows you to define and apply validation rules to objects of type T. You can create a validator with an initial set of rules and then use it to validate objects.
Examples
Here are some basic examples to get you started:
Creating a ValidationResult
ValidationResult newResult = new();
ValidationResult valid = ValidationResult.valid;
ValidationResult invalid = valid.AddError("error");
Creating a Validator and Performing Validation
List<(Func<string, bool> isInvalid, string errorMessage)> rules = new() {
(s => string.IsNullOrWhiteSpace(s), "String cannot be empty or whitespace."),
(s => s.Length > 10, "String length should not exceed 10 characters.")
};
Validator<string> validator = new(rules);
ValidationResult validation = validator.Validate("SampleString");
if (validation.IsValid) {
// Handle success
} else {
// Handle errors
string errorMessages = validation.AggregateErrors(", ");
Console.WriteLine($"Validation failed with errors: {errorMessages}");
}
Inheritance supported!
public class OddIntsValidator() : Validator<int>([(i => i % 2 == 0, "Value must be odd")]);
License
This library is licensed under the MIT License - see the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.