-
Notifications
You must be signed in to change notification settings - Fork 0
Validator
Run() is the single member of the Validator
static class.
Validator.Run(Func<Type, bool>? customIgnoreFunc=null, Func<Type, bool>? approvedDataFunc=null)
Iterate over all types in the assembly and throw exceptions if they break F
rules.
Validator.Run will run in Debug mode only. Also while highly recommended, invoking Validator.Run is optional.
Inevitably some framework classes have to be used which break F
rules (mutable classes ie razor pages), in these cases mark those classes with the [FIgnore]
for Validator.Run
to skip them when testing.
For classes inheriting DbContext
it is best to manually turn them from classes to records.
customIgnoreFunc
if given will force Validator.Run
treat all types for which it returns true
as if they were marked with [FIgnore]
.
In the below sample we ignore compiler generated Razor types and Page model types as their code is not available to be manually marked with [FIgnore].
var razorCompiledTypes = Assembly.GetExecutingAssembly().CustomAttributes
.Where(customAttributeData => customAttributeData.AttributeType.Name == "RazorCompiledItemAttribute")
.Select(customAttributeData => customAttributeData.ConstructorArguments)
.Where(constructorArguments => constructorArguments.Any())
.Where(constructorArguments => constructorArguments[0].Value is Type)
.Select(constructorArguments => (constructorArguments[0].Value as Type)!);
bool razorCompilerGenerated(Type type) => razorCompiledTypes.Contains(type);
bool inheritsPageModel(Type type) => typeof(Microsoft.AspNetCore.Mvc.RazorPages.PageModel).IsAssignableFrom(type);
F.Validator.Run(t => razorCompilerGenerated(t) || inheritsPageModel(t));
approvedDataFunc
if given will force Validator.Run
to treat all types for which it returns true
as if they were Data (and so are accepted types for Fields etc). Use with caution, it is always better to wrap a type with a record that is F Data compliant (immutable with value semantics).