A versatile and easy-to-use JavaScript validation library, JS Validator ensures the integrity of your data inputs with a range of customization options. Ideal for projects that require detailed, flexible validation rules, from simple forms to complex data structures.
- Supports a wide range of data types (string, number, boolean).
- Customizable validation rules (required, min/max length, regex matching, etc.).
- Asynchronous and custom validation functions.
- Easy error message customization.
- Options for strict mode validation.
- Methods for retrieving passed fields and current errors.
- Fully documented and TypeScript friendly.
npm install [your-package-name]
Below is a quick example of how to use the JS Validator:
const Validator = require('[your-package-name]');
const rules = {
email: { type: 'string', required: true, validate: 'email' },
age: { type: 'number', min: 18 },
};
const messages = {
email: { required: 'Email is required', validate: 'Invalid email' },
age: { min: 'Age must be at least 18' },
};
const validator = new Validator(rules, messages);
const data = { email: '[email protected]', age: 20 };
validator.validate(data).then(isValid => {
if (isValid) {
console.log('Validation successful');
} else {
console.log('Validation failed', validator.getErrors());
}
});
rules
: Object containing validation rules.messages
: Corresponding error messages for each rule.options
: Optional settings like strict mode.
validate(input)
: Validates the input against the rules.getErrors()
: Returns validation errors.getPassedFields()
: Returns fields that passed validation.reset()
: Resets the validation state.
This project is licensed under the MIT License.