korrekt
v2.2.4
Published
Async validation
Downloads
660
Maintainers
Readme
korrekt
Asynchronous validation library.
TOC
Installation
npm i korrekt --save
Usage
Out of the box
const v = require('korrekt')
const validator = v.create({
name: v.length({ min: 3 }),
email: v.email(),
skype: v.all(
v.length({ min: 3 }),
v.match(/\w+/),
),
phone: v.when(it => !it.skype, v.all(
v.match(/\d+/),
v.length({ min: 9, max: 9 }),
))
})
validator({ name: 'me', email: 'me;[email protected]', skype: 'abba' })
.then(validatedObject => console.log(validatedObject))
.catch(v.ValidationError, error => console.error(error.result))
// { name: { message: 'must be longer', meta: { min: 3 } }, email: { message: 'must be an email' } }
Custom rules
const v = require('korrekt')
v.register('same', function (field) {
return function (value, _, instance) {
if (instance[field] != value) {
return `must be same as ${field}`
}
}
})
const validator = v.create({
name: v.length({ min: 3, max: 10 }),
password: v.length({ min: 3, max: 40 }),
password_confirmation: v.same('password'),
})
validator({ name: 'me123456789', password: '1', password_confirmation: '2' })
.then(validatedObject => console.log(validatedObject))
.catch(v.ValidationError, error => console.error(error.fields))
// { name: { message: 'must be shorter', meta: { max: 10 } }, password: { message: 'must be longer', meta: { min: 3 } }, password_confirmation: { message: 'must be same as password' } }
Reference
List of methods
create(rule)
Creates validator function.
register(name, rule, [overwrite])
Registers custom rule. Rule parameter here is actually the rule builder, accepting options and custom message as arguments. By default register
throws exception if rule with same name already exists, but you can specify true
as 3rd argument to explicitly overwrite existing rule.
List of rules
Rule | Description
--- | ---
required(optionalNestedRule) | Requires value to be present (not undefined or null). Executes optionalNestedRule
, if specified.
length({ min, max, exactly }) | Verifies value has length and it is between specified boundaries (if any).
integer({ min, max }) | Verifies value is an integer and it is between specified boundaries (if any).
number({ min, max }) | Verifies value is a number (integer or real) and it is between specified boundaries (if any).
string({ min, max, exactly }) | Verifies value is a string and it's length is between specified boundaries (if any).
match(regex) | Verifies value matches regex.
enum(option1, option2, ...) | Verifies value is equal to one of specified options.
email() | Verifies value is an email (has @ inside).
when(predicate: instance => boolean, rule) | Verifies value is valid according to rule, but verification is done only if predicate returns true.
all(rule1, rule2, ...) | Verifies value is valid according to each rule from rules array.
any(rule1, rile2, ...) | Verifies value is valid according to at least one rule from rules array.
array(rule, { min, max, exactly }) | Verifies value is an array and each item of it is valid according to rule. Also checks array length if at least one boundary is specified.
object({ name: rule }) | Verifies value is an object and checks whether its fields are valid according to rules.
function({ min, max, exactly }) | Verifies value is a function and checks whether its arity falls between specified boundaries (if any).
License
MIT