Class BooleanSchemaBuilder<TResult, TRequired, TExplicitType, TFinalResult>

Similar to boolean type in TypeScript. Allows to define a schema for a boolean value. It can be required or optional. It can be restricted to be equal to a certain value.

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 = boolean().equals(true);
const result = await schema.validate(true);
// result.valid === true
// result.object === true

Example

const schema = boolean().equals(false);
const result = await schema.validate(true);
// result.valid === false
// result.errors[0].message === 'is expected to be equal to 'false''

Example

const schema = boolean().equals(true).optional();
const result = await schema.validate(undefined);
// result.valid === true
// result.object === undefined

See

boolean

Type Parameters

  • TResult = boolean

  • TRequired extends boolean = true

  • TExplicitType = undefined

  • TFinalResult = TExplicitType extends undefined
        ? TResult
        : TExplicitType

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

    • TReq extends boolean

    Parameters

    • props: Partial<{
          equalsTo: undefined | boolean;
          isRequired: boolean;
          preprocessors: readonly Preprocessor<TReq>[];
          type: string;
          validators: readonly Validator<TReq>[];
      }>

      arbitrary props object

    Returns BooleanSchemaBuilder<TResult, TRequired, TExplicitType, TFinalResult>

  • Restricts object to be equal to value.

    Type Parameters

    • T extends boolean

    Parameters

    • value: T

    Returns BooleanSchemaBuilder<T, TRequired, TExplicitType, TExplicitType extends undefined
        ? T
        : TExplicitType>

  • Set type of schema explicitly. notUsed param is needed only for cas when JS is used. E.g. when you can't call method like schema.hasType<Date>(), so instead you can call schema.hasType(new Date()) with the same result.

    Type Parameters

    • T

    Parameters

    • Optional notUsed: T

    Returns BooleanSchemaBuilder<TResult, true, T, T extends undefined
        ? TResult
        : T>

  • Generates a serializable object describing the defined schema

    Returns {
        equalsTo: undefined | boolean;
        isRequired: boolean;
        preprocessors: readonly Preprocessor<TFinalResult>[];
        type: string;
        validators: readonly Validator<TFinalResult>[];
    }

    • equalsTo: undefined | boolean

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

    • isRequired: boolean

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

    • preprocessors: readonly Preprocessor<TFinalResult>[]

      Array of preprocessor functions

    • type: string

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

    • validators: readonly Validator<TFinalResult>[]

      Array of validator functions

  • Parameters

    • object: any

      Object to validate

    • Optional context: ValidationContext

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

  • Parameters

    • props: Partial<{
          equalsTo: undefined | boolean;
          isRequired: boolean;
          preprocessors: readonly Preprocessor<any>[];
          type: string;
          validators: readonly Validator<any>[];
      }>

    Returns BooleanSchemaBuilder<boolean, any, undefined, boolean>

Generated using TypeDoc