RuleMaster 1.0.1
dotnet add package RuleMaster --version 1.0.1
NuGet\Install-Package RuleMaster -Version 1.0.1
<PackageReference Include="RuleMaster" Version="1.0.1" />
<PackageVersion Include="RuleMaster" Version="1.0.1" />
<PackageReference Include="RuleMaster" />
paket add RuleMaster --version 1.0.1
#r "nuget: RuleMaster, 1.0.1"
#:package RuleMaster@1.0.1
#addin nuget:?package=RuleMaster&version=1.0.1
#tool nuget:?package=RuleMaster&version=1.0.1
RuleMaster
A powerful, attribute-based validation library for .NET that provides comprehensive rule enforcement with clear error messages and fluent API support.
Features
- Attribute-based validation: Decorate your properties with validation attributes
- Comprehensive rule set: Built-in rules for strings, numbers, dates, collections, and more
- Clear error messages: Detailed validation exceptions with property names
- Fluent API support: Works seamlessly with existing validation frameworks
- Performance optimized: Minimal overhead with efficient validation logic
- .NET 10.0 support: Built on the latest .NET platform
Installation
dotnet add package RuleMaster
Quick Start
using RuleMaster;
public class User
{
[RequiredRule]
[NotEmptyRule]
[MinLengthRule(3)]
public string Name { get; set; }
[RequiredRule]
[EmailRule]
public string Email { get; set; }
[MinValueRule(18)]
[MaxValueRule(120)]
public int Age { get; set; }
[RequiredRule]
[IsInFutureRule]
public DateTime BirthDate { get; set; }
}
// Validate your entities
var user = new User { Name = "John", Email = "john@example.com", Age = 25, BirthDate = DateTime.Now.AddDays(1) };
try
{
RuleEnforcer.EnforceRules(user);
Console.WriteLine("Validation passed!");
}
catch (AggregateRuleValidationException ex)
{
foreach (var error in ex.Errors)
{
Console.WriteLine($"Error: {error.Message}");
}
}
Available Rules
String Rules
RequiredRule- Validates that the property is not nullNotEmptyRule- Validates that the string is not null or emptyMinLengthRule(int minLength)- Validates minimum string lengthMaxLengthRule(int maxLength)- Validates maximum string lengthLengthRule(int exactLength)- Validates exact string lengthExactLengthRule(int exactLength)- Validates exact string lengthIsUpperCaseRule- Validates that string is uppercaseIsLowerCaseRule- Validates that string is lowercaseIsNumericStringRule- Validates that string contains only numbersNoSpecialCharactersRule- Validates that string contains only alphanumeric charactersNotWhitespaceRule- Validates that string is not just whitespaceStartsWithRule(string prefix)- Validates that string starts with prefixEndsWithRule(string suffix)- Validates that string ends with suffixContainsRule(string substring)- Validates that string contains substringAllowedValuesRule(params string[] allowedValues)- Validates against allowed valuesRegularExpressionRule(string pattern)- Validates with custom regex pattern
Number Rules
NotZeroRule- Validates that number is not zeroNotNegativeRule- Validates that number is not negativeNotNaNRule- Validates that double is not NaNMinValueRule(double minValue)- Validates minimum valueMaxValueRule(double maxValue)- Validates maximum valueIsPositiveRule- Validates that number is positiveIsNegativeRule- Validates that number is negativeIsBetweenRule(double min, double max)- Validates that number is in range
Date Rules
MinDateRule(DateTime minDate)- Validates minimum dateMaxDateRule(DateTime maxDate)- Validates maximum dateIsInPastRule- Validates that date is in the pastIsInFutureRule- Validates that date is in the futureIsBeforeRule(DateTime date)- Validates that date is before specified dateIsAfterRule(DateTime date)- Validates that date is after specified dateIsBetweenDatesRule(DateTime min, DateTime max)- Validates that date is in rangeIsNotWeekendRule- Validates that date is not a weekendAgeAtLeastRule(int years)- Validates that date represents minimum age
Pattern Rules
EmailRule- Validates email formatPhoneNumberRule- Validates phone number in E.164 formatUrlRule- Validates URL format
Collection Rules
NotEmptyRule- Validates that collection is not emptyMinCountRule(int minCount)- Validates minimum collection countMaxCountRule(int maxCount)- Validates maximum collection countNoDuplicatesRule- Validates that collection has no duplicates
Common Rules
RequiredRule- Validates that property is not nullNotDefaultRule- Validates that property is not default valueRequiredIfRule(string propertyName, object value)- Validates conditionallyDefinedEnumValueRule- Validates that enum value is defined
Comparison Rules
EqualToRule(object value)- Validates equalityNotEqualToRule(object value)- Validates inequalityIsGreaterThanRule(object value)- Validates greater thanIsLessThanRule(object value)- Validates less than
Custom Rules
Create custom validation rules by inheriting from PropertyRule:
public class CustomRule : PropertyRule
{
public override void Validate(string propertyName, object? value)
{
if (value == null)
{
throw new RuleValidationException($"Property '{propertyName}' cannot be null.", propertyName);
}
// Your validation logic here
if (!IsValid(value))
{
throw new RuleValidationException($"Property '{propertyName}' failed custom validation.", propertyName);
}
}
private bool IsValid(object value)
{
// Implement your validation logic
return true;
}
}
Exception Handling
RuleMaster provides two main exception types:
RuleValidationException- Thrown when a single rule failsAggregateRuleValidationException- Contains multiple validation errors
Performance
RuleMaster is optimized for performance with:
- Minimal reflection overhead
- Efficient validation logic
- Support for validation caching
- No external dependencies for core functionality
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
If you have any issues or questions, please file an issue on our GitHub repository.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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. |
-
net10.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.