Class NumberSchemaBuilder<TResult, TRequired>

Number schema builder class. Allows to create Number schemas. Can be required or optional, can be restricted to be equal to a certain value, can be restricted to be in a certain range, can be restricted to be integer.

NOTE this class is exported only to give opportunity to extend it by inheriting. It is not recommended to create an instance of this class directly. Use () function instead.

Example

const schema = number().equals(42);
const result = await schema.validate(42);
// result.valid === true
// result.object === 42

Example

const schema = number();
const result = await schema.validate('42');
// result.valid === false
// result.errors[0].message === 'is expected to be a number'

Example

const schema = number().min(0).max(100);
const result = await schema.validate(42);
// result.valid === true
// result.object === 42

Example

const schema = number().min(0).max(100);
const result = await schema.validate(142.5);
// result.valid === false
// result.errors[0].message === 'is expected to be less than or equal to 100'

See

number

Type Parameters

  • TResult = number

  • TRequired extends boolean = true

Hierarchy

Accessors

  • get preprocessors(): Preprocessor<TResult>[]
  • A list of preprocessors associated with the Builder

    Returns Preprocessor<TResult>[]

  • get validators(): Validator<TResult>[]
  • A list of validators associated with the Builder

    Returns Validator<TResult>[]

Methods

  • Protected method used to create an new instance of the Builder defined by the props object. Should be used to instanticate new builders to keep builder's immutability.

    Type Parameters

    • T

    • TReq extends boolean

    Parameters

    • props: Partial<{
          ensureIsFinite: boolean;
          ensureNotNaN: boolean;
          equalsTo: undefined | number;
          isInteger: boolean;
          isRequired: boolean;
          max: undefined | number;
          min: undefined | number;
          preprocessors: Preprocessor<T>[];
          type: string;
          validators: Validator<T>[];
      }>

      arbitrary props object

    Returns NumberSchemaBuilder<TResult, TRequired>

  • Generates a serializable object describing the defined schema

    Returns {
        ensureIsFinite: boolean;
        ensureNotNaN: boolean;
        equalsTo: undefined | number;
        isInteger: boolean;
        isRequired: boolean;
        max: undefined | number;
        min: undefined | number;
        preprocessors: Preprocessor<TResult>[];
        type: string;
        validators: Validator<TResult>[];
    }

    • ensureIsFinite: boolean

      Make sure that object is not different kinds of infinity. true by default.

    • ensureNotNaN: boolean

      Make sure that object is not NaN. true by default.

    • equalsTo: undefined | number

      If set, restrict object to be equal to a certain value.

    • isInteger: boolean

      Allow only integer values (floating point values will be rejected as invalid)

    • isRequired: boolean

      If set to false, schema will be optional (null or undefined values will be considered as valid).

    • max: undefined | number

      Max valid value (if defined).

    • min: undefined | number

      Min valid value (if defined).

    • preprocessors: Preprocessor<TResult>[]

      Array of preprocessor functions

    • type: string

      String id of schema type, e.g. string', numberorobject`.

    • validators: Validator<TResult>[]

      Array of validator functions

  • Parameters

    • object: any

      Object to validate

    • Optional context: ValidationContext

    Returns Promise<PreValidationResult<any, {
        validatedObject: any;
    }>>

  • Parameters

    • props: Partial<{
          ensureIsFinite: boolean;
          ensureNotNaN: boolean;
          equalsTo: undefined | number;
          isInteger: boolean;
          isRequired: boolean;
          max: undefined | number;
          min: undefined | number;
          preprocessors: Preprocessor<number>[];
          type: string;
          validators: Validator<number>[];
      }>

    Returns NumberSchemaBuilder<number, true>

Generated using TypeDoc