• Validates a number or an array of numbers, with a fluent interface.

    If passed an array, it will return an ArrayNumberValidator

    If passed anything else, it will return a NumberValidator

    You can pass the things in as a one key object, and it will automatically set the name for you.

    Parameters

    • Optionalnumber: Record<string, number>

    Returns NumberValidator

    // Validate single numbers
    validate(2).gt(1).lt(3); // doesn't throw
    validate(3).gt(1).lt(3); // throws
    // Validate in the object fashion so it automatically sets the name
    let myVar = 5;
    validate({ myVar }).gt(10); // throws `Expected myVar to be greater than 10, got 5`
    // Also used with arrays of numbers
    validate([1, 2, 3]).lt(10); // doesn't throw
    validate([1, 2, 3]).sumto(6); // doesn't throw
    validate([1, 2, 3, 4]).sumtolt(9); // throws
    // All single number validations
    validate(1).int();
    validate(1).positive();
    validate(-1).negative();
    validate(1).between(0, 2);
    validate(1).betweenEq(1, 2);
    validate(1).gt(0);
    validate(1).gteq(1);
    validate(1).lt(2);
    validate(1).lteq(1);
    // All array of numbers validations
    validate([1, 2, 3]).sumcloseto(6);
    validate([1, 2, 3.0001]).sumcloseto(6, 0.001);
    validate([1, 2, 3]).sumto(6);
    validate([1, 2, 3]).sumtolt(7);
    validate([1, 2, 3]).sumtogt(5);
    validate([1, 2, 3]).sumtolteq(6);
    validate([1, 2, 3]).sumtogteq(1);
    validate([1, 2, 3]).int();
    validate([1, 2, 3]).positive();
    validate([-1, -2, -4]).negative();
    validate([1, 2, 3]).between(0, 4);
    validate([1, 2, 3]).betweenEq(1, 3);
    validate([1, 2, 3]).gt(0);
    validate([1, 2, 3]).gteq(1);
    validate([1, 2, 3]).lt(4);
    validate([1, 2, 3]).lteq(3);
  • Parameters

    • Optionalnumber: Record<string, number[]>

    Returns ArrayNumberValidator

  • Parameters

    • Optionalnumber: number

    Returns NumberValidator

  • Parameters

    • Optionalnumber: number[]

    Returns ArrayNumberValidator