Validate numbers in a fluent fashion.

Each validator method accepts a message as the last parameter for customising the error message.

const n = new NumberValidator();
n.validate(0).gt(1); // NumberValidationError
const n = new NumberValidator();
const probability = -0.1;
n.validate(probability).gteq(0, 'Probabilities should always be >= 0'); // NumberValidationError('Probabilities should always be >= 0').

Constructors

Properties

name: string = 'number'

The name of the variable being validated - shows up in error messages.

Accessors

  • get number(): undefined | number
  • Returns undefined | number

  • set number(number): void
  • Parameters

    • number: undefined | number

    Returns void

Methods

  • Parameters

    • Optionalnum: number

    Returns num is number

  • Parameters

    • Optionalmsg: string

    Returns this

  • Pass a string decribing the varname to this to make the error messages make more sense in your context.

    Parameters

    • name: string

      [description]

    Returns NumberValidator

    const potato = 1;
    validate(potato).varname('potato').gt(2); // "Expected potato to be greater than 2, got 1"