RuleValidator 1.0.1

dotnet add package RuleValidator --version 1.0.1
                    
NuGet\Install-Package RuleValidator -Version 1.0.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="RuleValidator" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RuleValidator" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="RuleValidator" />
                    
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 RuleValidator --version 1.0.1
                    
#r "nuget: RuleValidator, 1.0.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.
#:package RuleValidator@1.0.1
                    
#: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=RuleValidator&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=RuleValidator&version=1.0.1
                    
Install as a Cake Tool

RuleValidator

RuleValidator is a small, fluent validation helper for .NET that collects validation rules and executes them only when Validate() is called.

Features

  • Fluent chaining of property rules: .Property(x ⇒ x.Name).ShouldNotBeNullOrEmpty().ShouldHaveMinLength(3)
  • Deferred execution: register rules first, run them later via Validate() or Validate(obj)
  • Configurable validation options (email regex, password rules) via Configure(...)
  • Add custom validation rules with Custom(...) that receive the property value and the parent object

Quick usage

// configure validator
var validator = RuleValidation.RuleValidator.For(myObj)
    .Configure(cfg => {
        cfg.EmailRegex = @"^[^@\s]+@[^@\s]+\.[^@\s]+$";
        cfg.PasswordOption.MinLength = 8;
        cfg.PasswordOption.RequireSpecialCharacter = true;
    });

// add rules
validator.Property(x => x.Name)
    .ShouldNotBeNullOrEmpty()
    .ShouldHaveMinLength(3)
    .ShouldHaveMaxLength(50);

validator.Property(x => x.Email)
    .ShouldNotBeNullOrEmpty()
    .ShouldBeEmail();

// run validation
var errors = validator.Validate();

Custom rule example

validator.Property(x => x.SomeValue)
    .Custom((val, parent) => {
        if (val == null) return "SomeValue is required";
        if ((int)val < parent.Minimum) return "SomeValue too small";
        return null;
    });

More Examples

  1. Validate a property on a class instance (string length checks)
// MyDummyClass from tests
var obj = new MyDummyClass { Name = "John Doe" };
var validator = RuleValidator<MyDummyClass>.For(obj);
validator.Property(x => x.Name)
    .ShouldHaveMaxLength(20)
    .ShouldHaveMinLength(8);

var errors = validator.Validate(); // expected: no errors
  1. Validate a raw value (string)
// validate raw string values
var rawValidator = new RuleValidator<string>();
rawValidator.Property(x => x).ShouldNotBeNullOrEmpty();

var ok = rawValidator.Validate("Test");        // expected: no errors
var empty = rawValidator.Validate(string.Empty); // expected: 1 error for the root value
  1. Validate numeric values (range checks)
var numValidator = new RuleValidator<int>();
numValidator.Property(x => x).ShouldBeInRange(10, 20);

var ok = numValidator.Validate(19); // expected: no errors
var bad = numValidator.Validate(21); // expected: 1 error
  1. Validate boolean values
// RuleValidator correctly handles boolean properties
var boolObj = new { IsActive = true };
var boolValidator = RuleValidator.For(boolObj);
boolValidator.Property(x => x.IsActive).ShouldBeTrue();

boolValidator.Validate(); // expected: no errors

boolObj = new { IsActive = false };
boolValidator = RuleValidator.For(boolObj);
boolValidator.Property(x => x.IsActive).ShouldBeFalse();

var errors = boolValidator.Validate(); // expected: no errors

Tips and notes

  • Configure the validator once (for things like email regex and password options) and reuse the configured RuleValidator where appropriate.
  • Use .Custom(...) to implement rules that depend on multiple properties or external state; Custom receives the current property value and the parent object.
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.  net9.0 was computed.  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.
  • 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
1.0.1 184 10/26/2025