2.2.4 • Published 4 years ago

korrekt v2.2.4

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

korrekt

Asynchronous validation library.

Build Status Coverage Status

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;myself@world.com', 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

RuleDescription
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

2.2.4

4 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

6 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago