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.isOwn(base, prop)

This method is a shortcut of the following call

Object.prototype.hasOwnProperty.call(base, prop)

It serves as a shorthand to replace a lengthy call. Below are some examples of where this is used. Most commonly found when looping through objects, see example 2.

Example 1

const Validate = require('./lib/Validate')
const obj1 = { test1: 'test1' }
class Obj2 {}
Obj2.prototype.test2 = 'test2'
class Obj3 extends Obj2 {
  constructor () {
    super()
    this.test3 = 'test3'
  }
}
const inst1 = new Obj2()
const inst2 = new Obj3()
const isTest1Local = Validate.isOwn(obj1, 'test1') // true
const isTest2Local = Validate.isOwn(inst1, 'test2') // false
const isTest3Local = Validate.isOwn(inst2, 'test3') // true

Example 2

const obj1 = { test1: 'test1', test2: 'test2', test3: 'test3' }
for (let key in obj1) {
  if (!Validate.isOwn(obj1, key)) continue
  // the key is owned by obj1
}

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