Validator
🎸 Dependency free fancy validator for objects.
Installation
npm install @handy-tools/validator@latest
Usage
import { Validator } from '@handy-tools/validator';
const scheme = new Validator({
name: 'string',
age: 'number',
emails: Validator.nested({
gmailEmail: {
type: 'email',
required: true,
errorMessage: 'Gmail email is required',
validate: (value) => {
return value?.includes('@gmail.com');
}
},
optionalEmail: {
type: 'email',
required: false,
},
}),
extra: Validator.nested({
ip: 'ipv4',
ipv6: 'ipv6',
})
});
const res = scheme.validate({
name: 'John',
age: 20,
emails: {
gmailEmail: 'some@gmail.com',
optionalEmail: '',
},
extra: {
ip: '192.168.0.34',
ipv6: '2001:0db8:85a3:0000:0000:8a2e:0370:7334',
}
});
const ipScheme = new Validator({
ip: 'ipv4',
});
const res = ipScheme.validate({
ip: 'not an ip',
});
See tests for more examples.