Validation library inspired from joi library and laravel validator, to make easy validation in vanilla.js
var validator = KValidation.schema(rules).validate(value)
KValidation.schema(rules).validate(value, function(result) {
// return
})
- status
[HTTP STATUS CODE]
sign the function execution
- message
[STRING]
show message according to status
- data
[OBJECT|NULL]
return null if there was no errors validation
{
"id": "required",
"title": "required|min-6"
}
- string
- number
- required
- min-{value}
// min-6
- max-{value}
// max-12
- phone
- RegExp
// regex-(/[^0-9]/g)
see notes for detail - sameas-{id}
// sameas-id
- for using regex rules, you must escape some special characters because rules writed as string
for example
regex-(/\d/g)
will returnregex-(/d/g)
if you not escape the special characters, so you had changes like thisregex(/\\d/g)
and it will returnregex-(/\d/g)
{
"id": "{value}"
}
{
"id": {
"value": "value",
"field": "field name",
"rules": {
"required": "custom message"
}
}
}
value
: Value want validate
field
: Re-assign return field name if you called in custom message
rules
: Object with keys have a same name with validation rulesnotes: to call field name in custom message, follow example below
{ "id": { "value": "value", "field": "User ID", // if you not passing it with default use object key as field name "rules": { "required": "field {{field}} can't be empty!" // will return field User ID can't be empty! } } }
The installation instructions are low priority in the readme and should come at the bottom. The first part answers all their objections and now that they want to use it, show them how.