FluentValidation.DynamicRules
Add support to dynamically loading validation rules from xml based rule definition
Install / Use
/learn @mustafamagdy/FluentValidation.DynamicRulesREADME
Dynamic Rules for FluentValidation
Add support to dynamically loading validation rules from xml based rule definition
Setup
public class CustomerValidator : AbstractDynamicValidator<Customer> {
public CustomerValidator(ValidationBuilder builder) : base(builder) { }
private bool BeValidPostalCode(string postalCode) {
// custom postcode validating logic goes here
}
}
Construct the builder
// Some where in your setup
var parser = new RuleParser();
var builder = parser.Parse(xml);
var customerValidator = new CustomerValidator(builder);
Sample XML rules
<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>
This is equivalent to:
public class CustomerValidator : AbstractValidator<Customer> {
public CustomerValidator() {
RuleFor(x => x.Surname).NotEmpty();
RuleFor(x => x.Forename).NotEmpty().WithMessage("Please specify a first name");
RuleFor(x => x.Discount).NotEqual(0);
RuleFor(x => x.Address).Length(20, 250);
RuleFor(x => x.Postcode).Must(BeAValidPostcode).WithMessage("Please specify a valid postcode");
}
private bool BeAValidPostcode(string postcode) {
// custom postcode validating logic goes here
}
}
Related Skills
openhue
349.0kControl Philips Hue lights and scenes via the OpenHue CLI.
sag
349.0kElevenLabs text-to-speech with mac-style say UX.
weather
349.0kGet current weather and forecasts via wttr.in or Open-Meteo
tweakcc
1.6kCustomize Claude Code's system prompts, create custom toolsets, input pattern highlighters, themes/thinking verbs/spinners, customize input box & user message styling, support AGENTS.md, unlock private/unreleased features, and much more. Supports both native/npm installs on all platforms.
