Using older version of Indicative or @adonisjs/validator?. Click here to access the old version documentation.

guides

extend

The extend method of validator and sanitizer let you add your own custom rules.

validator.extend

The validator extend method accepts a total of two arguments. First is the rule name and second is the validation definition.

async: (boolean)

Whether or not the validate method is asynchronous. This helps indicative optimize the runtime behavior of the validation cycle.

compile

The optional compile method receives the arguments passed to the rule and it can transform those args in any way and return them back. For example:

And the compile method can validate the passed arguments as follows:

validate

The validate method performs the actual validation on a given field.

Let's understand the validate method line by line.

  1. The getValue method from indicative-utils returns the value of a field from the data object. We recommend using this method, since it will handle the use cases for nested values, unknown array indexes and so on.
  2. The skippable method tells whether we should skip validation for this field or not. The validation must be skipped when it's non-existing. Only the required rule is meant for validating undefined value.
  3. Finally, we write some dummy code to make a database query.
  4. The validate must return true to mark the validation as passed and false to mark it as failed.

sanitizer.extend

The extend method from the sanitizer will extend the sanitization rules.

compile

The compile method follows the same principles from the validator.extend method.

sanitize

The sanitize method must mutate the fieldValue with the sanitized value.

The sanitize method also receives the same arguments as the validate.extend method.

After sanitizing the field value, you must use the patchValue method to set the new value.

Extending interfaces

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