- Notifications
You must be signed in to change notification settings - Fork1
rtlnl/Rtl.Configuration.FluentValidation
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Rtl.Configuration.FluentValidation is based on packageRtl.Configuration.Validation and extends it by providing ability to useFluentValidation library for creating sophisticated validators not possible withDataAnnotations
Suppose there is config class:
publicclassMyConfiguration{[Required]publicstringName{get;set;}[Range(0,10)]publicintValue{get;set;}}
and validator:
usingFluentValidation;publicclassMyConfigurationValidator:AbstractValidator<MyConfiguration>{publicMyConfigurationValidator(){RuleFor(x=>x.Name).Length(2,5);RuleFor(x=>x.Value).Must(x=>x%2==0);}}
The config class is registered like this:
publicvoidConfigureServices(IServiceCollectionservices){services.AddConfig<MyConfiguration,MyConfigurationValidator>(Configuration,"ConfigSectionName");}
Now the config values are validated based onDataAnnotations
attributes and on rules inMyConfigurationValidator
publicstaticIServiceCollectionAddConfig<TConfig,TValidator>(thisIServiceCollectionservices,IConfigurationconfiguration,stringsectionName)whereTConfig:class,new()whereTValidator:AbstractValidator<TConfig>
Adds IOptions<T> to IoC container, validates config beforeStartup.Configure
is called usingDataAnnotations
attributes andTValidator
publicstaticTConfigGetConfig<TConfig,TValidator>(thisIConfigurationconfiguration,stringsectionName)whereTConfig:class,new()whereTValidator:AbstractValidator<TConfig>,new()
Gets config of typeT
from configuration, validates and returns it.Use this method when you don't need to addIOptions
and you want to get validated config insideConfigureServices
method and use right away
About
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.