DotValidator 1.0.7
dotnet add package DotValidator --version 1.0.7
NuGet\Install-Package DotValidator -Version 1.0.7
<PackageReference Include="DotValidator" Version="1.0.7" />
<PackageVersion Include="DotValidator" Version="1.0.7" />
<PackageReference Include="DotValidator" />
paket add DotValidator --version 1.0.7
#r "nuget: DotValidator, 1.0.7"
#:package DotValidator@1.0.7
#addin nuget:?package=DotValidator&version=1.0.7
#tool nuget:?package=DotValidator&version=1.0.7
DotValidator
DotValidator is a lightweight, fluent, and easy-to-use .NET validation library that provides ready-to-use validators for common data types. It offers both static validator methods and convenient extension methods for clean, readable validation code.
📋 Table of Contents
- Features
- Installation
- Quick Start
- Usage Examples
- Using Static Validators
- Benefits
- Project Structure
- Future Plans
- License
✅ Features
DotValidator provides comprehensive validation methods for the following data types:
String Validation
Validate string content, format, and length:
- Null/Empty Checks:
IsNotNullOrEmpty(),IsNotNullOrWhiteSpace() - Length Validation:
HasLengthBetween(),HasMinLength(),HasMaxLength() - Format Validation:
IsAlphabetic(),IsAlphanumeric(),IsNumeric() - Pattern Matching:
MatchesPattern()for regex validation - String Operations:
StartsWith(),EndsWith(),Contains()
DateTime Validation
Validate dates, times, and age calculations:
- Temporal Checks:
IsPast(),IsFuture(),IsToday() - Age Validation:
IsAgeAtLeast(),IsAgeAtMost() - Range Validation:
IsBetween(),IsValidRange(),IsWithinDays() - Validity Checks:
IsValid()to ensure date is not default
Integer Validation
Validate numeric values and properties:
- Sign Checks:
IsPositive(),IsNegative(),IsZero(),IsNonNegative(),IsNonPositive() - Parity Checks:
IsEven(),IsOdd() - Comparison:
IsGreaterThan(),IsLessThan(),IsEqualTo(),IsBetween() - Mathematical:
IsMultipleOf()
Collection Validation
Validate collections and enumerables:
- Null/Empty Checks:
IsNotNullOrEmpty(),IsNullOrEmpty() - Element Checks:
ContainsElement(),AllNotNull() - Count Validation:
HasMinimumCount(),HasMaximumCount(),HasExactCount()
Dictionary Validation
Validate dictionary structures and contents:
- Key Validation:
HasKey(),HasKeys()(check for single or multiple keys) - Value Validation:
HasValue(),ValidateUniqueValues() - Type Safety:
EnsureValueType()to verify all values match expected type - Size Validation:
ValidateDictionarySize()for count range checks - State Checks:
IsNotNullOrEmpty(),IsEmpty()
Email Validation
Validate email address format:
- Format Check:
IsEmail()validates standard email format
📦 Installation
Using NuGet Package Manager
Install-Package DotValidator
Using .NET CLI
dotnet add package DotValidator
Using PackageReference
Add to your .csproj file:
<PackageReference Include="DotValidator" Version="1.0.0" />
🚀 Quick Start
After installing the package, simply import the extension methods namespace:
using DotValidator.Extensions;
// Validate a string
string username = "JohnDoe123";
if (username.IsNotNullOrEmpty() && username.HasLengthBetween(3, 20))
{
Console.WriteLine("Valid username!");
}
// Validate a number
int age = 25;
if (age.IsPositive() && age.IsBetween(18, 100))
{
Console.WriteLine("Valid age!");
}
🧩 Usage Examples
String Validation
Validate string content, length, and format:
using DotValidator.Extensions;
string name = "John123";
// Check if not empty and alphanumeric
if (name.IsNotNullOrEmpty() && name.IsAlphanumeric())
{
Console.WriteLine("Valid name format!");
}
// Validate length
if (name.HasLengthBetween(3, 10))
{
Console.WriteLine("Name length is valid!");
}
// Pattern matching
string phone = "123-456-7890";
if (phone.MatchesPattern(@"^\d{3}-\d{3}-\d{4}$"))
{
Console.WriteLine("Valid phone format!");
}
// String operations
string email = "user@example.com";
if (email.StartsWith("user") && email.Contains("@"))
{
Console.WriteLine("Email starts with 'user'!");
}
DateTime Validation
Validate dates, calculate ages, and check date ranges:
using DotValidator.Extensions;
// Age validation
DateTime birthDate = new DateTime(2000, 5, 15);
if (birthDate.IsAgeAtLeast(18) && birthDate.IsPast())
{
Console.WriteLine("User is adult and birth date is valid.");
}
// Date range validation
DateTime start = new DateTime(2023, 1, 1);
DateTime end = new DateTime(2025, 1, 1);
if (start.IsValidRange(end))
{
Console.WriteLine("Date range is valid.");
}
// Temporal checks
DateTime appointment = new DateTime(2024, 12, 25);
if (appointment.IsFuture() && appointment.IsWithinDays(30))
{
Console.WriteLine("Appointment is in the near future.");
}
// Today check
if (DateTime.Now.IsToday())
{
Console.WriteLine("It's today!");
}
Integer Validation
Validate numeric values and mathematical properties:
using DotValidator.Extensions;
int number = 10;
// Basic checks
if (number.IsPositive() && number.IsEven())
{
Console.WriteLine("Number is positive and even!");
}
// Multiple check
if (number.IsMultipleOf(5))
{
Console.WriteLine("Number is divisible by 5!");
}
// Range validation
int score = 85;
if (score.IsBetween(0, 100))
{
Console.WriteLine("Score is within valid range!");
}
// Comparison
int threshold = 50;
if (score.IsGreaterThan(threshold))
{
Console.WriteLine("Score exceeds threshold!");
}
Collection Validation
Validate collections and their contents:
using DotValidator.Extensions;
using System.Collections.Generic;
var names = new List<string> { "Alice", "Bob", "Charlie" };
// Basic validation
if (names.IsNotNullOrEmpty() && names.HasMinimumCount(2))
{
Console.WriteLine("Collection has sufficient items!");
}
// Null check for all elements
if (names.AllNotNull())
{
Console.WriteLine("All elements are not null!");
}
// Count validation
if (names.HasExactCount(3))
{
Console.WriteLine("Collection has exactly 3 items!");
}
// Element check
if (names.ContainsElement("Alice"))
{
Console.WriteLine("Collection contains 'Alice'!");
}
Dictionary Validation
Validate dictionary structures, keys, values, and types:
using DotValidator.Extensions;
using System.Collections.Generic;
var userSettings = new Dictionary<string, object>
{
{ "theme", "dark" },
{ "language", "en" },
{ "notifications", true }
};
// Key validation
if (userSettings.HasKey("theme"))
{
Console.WriteLine("Theme setting exists!");
}
// Multiple keys validation
var requiredKeys = new[] { "theme", "language" };
if (userSettings.HasKeys(requiredKeys))
{
Console.WriteLine("All required settings exist!");
}
// Value validation
if (userSettings.HasValue("dark"))
{
Console.WriteLine("Dark theme is set!");
}
// Size validation
if (userSettings.ValidateDictionarySize(1, 10))
{
Console.WriteLine("Dictionary size is within range!");
}
// Type validation
var stringValues = new Dictionary<string, string>
{
{ "name", "John" },
{ "email", "john@example.com" }
};
if (stringValues.EnsureValueType<string, string, string>())
{
Console.WriteLine("All values are strings!");
}
// Unique values validation
var uniqueDict = new Dictionary<int, string>
{
{ 1, "one" },
{ 2, "two" },
{ 3, "three" }
};
if (uniqueDict.ValidateUniqueValues())
{
Console.WriteLine("All values are unique!");
}
// Empty check
Dictionary<string, int>? emptyDict = null;
if (emptyDict.IsEmpty())
{
Console.WriteLine("Dictionary is null or empty!");
}
Email Validation
Validate email address format:
using DotValidator.Extensions;
string email = "user@example.com";
if (email.IsEmail())
{
Console.WriteLine("Valid email address!");
}
// Combine with other validations
if (email.IsEmail() && email.HasMaxLength(100))
{
Console.WriteLine("Valid email with acceptable length!");
}
🔧 Using Static Validators
You can also use the static validator classes directly without extension methods:
using DotValidator.Validators;
// String validation
bool isValid = StringValidator.IsNotNullOrEmpty("test");
// DateTime validation
bool isAdult = DateTimeValidator.IsAgeAtLeast(birthDate, 18);
// Integer validation
bool isPositive = IntValidator.IsPositive(10);
// Collection validation
bool hasItems = CollectionValidator.IsNotNullOrEmpty(myList);
// Dictionary validation
bool hasKey = DictionaryValidator.HasKey(myDict, "key");
🌟 Benefits
- Lightweight: Minimal dependencies, fast performance
- Fluent API: Extension methods provide clean, readable code
- Comprehensive: Covers common validation scenarios
- Type-Safe: Full generic type support
- Null-Safe: All methods handle null values gracefully
- Cross-Platform: Works with .NET Core, .NET 5/6/7/8+, and .NET Framework
- Flexible: Use static methods or extension methods based on your preference
- Well-Documented: XML documentation comments for IntelliSense support
Use Cases
- Form Validation: Validate user input in web forms and desktop applications
- Business Logic: Enforce business rules and constraints
- API Validation: Validate request parameters and data models
- Data Processing: Validate data before processing or storage
- Configuration Validation: Ensure configuration values meet requirements
📁 Project Structure
DotValidator/
├── Validators/
│ ├── DateTimeValidator.cs
│ ├── StringValidator.cs
│ ├── IntValidator.cs
│ ├── CollectionValidator.cs
│ ├── DictionaryValidator.cs
│ └── EmailValidator.cs
├── Extensions/
│ ├── DateTimeExtension.cs
│ ├── StringExtension.cs
│ ├── IntExtension.cs
│ ├── CollectionExtension.cs
│ ├── DictionaryExtension.cs
│ └── EmailExtension.cs
├── DotValidator.csproj
└── README.md
⚡ Future Plans
- Guard/Ensure Methods: Automatic exception throwing for failed validations
- ASP.NET Core Integration: Model validation attributes for seamless MVC integration
- Additional Validators: Support for
decimal,double,float,Guid,Uri, and more - Custom Validators: Framework for creating custom validation rules
- Validation Results: Rich validation result objects with error messages
- Async Support: Async validation methods for I/O operations
📝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
🔗 License
MIT License © KhanbariDev
📞 Support
For issues, questions, or feature requests, please open an issue on the GitHub repository.
| Product | Versions 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 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. |
-
net8.0
- No dependencies.
-
net9.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.