The library provide set of methods to validate data with ability to write your own validators. It can be used anywhere: backend, frontend - no matter.
The purpose of this project is to understand how to work with OOP in JavaScript
You can find more information here
To start work with library you need to create a validator
:
const validator = new Validator();
const schema = schema.string();
schema.isValid('string'); // true
Every validator has isValid
method
Provide 3 methods to validate strings:
string
- creates a schemarequired
contains
const schema = validator.string();
schema.isValid(''); // true
schema.required();
schema.isValid('what does the fox say'); // true
schema.isValid('hexlet'); // true
schema.isValid(null); // false
schema.isValid(''); // false
schema.contains('what').isValid('what does the fox say'); // true
schema.contains('whatthe').isValid('what does the fox say'); // false
Provide 4 methods to validate strings:
number
- creates a schemarequired
positive
range
const schema = validator.number();
schema.isValid(null); // true
schema.required();
schema.isValid(null); // false
schema.isValid(7); // true
schema.positive().isValid(10); // true
schema.range(-5, 5);
schema.isValid(-3); // false
schema.isValid(5); // true
Provide 3 methods to validate strings:
array
- creates a schemarequired
sizeof
const schema = validator.array();
schema.isValid(null); // false
const schema = schema.required();
schema.isValid([]); // true
schema.isValid(['hexlet']); // true
schema.sizeof(2);
schema.isValid(['hexlet']); // false
schema.isValid(['hexlet', 'code-basics']); // true
Provide 2 methods to validate strings:
object
- creates a schemashape
- allows to set nested schemas for every object property
const schema = validator.object();
schema.shape({
name: validator.string().required(),
age: validator.number().positive(),
});
schema.isValid({ name: 'kolya', age: 100 }); // true
schema.isValid({ name: 'maya', age: null }); // true
schema.isValid({ name: '', age: null }); // false
schema.isValid({ name: 'ada', age: -5 }); // false
You can write your own validators for any schema using addValidator
method
const validator = new Validator();
const validateStartsWith = (value, start) => value.startsWith(start);
validator.addValidator('string', 'startWith', validateStartsWith);
const schema = validator.string().test('startWith', 'H');
schema.isValid('exlet'); // false
schema.isValid('Hexlet'); // true
const validateMin = (value, min) => value >= min;
validator.addValidator('number', 'min', validateMin);
const schema = validator.number().test('min', 5);
schema.isValid(4); // false
schema.isValid(6); // true