extend method of
sanitizer let you add your own custom rules.
extend method accepts a total of two arguments. First is the rule name and second is the validation definition.
Whether or not the
validate method is asynchronous. This helps indicative optimize the runtime behavior of the validation cycle.
compile method receives the arguments passed to the rule and it can transform those args in any way and return them back. For example:
compile method can validate the passed arguments as follows:
validate method performs the actual validation on a given field.
Let's understand the
validate method line by line.
indicative-utilsreturns the value of a
dataobject. We recommend using this method, since it will handle the use cases for nested values, unknown array indexes and so on.
skippablemethod tells whether we should skip validation for this field or not. The validation must be skipped when it's non-existing. Only the
requiredrule is meant for validating
- Finally, we write some dummy code to make a database query.
trueto mark the validation as passed and
falseto mark it as failed.
extend method from the sanitizer will extend the sanitization rules.
compile method follows the same principles from the
sanitize method must mutate the fieldValue with the sanitized value.
sanitize method also receives the same arguments as the
After sanitizing the field value, you must use the
patchValue method to set the new value.
When using Indicative in a typescript project, you will also have to extend the interface that contains the list of validations and sanitizations.
In order for the
unique rule to show up in the list of
validations, you will have to extend the interface.
Create a separate file
types.ts in your project directory and write the following code inside it.
Typescript declaration merging will merge the two interfaces with same name
In order to extend the sanitizations list, you need to write the following code