Frognar.ValidDotNet 0.1.1

dotnet add package Frognar.ValidDotNet --version 0.1.1                
NuGet\Install-Package Frognar.ValidDotNet -Version 0.1.1                
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="Frognar.ValidDotNet" Version="0.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Frognar.ValidDotNet --version 0.1.1                
#r "nuget: Frognar.ValidDotNet, 0.1.1"                
#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.
// Install Frognar.ValidDotNet as a Cake Addin
#addin nuget:?package=Frognar.ValidDotNet&version=0.1.1

// Install Frognar.ValidDotNet as a Cake Tool
#tool nuget:?package=Frognar.ValidDotNet&version=0.1.1                

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:

  1. 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.

  2. 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

// Create a ValidationResult with no errors (valid result).
ValidationResult validResult = ValidationResult.valid;
ValidationResult otherValidResult = new();

// Create a validation result with errors.
ValidationResult invalidResult = new ValidationResult(new[]
{
    Validation.Error("Invalid input."), // new ValidationErrorMessage(Invalid input."),
    Validation.Error("Code123", "Invalid key."), // new ValidationErrorMessageWithKey("Code123", "Invalid key.")
    // Add more errors as needed
});

// Add a new error to the  existing validation result.
ValidationResult otherInvalidResult = validResult
    .AddError(new ValidationErrorMessage("Invalid input."))
    .AddError("Invalid input.") // Shorthand for adding a new error with a string message.
    .AddError(new ValidationErrorMessageWithKey("Code123", "Invalid key."))
    .AddError("Code123", "Invalid key."); // Shorthand for adding a new error with a key and a string message.

// Aggregate error messages into a single string.
string errors = invalidResult.AggregateErrors(separator: ", ", keyValueSeparator: "|");
Console.WriteLine($"Validation failed with errors: {errors}"); // "Validation failed with errors: Invalid input., Code123|Invalid key."

// Custom Error handling
ValidationResult invalidWithCustomError = validResult.AddError(new CustomError()).AddError("key", "error");
// string customError = invalidWithCustomError.AggregateErrors(); // Throws NotSupportedException
string customError = invalidWithCustomError.AggregateErrors(CustomSelector, separator: "\n");
Console.WriteLine($"Validation failed with errors: {customError}"); // "Validation failed with errors: custom\nkey:error 

record CustomError : ValidationError;

Creating a Validator and Performing Validation

char[] invalidChars = ['a', 'b', 'c'];
List<(Func<CreateUserCommand, bool> isInvalid, ValidationError error)> rules =
[
  (u => string.IsNullOrWhiteSpace(u.Login), Validation.Error(nameof(CreateUserCommand.Login), "Login cannot be empty")),
  (u => u.Password.Length < 10, Validation.Error(nameof(CreateUserCommand.Password), "Password must be at least 10 characters long")),
  (u => u.Password.Any(c => invalidChars.Contains(c)), Validation.Error(nameof(CreateUserCommand.Password), "Password cannot contain 'a', 'b', or 'c'")),
];

Validator<CreateUserCommand> validator = new(rules);
ValidationResult validation = validator.Validate(new CreateUserCommand("user", "password"));
if (validation.IsValid) {
  // Handle success
} else {
  // Handle errors
  string errorMessages = validation.AggregateErrors(", ");
  Console.WriteLine($"Validation failed with errors: {errorMessages}"); // Validation failed with errors: Password:Password must be at least 10 characters long, Password:Password cannot contain 'a', 'b', or 'c'
}

record CreateUserCommand(string Login, string Password);
Inheritance supported!
public class OddIntsValidator() : Validator<int>([(i => i % 2 == 0, Validation.Error("Value must be odd"))]);

License

This library is licensed under the MIT License - see the LICENSE file for details.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last updated
0.1.1 118 2/2/2024
0.1.0 92 2/1/2024
0.0.1 92 2/1/2024

v 0.1.1:
- support for custom error types
- factory methods for validation errors