This repository was archived by the owner on Jan 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Home
Ernest edited this page Apr 19, 2020
·
52 revisions
The most customizable verification framework for JavaScript.
Intuitive APIs. 🎯
Asynchronous Rules Support.
Type Annotation Support. 💡
Full-custom Message Support.
Written in TypeScript. 💪
Self Sufficient.
// custom rules
const minlength = length => ({ value }) => {
if (value.length < length) {
return `Must be at least ${length} characters long.`
}
}
const form = {
password: '123'
}
const schema = VerifyJs.createSchema({
password: {
$rule: minlength(6)
}
})
schema.$verify(form, () => {
console.log(schema.$hasMessage)
console.log(schema.$messages)
})
// > true
// > ['Must be at least 6 characters long.']
Even though the required
is a very common rule method, sometimes you still need to re-write it.
For example, if you want to introduce internationalization:
const required = ({ value, params }) => {
const isError = (
(value === void 0) ||
(value === null) ||
(value === '') ||
(Object.prototype.toString.call(value) === '[object Object]' && Object.keys(value).length === 0) ||
(Object.prototype.toString.call(value) === '[object Array]' && value.length === 0)
)
if (isError) {
const language = params.language || currentLanguage
return translate(`This field is required.`, { language })
}
}
const form = {
password: '123',
}
const schema = VerifyJs.createSchema({
password: {
$params: {
language: 'en-US'
},
$rule: required
}
})