TypeGuard.Console 0.1.1-test

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

TypeGuard

πŸ“‘ Table of Contents

πŸš€ Features

  • Type-Safe Validation β€” Built-in validators for int, string, DateTime, and more coming soon
  • Automatic Retry Logic β€” Invalid input automatically re-prompts the user
  • Composable Rules β€” Chain validation rules (Similar to LINQ)
  • Async Support β€” Full async/await support with cancellation tokens
  • Extensible β€” Easy to add as many custom validators and rules as you want!
  • Multi-Platform Ready β€” Separate implementations for Console, WinForms, WPF (coming soon)
  • Colored Console Output β€” Integration with ConsolePrism for beautiful error messages

πŸ“¦ Installation

NuGet Package

For console applications:

dotnet add package TypeGuard.Console

(More to come!)

The core library (TypeGuard.Core) is included automatically as a dependency.

Manual Installation

  1. Clone the repository
  2. Add references to your project:
dotnet add reference path/to/TypeGuard.Core/TypeGuard.Core.csproj
dotnet add reference path/to/TypeGuard.Console/TypeGuard.Console.csproj

🎯 Quick Start

using TypeGuard.Console;

// Simple integer input
int age = await TypeGuard.GetIntAsync("Enter your age");

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

// DateTime with specific format
DateTime birthday = await TypeGuard
    .ForDateTime("Enter your birthday", "dd/MM/yyyy")
    .WithRange(new DateTime(1900, 1, 1), DateTime.Today)
    .GetAsync();

// Integer with range validation
int score = await TypeGuard
    .ForInt("Enter score")
    .WithRange(0, 100)
    .GetAsync();

πŸ“š Usage Guide

Simple Validation

// Integer input
int count = await TypeGuard.GetIntAsync("How many items?");
int countSync = TypeGuard.GetInt("How many items?"); // Synchronous version

// String input
string username = await TypeGuard.GetStringAsync("Enter username");
string usernameSync = TypeGuard.GetString("Enter username");

Validation with Rules

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

// Username validation: 3-20 characters, no digits
string username = await TypeGuard
    .ForString("Choose a username")
    .WithLengthRange(3, 20)
    .WithNoDigits()
    .GetAsync();

// Email validation using regex
string email = await TypeGuard
    .ForString("Enter your email")
    .WithRegex(@"^[^@\s]+@[^@\s]+\.[^@\s]+$", "Invalid email format")
    .GetAsync();

Custom Validation Rules

// Even numbers only (Might be dedicated rule in the future)
int evenNumber = await TypeGuard
    .ForInt("Enter an even number")
    .WithCustomRule(x => x % 2 == 0, "Must be an even number")
    .GetAsync();

// Password strength
string password = await TypeGuard
    .ForString("Create 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

(No time support yet)

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

// Flexible format (accepts various date formats)
DateTime birthday = await TypeGuard
    .ForDateTime("Enter birthday", format: null) // null = flexible parsing
    .WithCustomRule(
        d => DateTime.Today.Year - d.Year >= 18,
        "Must be 18 or older")
    .GetAsync();

// Business days only
DateTime meetingDate = await TypeGuard
    .ForDateTime("Select meeting date", "yyyy-MM-dd")
    .WithCustomRule(
        d => d.DayOfWeek != DayOfWeek.Saturday && d.DayOfWeek != DayOfWeek.Sunday,
        "Must be a weekday")
    .GetAsync();

πŸ—οΈ Architecture

TypeGuard.Core          β†’ Platform-agnostic validation logic
β”œβ”€β”€ Abstractions        β†’ Interfaces (IValidator, IInputProvider, IOutputProvider)
β”œβ”€β”€ Validators          β†’ Type-specific validators (IntValidator, StringValidator, etc.)
β”œβ”€β”€ Rules               β†’ Validation rules (RangeRule, RegexRule, CustomRule, etc.)
└── Builders            β†’ Fluent API builders

TypeGuard.Console       β†’ Console-specific implementation
β”œβ”€β”€ ConsoleInput   β†’ Reads from Console.ReadLine()
β”œβ”€β”€ ConsoleOutput  β†’ Writes errors/prompts with ConsolePrism
└── TypeGuard              β†’ Main entry point for developers

πŸ”Œ Extending TypeGuard

Creating Custom Validators

public class DecimalValidator : ValidatorBase<decimal>
{
    public DecimalValidator(
        IInputProvider inputProvider,
        IOutputProvider outputProvider,
        string prompt)
        : base(inputProvider, outputProvider, prompt)
    {
    }

    protected override bool TryParse(string? input, out decimal value, out string? errorMessage)
    {
        if (decimal.TryParse(input, out value))
        {
            errorMessage = null;
            return true;
        }

        errorMessage = "Please enter a valid decimal number";
        value = default;
        return false;
    }
}

Creating Custom Rules

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

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

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

πŸ’» Requirements

  • .NET 9.0 or higher
  • TypeGuard.Console requires ConsolePrism for colored output (Dependency)
  • Works on Windows, macOS, and Linux

πŸ“„ License

s MIT License β€” see LICENSE file for details

🀝 Contributing

Contributions welcome! Feel free to:

  • Report bugs or request features via Issues
  • Submit pull requests
  • Suggest improvements to the API

πŸ—ΊοΈ Roadmap

Current version: v0.1.0 (Pre-release)

Planned Features:

  • WinForms implementation
  • WPF implementation
  • TimeSpan validator
  • Decimal/Float validators
  • Collection validators
  • Comprehensive unit tests?
  • Full XML documentation
  • Interactive demo application?

Last Updated: 2025

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

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-test 150 10/31/2025
0.1.0 165 10/31/2025