async-validator
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
The async-validator is a powerful JavaScript library that allows asynchronous form validation. It can handle a wide variety of validation checks such as string, number, boolean, regular expressions, object, array, date, URL, email, and hex, amongst others. Additionally, it offers type checking, required field checking, pattern matching, ranges, lengths, enumerables, deep rules, and message customization, making it an all-in-one solution for validating form fields in an asynchronous way.
To use async-validator, you need to define a descriptor, assign it to a schema, and pass the object to be validated and a callback function to the 'validate' method of the schema. Here's an example of how to use it:
import Schema from 'async-validator';
const descriptor = {
name: {
type: 'string',
required: true,
validator: (rule, value) => value === 'muji',
},
age: {
type: 'number',
asyncValidator: (rule, value) => {
return new Promise((resolve, reject) => {
if (value < 18) {
reject('too young'); // reject with error message
} else {
resolve();
}
});
},
},
};
const validator = new Schema(descriptor);
validator.validate({ name: 'muji', age: 16 }).then(() => {
// validation passed or without error message
}).catch(({ errors, fields }) => {
return handleErrors(errors, fields);
});
In this code, a schema is created with two fields (name and age) which need to be validated. The name field must be a string and its value must be 'muji'. The age field must be a number and it cannot be less than 18.
The detailed documentation for async-validator containing all its usage details, API reference, rules, and FAQ can be found directly on its GitHub page where you can explore the repository's readme file for a comprehensive understanding of its features, installation, usage, faq, test cases, and license details. For particularities about the more advanced usage variants and behaviors, you should refer to the examples and explanations provided in the readme file.