@ezier/validate
v0.2.3
Published
An ezier validator for nodejs.
Maintainers
Readme
Ezier Validator
Why?
This validator allows you to validate your objects with eze. Nothing more, nothing less.
Also used in the Fronvo server
Installing
npm i @ezier/validateDocumentation
Documentation for the Ezier Validator can be found at https://ezier-project.github.io/validate/.
Examples
Validate a string's length:
import { StringSchema } from '@ezier/validate';
const schema = new StringSchema({
value: {
minLength: 5,
maxLength: 20
}
});
const result = schema.validate({
value: 'some string'
});Validating manually with regex:
import { v4 } from 'uuid';
const uuidSchema = new StringSchema({
uuid: {
regex: /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/
},
});
uuidSchema.validate({ uuid: v4() });Or with provided types:
import { v4 } from 'uuid';
const uuidSchema = new StringSchema({
uuid: {
type: 'uuid'
},
});
uuidSchema.validate({ uuid: v4() });With support for optional fields:
const schema = new StringSchema({
provideThis: {
minLength: 8,
maxLength: 60,
type: 'email',
},
orThis: {
minLength: 8,
maxLength: 30,
regex: /^[a-zA-Z0-9]+$/,
optional: true
},
});
schema.validate({
provideThis: '[email protected]'
});Getting error info:
for (const errorObjectIndex in result) {
const errorObject = result[Number(errorObjectIndex)];
console.log(`[${errorObject.name}]: ${errorObject.message}`);
}More examples located here
Made by Shadofer with joy.
