Frognar.ValidDotNet 0.1.0

There is a newer version of this package available.
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                
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.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Frognar.ValidDotNet --version 0.1.0                
#r "nuget: Frognar.ValidDotNet, 0.1.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.
// 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:

  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

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 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 141 2/2/2024
0.1.0 108 2/1/2024
0.0.1 119 2/1/2024