Validate

Introduced in 4.0.0

Stability: 2 - Stable

The Validate library provides validation of data and data types.

The Validate library should be used for boolean evaluations, eg: if statements or other logic.

The Assert library should be used for building tests, or to protect code from execution when input sanity is not achieved.

const Validate = require('kado/lib/Validate')

Class: AssertionError

AssertionError.constructor(data)

This constructor takes a string for easier setup. It extends the standard AssertionError and is the same other than the simplified allowance of string input on the constructor. It is available via alias Validate.Error which should be used instead to ensure it not be assumed to be the standard method.


Class: Validate

Evaluates data and data types to {boolean}.

static Validate.getInstance(data)

Validate.constructor(data)


Stateful functions

These are used upon an instance, with the data provided in the constructor

Validate.above(floor)

Validate.below(ceiling)

Validate.equal(against)

Validate.is(type)

Validate.min(bottom)

Validate.max(top)

Validate.not(val)

Validate.ok(message)


Static (stateless) functions

These methods have common return and throw behavior (except where specified):

static Validate.assert(val1, val2)

Loosely asserts data types to okay and has logic to improve matching types:

If none of the above types are passed, the assertion will be tested against Validate.eq(val1, val2)

static Validate.date(val1, val2, options)

With no options, valid when val1 is the exact same date as val2

When granularity is used, time is subdivided by the provided milliseconds and val1 should be in the same subdivision as val2 to be considered valid.

When distance is used, val1 difference from val2 should be equal to or less than provided milliseconds to be considered valid.

static Validate.eq(val1, val2)

Valid when val1 equal to val2

static Validate.eqDeep(val1, val2)

Valid when val1 deeply matches val2 Note: Uses Node.JS->assert->deepStrictEqual for evaluations

static Validate.neq(val1, val2)

Valid when val1 not equal to val2

static Validate.getType(val)

Utility method to obtain type or class, internally used by the next method:

static Validate.isType(type, value)

Valid when type or class of value matches

static Validate.isAbove(base, test)

Valid when test is greater than base

static Validate.isbelow(base, test)

Valid when test is less than base

static Validate.minimum(base, test)

Valid when test is greater than or equal to base

static Vaidate.maximum(base, test)

Valid when test is less than or equal to base

static Validate.isOk(value)

Valid when value is truthy

static Validate.catch(fn, err)

Valid when err matches caught Error.message