Skip to the content.

Validation Rules

attributes

Checks object properties.

Errors:

Source

base64

Checks input to be a valid base64 string.

Specification: https://datatracker.ietf.org/doc/html/rfc5322#section-3.4.1

Note! The string rule is checked before running validation.

Errors:

Source

boolean

Checks input to be a boolean-like primitive.

Errors:

Source

Tests

Examples

boolean (positive)

import cottus from 'cottus';

const validator = cottus.compile("boolean");

validator.validate(true) //true
validator.validate(false) //false

string (positive)

import cottus from 'cottus';

const validator = cottus.compile("boolean");

validator.validate('true') //true
validator.validate('false') //false

number (positive)

import cottus from 'cottus';

const validator = cottus.compile("boolean");

validator.validate(1) //true
validator.validate(0) //false

empty value (positive)

import cottus from 'cottus';

const validator = cottus.compile("boolean");

validator.validate(null) //null
validator.validate(undefined) //undefined

cron

Checks string to be valid a cron string.

Note! The string rule is checked before running validation.

Errors:

Source

Tests

Examples

cron (positive)

import cottus from 'cottus';

const validator = cottus.compile("cron");

validator.validate('0 4 8-14 * *') //'0 4 8-14 * *'
validator.validate('0 0 1,15 * 3') //'0 0 1,15 * 3'
validator.validate('5 0 * 8 *') //'5 0 * 8 *'
validator.validate('23 0-20/2 * * *') //'23 0-20/2 * * *'
validator.validate('5-10 4 */3 * sun') //'5-10 4 */3 * sun'
validator.validate('0 0,5-12 1 */2 *') //'0 0,5-12 1 */2 *'

empty value (positive)

import cottus from 'cottus';

const validator = cottus.compile("cron");

validator.validate(null) //null
validator.validate(undefined) //undefined

date

Checks value to be a valid date.

Errors:

Source

Tests

Examples

numbers (positive)

import cottus from 'cottus';

const validator = cottus.compile("date");

validator.validate(1697837159631) //2023-10-20T21:25:59.631Z

string (positive)

import cottus from 'cottus';

const validator = cottus.compile("date");

validator.validate('04 Dec 1995 00:12:00 GMT') //1995-12-04T00:12:00.000Z
validator.validate('2022-10-20T19:33:38.118Z') //2022-10-20T19:33:38.118Z

empty value (positive)

import cottus from 'cottus';

const validator = cottus.compile("date");

validator.validate(null) //null
validator.validate(undefined) //undefined

default

Set default value.

Source

Tests

Examples

set default value (positive)

import cottus from 'cottus';

const validator = cottus.compile({"default":100});

validator.validate(null) //100
validator.validate(undefined) //100

skip if value is already set (positive)

import cottus from 'cottus';

const validator = cottus.compile({"default":100});

validator.validate(false) //false
validator.validate(NaN) //NaN
validator.validate(0) //0
validator.validate('') //''

email

Checks input to be a valid email address.

Specification: https://datatracker.ietf.org/doc/html/rfc5322#section-3.4.1

Note! The string rule is checked before running validation.

Errors:

Source

Tests

Examples

email (positive)

import cottus from 'cottus';

const validator = cottus.compile("email");

validator.validate('mo@soto.pr') //'mo@soto.pr'
validator.validate('qwe+caf_=4evnlldiziyrfbozh7wgx5k3x=ololo.domain.com@test.com') //'qwe+caf_=4evnlldiziyrfbozh7wgx5k3x=ololo.domain.com@test.com'
validator.validate('"dasdsd@dkslfksd"@kfdfkds.dlkfdf') //'"dasdsd@dkslfksd"@kfdfkds.dlkfdf'
validator.validate('is+1@uzehuh.ky') //'is+1@uzehuh.ky'

empty value (positive)

import cottus from 'cottus';

const validator = cottus.compile("email");

validator.validate(null) //null
validator.validate(undefined) //undefined

encryption_key

Checks input to be a valid encryption key.

Alias: encryptionKey

Note! The string and base64 rules are checked before running validation.

Errors:

Source

enum

Checks value to be one of possible options.

Errors:

Source

Tests

Examples

enum (positive)

import cottus from 'cottus';

const validator = cottus.compile({"enum":[false,1,"a"]});

validator.validate('a') //'a'
validator.validate(1) //1
validator.validate(false) //false

empty value (positive)

import cottus from 'cottus';

const validator = cottus.compile({"enum":[false,1,"a"]});

validator.validate(null) //null
validator.validate(undefined) //undefined

every

Validate array items.

Errors:

Source

Tests

Examples

valid input (positive)

import cottus from 'cottus';

const validator = cottus.compile({"every":"integer"});

validator.validate([ 1 ]) //[ 1 ]
validator.validate([ 0, 1, -3869 ]) //[ 0, 1, -3869 ]

cast by inner rule (positive)

import cottus from 'cottus';

const validator = cottus.compile({"every":"integer"});

validator.validate([ '5' ]) //[ 5 ]
validator.validate([ 10, '-34', -2 ]) //[ 10, -34, -2 ]

integer

Checks value to be an integer.

Note! The number rule is checked before running validation.

Errors:

Source

Tests

Examples

numbers (positive)

import cottus from 'cottus';

const validator = cottus.compile("integer");

validator.validate(1) //1
validator.validate(-3874) //-3874
validator.validate(0) //0
validator.validate(10000000) //10000000

string (positive)

import cottus from 'cottus';

const validator = cottus.compile("integer");

validator.validate('5') //5
validator.validate('6.0') //6
validator.validate('-34') //-34

empty value (positive)

import cottus from 'cottus';

const validator = cottus.compile("integer");

validator.validate(null) //null
validator.validate(undefined) //undefined

max

Checks value to be less than threshold.

Errors:

Source

Tests

Examples

empty value (positive)

import cottus from 'cottus';

const validator = cottus.compile({"max":5});

validator.validate(null) //null
validator.validate(undefined) //undefined

min

Checks value to be more than threshold.

Errors:

Source

Tests

Examples

empty value (positive)

import cottus from 'cottus';

const validator = cottus.compile({"min":5});

validator.validate(null) //null
validator.validate(undefined) //undefined

number

Checks value to be a number (or number-like string).

Errors:

Source

Tests

Examples

numbers (positive)

import cottus from 'cottus';

const validator = cottus.compile("number");

validator.validate(1) //1
validator.validate(-3874) //-3874
validator.validate(0) //0
validator.validate(10000000) //10000000

string (positive)

import cottus from 'cottus';

const validator = cottus.compile("number");

validator.validate('5') //5
validator.validate('6.0') //6
validator.validate('-34') //-34

empty value (positive)

import cottus from 'cottus';

const validator = cottus.compile("number");

validator.validate(null) //null
validator.validate(undefined) //undefined

port

Checks value to be a valid port number.

Note! The integer rule is checked before running validation.

Errors:

Source

or

Checks value to match one of following rules. throws last failed rule, if none matched.

Source

string

Checks value to be a string.

Errors:

Source

Tests

Examples

string (positive)

import cottus from 'cottus';

const validator = cottus.compile("string");

validator.validate('5') //'5'
validator.validate('odsozci@jiswupre.co.uk') //'odsozci@jiswupre.co.uk'

empty value (positive)

import cottus from 'cottus';

const validator = cottus.compile("string");

validator.validate(null) //null
validator.validate(undefined) //undefined

required

Checks value is present.

Errors:

Source

Tests

Examples

numbers (positive)

import cottus from 'cottus';

const validator = cottus.compile("required");

validator.validate(1) //1
validator.validate(NaN) //NaN
validator.validate(0) //0

time_unit

Checks value to be a time unit.

Alias: timeUnit

Note! The number rule is checked before running validation.

Errors:

Source

Tests

Examples

time_unit (positive)

import cottus from 'cottus';

const validator = cottus.compile("time_unit");

validator.validate('1s') //1000
validator.validate(' -1000') //-1000
validator.validate(0) //0
validator.validate(' -2 hours ') //-7200000

empty value (positive)

import cottus from 'cottus';

const validator = cottus.compile("time_unit");

validator.validate(null) //null
validator.validate(undefined) //undefined

to_lowercase

Transforms value to lowercase.

Alias: to_lower

Note! The string rule is checked before running validation.

Source

uuid

Checks value to be a uuid v4.

Note! The string rule is checked before running validation.

Errors:

Source

Tests

Examples

uuid (positive)

import cottus from 'cottus';

const validator = cottus.compile("uuid");

validator.validate('b69f3671-01eb-4802-88f5-0bf567a9522c') //'b69f3671-01eb-4802-88f5-0bf567a9522c'
validator.validate('497680db-4963-488f-82ee-3ec0e734a7b6') //'497680db-4963-488f-82ee-3ec0e734a7b6'

empty value (positive)

import cottus from 'cottus';

const validator = cottus.compile("uuid");

validator.validate(null) //null
validator.validate(undefined) //undefined