Myce.FluentValidator
1.2.4
dotnet add package Myce.FluentValidator --version 1.2.4
NuGet\Install-Package Myce.FluentValidator -Version 1.2.4
<PackageReference Include="Myce.FluentValidator" Version="1.2.4" />
<PackageVersion Include="Myce.FluentValidator" Version="1.2.4" />
<PackageReference Include="Myce.FluentValidator" />
paket add Myce.FluentValidator --version 1.2.4
#r "nuget: Myce.FluentValidator, 1.2.4"
#:package Myce.FluentValidator@1.2.4
#addin nuget:?package=Myce.FluentValidator&version=1.2.4
#tool nuget:?package=Myce.FluentValidator&version=1.2.4
MYCE.FluentValidator
MYCE (Makes Your Coding Easier) FluentValidator is a fluent validation library designed to simplify entity validation in .NET applications.
Supports net6.0, net7.0, net8.0, net9.0, and netstandard2.0.
Installation
Package Manager Console:
Install-Package Myce.FluentValidator
Features
- FluentValidator: Fluent validation class for entities
- ValidatorBuilder: Orchestrates multiple validations in a single process
- High Performance: Optimized with typed value access to avoid boxing/unboxing.
- Reusable Templates: Define rules once and apply them to multiple DTOs.
- External Validation: Validate standalone variables or external states with RuleForValue.
Usage
1. Create your Entity
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Email { get; set; }
public string Code { get; set; }
}
2. Define Validation Rules
Use the FluentValidator to define rules for your entity properties.
var person = new Person { Name = "John Doe", Age = 30, Email = "john.doe@example.com", Code = "A123" };
var validator = new FluentValidator<Person>();
.RuleFor(x => x.Name)
.IsRequired()
.MinLength(3)
.MaxLength(50)
.RuleFor(x => x.Age)
.IsGreaterThanOrEqualTo(18)
.IsLessThanOrEqualTo(100)
.RuleFor(x => x.Email)
.IsRequired()
.IsValidEmailAddress()
.RuleFor(x => x.Code)
.ExactNumberOfCharacters(4)
.ContainsOnlyNumber();
var result = validator.Validate(person);
if (validator.Messages.Any())
{
foreach (var message in validator.Messages)
{
Console.WriteLine(message.Text);
}
}
3. Reusable Templates (DRY Principle)
You can define validation logic for common fields (like Email) and reuse them across different classes.
public static class SharedRules
{
public static void NameTemplate<T>(RuleBuilder<T, string> rb) where T : class
=> rb.IsRequired().MinLength(3).MaxLength(100);
}
// In your validator:
validator.RuleFor(x => x.FirstName).ApplyTemplate(SharedRules.NameTemplate);
validator.RuleFor(x => x.LastName).ApplyTemplate(SharedRules.NameTemplate);
4. External Value Validation
Use RuleForValue to validate data that isn't a property of your main entity, such as checking if a record already exists in the database.
bool emailAlreadyExists = service.CheckEmail(request.Email);
var validator = new FluentValidator<Person>()
.RuleForValue(emailAlreadyExists, "Email Uniqueness")
.IsFalse(new ErrorMessage("This email is already taken"));
Supported Validators
Core validators available:
| Validator | Description |
| :--- | :--- |
| Custom | Allows you to define a custom validation function. |
| IsNull |Validates if the property value is null. |
| IsNotNull |Validates if the property value is not null. |
| IsRequired | Validates that the property is not null or empty. |
| IsRequiredIf | Validates that the property is required if a condition is true. |
| IsTrue | Validates that the boolean attribute is true.|
| IsFalse | Validates that the boolean attribute is false.|
Collection validators:
| Validator | Description |
| :--- | :--- |
| IsEmpty | Validates that the collection is empty (contains no items). |
| IsIn | Validates that the attribute value is present within a sequence of allowed valeus. |
| HasItems | Validates that the collection is not empty and has at least one item. |
| MaxNumberOfItems | Validates that the collection does not exceed a maximum number of items. |
Comparison validators:
| Validator | Description |
| :--- | :--- |
| IsEqualTo | Validates equality to a specific value. |
| IsNotEqualTo | Validates if the property value is not equal to a fixed value. |
Numeric validators:
| Validator | Description |
| :--- | :--- |
| IsBetween | Validates that the value is between two specified values. |
| IsGreaterThan | Validates that the value is greater than a specified value. |
| IsGreaterThanOrEqualTo | Validates that the value is greater than or equal to a specified value. |
| IsLessThan | Validates that the value is less than a specified value. |
| IsLessThanOrEqualTo | Validates that the value is less than or equal to a specified value. |
| IsPositive | Validates that the integer value is positive (greater than zero). |
| IsNegative | Validates that the integer value is negative (less than zero). |
String validators:
| Validator | Description |
| :--- | :--- |
| Contains | Validates that the string contains a specific substring. |
| ContainsOnlyNumber | Validates that a string contains only numeric characters. |
| ExactNumberOfCharacters | Validates that a string has an exact length. |
| ExactNumberOfCharactersIf | Validates exact length if a condition is true. |
| IsValidDate | Validates that a string can be parsed as a valid date. |
| IsValidEmailAddress | Validates that a string is a valid email format. |
| MaxLength | Validates the maximum length of a string. |
| MaxLengthIf | Validates maximum length if a condition is true. |
| MinLength | Validates the minimum length of a string. |
| MinLengthIf | Validates minimum length if a condition is true. |
Notes
Version 1.2.4
- Add Custom validator to allow users to define their own validation logic with a custom function.
Version 1.2.3
- Add extension methods to validate collection and enumerable attributes.
- Added
IsPositiveandIsNegativevalidation rule for all numeric types (int,double,decimal) including nullable support.
Version 1.2.2
- Added
IsBetweenvalidation rule for all numeric types (int,double,decimal) including nullable support- - Fixed fluent chaining for RuleForValue, allowing multiple external value validations in a single statement.
Version 1.2.1
- Internal engine updated to use
GetAttributeValue<T>to eliminate boxing of primitive types and improve performance.
Version 1.2.0
- Added
RuleForValuefor external variable validation andApplyTemplatefor rule reuse.
Version 1.1.1
- Addedd new validators:
IsNotNull, andIsNull.
Version 1.1.0
- Introduces multi-targeting support (
net6.0,net7.0,net8.0,net9.0, andnetstandard2.0) and full nullability support.
Version 1.0.0
- The initial stable release of Myce.FluentValidator, providing basic validation capabilities for .NET applications.
Dependencies
- Myce.Extensions
- Myce.Response
Contributions
Contributions are welcome! If you have a validation method you think is useful and can make life easier for other developers, please create a Pull Request and submit it. Attention: All submitted methods must include a unit test.
Developed by Fernando Prass
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Myce.Extensions (>= 1.0.2)
- Myce.Response (>= 1.0.4)
-
net6.0
- Myce.Extensions (>= 1.0.2)
- Myce.Response (>= 1.0.4)
-
net7.0
- Myce.Extensions (>= 1.0.2)
- Myce.Response (>= 1.0.4)
-
net8.0
- Myce.Extensions (>= 1.0.2)
- Myce.Response (>= 1.0.4)
-
net9.0
- Myce.Extensions (>= 1.0.2)
- Myce.Response (>= 1.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.