Getting started
Installation
Install @hiperf/validate
with your favorite package manager:
sh
npm i @hiperf/validate
sh
yarn add @hiperf/validate
sh
pnpm i @hiperf/validate
sh
bun add @hiperf/validate
Usage
js
import { validate } from '@hiperf/validate';
const schema = {
name: {
minLength: 3,
isString: true,
},
age: {
min: {
value: 18,
error: 'Minimal age is 18 y.o.' // Custom error message
},
isNumber: true
},
email: {
isEmail: true,
},
message: {
custom(v) {
const errors = [];
if (!/🐈|😺|😸|😻|😽/.test(v))
errors.push('Message should contain cat emoji! 😾');
return errors;
}
}
};
const data = {
name: 'John Doe, Jr.',
age: 15,
email: 'john.doe.jr@example.com',
message: '🐶 woof!'
};
const { isValid, errors } = validate(schema, data);
// isValid = false
// errors = [
// 'Minimal age is 18 y.o.',
// 'Message should contain cat emoji! 😾'
// ]
TIP
Both ESM
and CJS
modules is supported.
js
import { validate } from '@hiperf/validate';
js
const { validate } = require('@hiperf/validate');