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.
Say 👋 to Indicative!
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.
Install the package from the npm registry as follows:
Following is the simplest example of using Indicative to validate a new user signup data object.
rules object (known as schema) is defined as an object of key-value pairs.
keyis the field path you want to validate.
valueis an array of multiple validation rules. You can define them as a string separated by
pipe (|)operator or an array of functions calls.
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:
- Or define them in combination of field + rule, ie
username.required. The latter one gets priority over the former.
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.