So, you have a web server that accepts form requests. You want to validate and sanitize the data before your application can consume and store it in the database.

What is Indicative?

Indicative is a data validation library to validate complex data objects by defining a human readable schema. In a nutshell, you can

Validate nested objects and arrays. Define custom validation error messages. Use error formatters to define the shape of error messages. For example: JSON:API formatter to format error messages as per the JSON:API spec. Option to remove unvalidated properties from the data object. Support for async validations. Extensible API for adding custom rules. Data sanitizer.

Installation

Install the package from the npm registry as follows:

Basic example

Following is the simplest example of using Indicative to validate a new user signup data object.

The rules object (known as schema) is defined as an object of key-value pairs.

The key is the field path you want to validate.

is the field path you want to validate. The value is an array of multiple validation rules. You can define them as a string separated by pipe (|) operator or an array of functions calls.

Learn more about the Indicative validator

Defining custom messages

Indicative has first class support for defining custom messages for validation failures. You can define generic messages for validation rules or define a custom message for each field.

The messages object has the same syntax as the rules objects.

You can define messages just for the rules, ie: required , min , email and so on. Or define them in combination of field + rule, ie username.required . The latter one gets priority over the former.

Learn more about custom messages

Data sanitization

Validating data is one step to ensure data integrity. Data sanitization is equally important since you don't want to save malicious input to the database.

Similar to the validator schema definition, Indicative also allows you to define rules for cleaning up the user input.

Learn more about data sanitizer