TypeGuard.Core 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package TypeGuard.Core --version 1.0.0
                    
NuGet\Install-Package TypeGuard.Core -Version 1.0.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="TypeGuard.Core" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TypeGuard.Core" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="TypeGuard.Core" />
                    
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 TypeGuard.Core --version 1.0.0
                    
#r "nuget: TypeGuard.Core, 1.0.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.
#:package TypeGuard.Core@1.0.0
                    
#: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=TypeGuard.Core&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=TypeGuard.Core&version=1.0.0
                    
Install as a Cake Tool

TypeGuard

<div align="center">

CI/CD CodeQL NuGet License: MIT .NET

A fluent, type-safe user input validation library for .NET with platform-specific implementations for Console, WinForms, WPF, Avalonia, MAUI, and Blazor.

</div>


📑 Table of Contents


🚀 Features

  • Type-Safe Validation - Built-in validators for all common C# types
  • Composable Rules - Chain validation rules fluently, similar to LINQ
  • Async Support - Full async/await support with cancellation tokens
  • Extensible - Add custom validators and rules with ease
  • Multi-Platform - Dedicated implementations for Console, WinForms, WPF, Avalonia, MAUI, and Blazor

📦 Installation

Install only the package for your platform. TypeGuard.Core is included automatically as a dependency.

Console

dotnet add package TypeGuard.Console

WinForms

dotnet add package TypeGuard.Winforms

WPF

dotnet add package TypeGuard.Wpf

Avalonia

dotnet add package TypeGuard.Avalonia

MAUI

dotnet add package TypeGuard.Maui

Blazor

dotnet add package TypeGuard.Blazor

ASP.NET / Razor Pages (server-side Blazor)

ASP.NET and Razor Pages use the core library directly but is currently unfinished:

dotnet add package TypeGuard.Core

Manual Installation

dotnet add reference path/to/TypeGuard.Console/TypeGuard.Console.csproj  # or your platform project

🎯 Quick Start

Console

using TypeGuard.Console;

// Simple integer input
int age = await Guard.Int("Enter your age")
    .WithRange(1, 120)
    .GetAsync();

// String with validation rules
string name = await Guard.String("Enter your name")
    .WithNoDigits()
    .WithLengthRange(2, 50)
    .GetAsync();

WinForms

using TypeGuard.Winforms;

private readonly Guard _guard;

public MyForm()
{
    InitializeComponent();
    _guard = new Guard(inputTextBox, promptLabel, errorLabel);
}

private async void submitButton_Click(object sender, EventArgs e)
{
    int age = await _guard.Int("Enter your age")
        .WithRange(1, 120)
        .GetAsync();
}

📚 Usage Guide

Simple Validation

// Integer
int count = await Guard.Int("How many items?").GetAsync();

// String
string username = await Guard.String("Enter username").GetAsync();

// DateTime
DateTime date = await Guard.DateTime("Enter a date", "dd/MM/yyyy").GetAsync();

Validation with Rules

// Age: must be between 18 and 120
int age = await Guard.Int("Enter your age")
    .WithRange(18, 120)
    .GetAsync();

// Username: 3–20 characters, letters only
string username = await Guard.String("Choose a username")
    .WithLengthRange(3, 20)
    .WithNoDigits()
    .GetAsync();

Custom Validation Rules

// Password strength
string password = await Guard.String("Create a password")
    .WithLengthRange(8, null)
    .WithRegex(@"[A-Z]", "Must contain at least one uppercase letter")
    .WithRegex(@"[a-z]", "Must contain at least one lowercase letter")
    .WithRegex(@"\d", "Must contain at least one digit")
    .WithCustomRule(
        p => p.Distinct().Count() >= 5,
        "Must contain at least 5 unique characters")
    .GetAsync();

DateTime Validation

// Strict format
DateTime appointment = await Guard.DateTime("Enter appointment date", "dd/MM/yyyy")
    .WithRange(DateTime.Today, DateTime.Today.AddMonths(6))
    .GetAsync();

// Flexible parsing
DateTime birthday = await Guard.DateTime("Enter birthday")
    .WithCustomRule(
        d => DateTime.Today.Year - d.Year >= 18,
        "Must be 18 or older")
    .GetAsync();

// Weekdays only
DateTime meeting = await Guard.DateTime("Select meeting date", "yyyy-MM-dd")
    .WithWeekday()
    .GetAsync();

🖥️ Platform Guide

Console

using TypeGuard.Console;

int age = await Guard.Int("Enter your age")
    .WithRange(1, 120)
    .GetAsync();

string name = await Guard.String("Enter your name").GetAsync();

WinForms

using TypeGuard.Winforms;

private readonly Guard _guard;

public MyForm()
{
    InitializeComponent();
    _guard = new Guard(inputTextBox, promptLabel, errorLabel);
}

private async void submitButton_Click(object sender, EventArgs e)
{
    int age = await _guard.Int("Enter your age")
        .WithRange(1, 120)
        .GetAsync();
}

WPF

using TypeGuard.Wpf;

private readonly Guard _guard;

public MyWindow()
{
    InitializeComponent();
    _guard = new Guard(inputTextBox, promptBlock, errorBlock);
}

private async void SubmitButton_Click(object sender, RoutedEventArgs e)
{
    int age = await _guard.Int("Enter your age")
        .WithRange(1, 120)
        .GetAsync();
}

Avalonia

using TypeGuard.Avalonia;

private readonly Guard _guard;

public MyView()
{
    InitializeComponent();
    _guard = new Guard(InputTextBox, PromptBlock, ErrorBlock);
}

private async void SubmitButton_Click(object sender, RoutedEventArgs e)
{
    int age = await _guard.Int("Enter your age")
        .WithRange(1, 120)
        .GetAsync();
}

MAUI

using TypeGuard.Maui;

_guard = new Guard(InputEntry, PromptLabel, ErrorLabel, SubmitButton);

int age = await _guard.Int("Enter your age")
    .WithRange(1, 120)
    .GetAsync();

Blazor

@inject Guard Guard

<InputText @bind-Value="Guard.Input.Value" />
<p>@Guard.Output.PromptMessage</p>
<p style="color: red">@Guard.Output.ErrorMessage</p>

@code {
    protected override void OnInitialized()
    {
        Guard.Output.OnStateChanged = StateHasChanged;
    }

    private async Task SubmitAsync()
    {
        int age = await Guard.Int("Enter your age")
            .WithRange(1, 120)
            .GetAsync();
    }
}

🏗️ Architecture

TypeGuard.Core              → Platform-agnostic validation logic
├── Abstractions            → Interfaces
├── Validators              → Type-specific validators
├── Rules                   → Validation rules
└── Builders                → Fluent API builders

TypeGuard.Console           → Console implementation
├── ConsoleInput            → Reads from Console.ReadLine()
├── ConsoleOutput           → Writes prompts and errors
└── Guard                   → Main entry point

TypeGuard.Winforms          → WinForms implementation (TextBox, TextBlock)
├── WinformsInput            → Reads from Control.Text
├── WinformsOutput           → Writes prompts and errors
└── Guard                   → Main entry point

TypeGuard.Wpf               → WPF implementation (TextBox, TextBlock)
├── WpfInput                → Reads from Textbox.Text
├── WpfOutput               → Writes prompts and errors
└── Guard                   → Main entry point

TypeGuard.Avalonia          → Avalonia implementation (TextBox, TextBlock)
├── AvaloniaInput            → Reads from Textbox.Text
├── AvaloniaOutput           → Writes prompts and errors
└── Guard                   → Main entry point

TypeGuard.Maui              → MAUI implementation (Entry, Label, Button)
├── MauiInput               → Reads from Entry.Text
├── MauiOutput              → Writes prompts and errors
└── Guard                   → Main entry point

TypeGuard.Blazor            → Blazor implementation (InputText, DI-injected Guard)
├── BlazorInput             → Reads from InputText.Value
├── BlazorOutput            → Writes prompts and errors
└── Guard                   → Main entry point

🔌 Extending TypeGuard

Creating Custom Rules

using TypeGuard.Core.Interfaces;

public class EmailRule : IValidationRule<string>
{
    public bool IsValid(string value) =>
        value.Contains('@') && value.Contains('.');

    public string ErrorMessage => "Must be a valid email address";
}

// Usage
string email = await Guard.String("Enter email")
    .WithCustomRule(new EmailRule())
    .GetAsync();

// Note: Email Rule already exists but this is just an example of how to create your own custom rule.

💻 Requirements

  • .NET 9.0 or .NET 10.0
  • Windows, macOS, or Linux (platform packages may have OS restrictions)

📄 License

MIT Licence — see LICENCE for details.


🤝 Contributing

Contributions are welcome! You can:

  • Report bugs or request features via Issues
  • Submit pull requests
  • Suggest improvements to the API
  • Add new type validators/builders/rules
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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 is compatible.  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.
  • net10.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (7)

Showing the top 5 NuGet packages that depend on TypeGuard.Core:

Package Downloads
TypeGuard.Console

Console implementation for TypeGuard.

TypeGuard.Blazor

Blazor implementation for TypeGuard.

TypeGuard.WinForms

Windows Forms implementation for TypeGuard.

TypeGuard.Wpf

WPF implementation for TypeGuard.

TypeGuard.Maui

MAUI implementation for TypeGuard.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 211 3/24/2026
1.1.0 159 3/20/2026
1.0.0 162 3/3/2026
0.1.1-test 208 10/31/2025
0.1.0 222 10/31/2025