Integrity Checks - smbc-digital/form-builder GitHub Wiki
Integrity checks are small units of code that validate features or components defined in the JSON DSL.
When a form is loaded, before it's cached (if caching is turned on), the integrity of the form is checked to ensure it's in a valid state.
Form Schema Integrity Validator
Validation happens in a IFormSchemaIntegrityValidator.Validate
this should be called from FormSchemaFactory
.
A list of IFormSchemaIntegrityCheck
is injected into the IFormSchemaIntegrityValidator
.
The default implementation of IFormSchemaIntegrityValidator
simply aggregates any failure messages and throws a new ApplicationException
containing all recorded failures.
Creating an Integrity Check
If a new component or element is being added to the DSL that contains rules custom logic an integrity check should be created to check these rules.
You can create an Integrity Check for the whole form if required, perhaps when corresponding/dependent elements are on different pages. Or if it's just a specific behaviour, or just need to validate a stand-a-lone element.
- IFormSchemaIntegrityCheck -> Form wide validation
- IBehaviourSchemaIntegrityCheck -> For validating a new behaviour of a page
- IElementSchemaIntegrityCheck -> For validating just the element
To create a new Integrity check you must implement the I{Form/Behaviour/Element}SchemaIntegrityCheck.Validate
methods.
eg.
public interface IFormSchemaIntegrityCheck
{
IntegrityCheckResult Validate(FormSchema schema);
Task<IntegrityCheckResult> ValidateAsync(FormSchema schema);
}
the Validate
methods return an IntegrityCheckResult
object, this contains an bool IsValid
flag and a list Messages
of reasons for failure.
Registering a new Integrity Check
Registering an integrity check using dependency injects as below.
services.AddSingleton<IFormSchemaIntegrityCheck, MyCustomIntegrityCheck>();
services.AddSingleton<IBehaviourSchemaIntegrityCheck, MyBehaviourCheck>();
services.AddSingleton<IElementSchemaIntegrityCheck, MyElementCheck>();
This can be done either directly in startup.cs
or preferrably in the convenience method AddSchemaIntegrityValidation
in /Utils/Startup/ ServiceCollectionsExtensions.cs