FluentValidationDynamicRules 0.0.1
dotnet add package FluentValidationDynamicRules --version 0.0.1
NuGet\Install-Package FluentValidationDynamicRules -Version 0.0.1
<PackageReference Include="FluentValidationDynamicRules" Version="0.0.1" />
paket add FluentValidationDynamicRules --version 0.0.1
#r "nuget: FluentValidationDynamicRules, 0.0.1"
// Install FluentValidationDynamicRules as a Cake Addin #addin nuget:?package=FluentValidationDynamicRules&version=0.0.1 // Install FluentValidationDynamicRules as a Cake Tool #tool nuget:?package=FluentValidationDynamicRules&version=0.0.1
Creating your first validator
To define rules for an object, you can implement your own parser to parse from any serialized rule definition to a set of ValidatedProperty
. Or you can use the xml based provided one in this package.
In this documentation, I tried to copy the same structure as FluentValidation
to make it easy for the ready to find what need to be added to make the validation works as expected.
For example, imagine that you have a Customer class:
public class Customer
{
public int Id { get; set; }
public string Surname { get; set; }
public string Forename { get; set; }
public decimal Discount { get; set; }
public string Address { get; set; }
}
Instead of inheriting from AbstractValidator<Customer>
you will inherit from AbstractDynamicValidator<Customer>
, which has a default constructor that takes ValidattionBuilder
instance, and pass it down to its base class.
using FluentValidation;
public class CustomerValidator : AbstractDynamicValidator<Customer>
{
public CustomerValidator(ValidationBuilder<Customer> builder) : base(builder) { }
}
The validation rules themselves should be defined in any xml
string.
Then you pass the xml
rules to a RuleParser
.Parse()
method and get back an instance of ValidationBuilder<T>
.
Here is an example for the same Customer
class
<rules>
<rule-for prop="firstName">
<not-empty message="Please specify a first name" />
</rule-for>
<rule-for prop="address">
<string-len min="20" max="250" />
</rule-for>
<rule-for prop="discount">
<not-equal value="0"/>
</rule-for>
<rule-for prop="postalCode">
<must-be call="BeValidPostalCode" message="Please specify a valid postcode" />
</rule-for>
</rules>
var xml = "";//Get Customer xml rules
var parser = new RuleParser();
var builder = parser.Parse(xml);
var customerValidator = new CustomerValidator(builder);
From now on, everything is handled by FluentValidation
as is.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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 was computed. 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 was computed. 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. |
-
net6.0
- FastExpressionCompiler (>= 3.2.1)
- FluentValidation (>= 10.3.4)
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.0.1 | 3,438 | 11/15/2021 |