Create own validate
You can also create your own validate
with predefined validators
and locales
. Which is recommended way if you need to use and reuse custom validators.
Import
js
import create from '@hiperf/validate/create';
Example
Create
js
// validate.js
import create from '@hiperf/validate/create';
import isNumber from '@hiperf/validate/isNumber';
const validators = {
isNumber,
isCatMessage: v => /🐈|😺|😸|😻|😽/.test(v)
};
const locales = {
en: {
'isNumber': 'Value should be a number',
'isCatMessage': 'Message should contain cat emoji! 😾'
}
};
export default create({ validators, locales });
Use
js
// app.js
import validate from './validate';
const schema = {
year: { isNumber: true }
};
const data = { year: 2000 };
const { isValid, errors } = validate(schema, data);
// isValid = true
// errors = []