Class AnySchemaBuilder<TRequired, TExplicitType, TResult>

Any schema builder class. Similar to the any type in TypeScript. Allows to define a schema for any value. Use it when you don't know the type of the 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 any() function instead.

Example

const schema = any();
const result = await schema.validate(123);
// result.valid === true
// result.object === 123

Type Parameters

  • TRequired extends boolean = true

  • TExplicitType = undefined

  • TResult = TExplicitType extends undefined
        ? any
        : 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<{
          isRequired: boolean;
          preprocessors: readonly Preprocessor<any>[];
          type: string;
          validators: readonly Validator<any>[];
      }>

      arbitrary props object

    Returns AnySchemaBuilder<TRequired, TExplicitType, TResult>

  • Generates a serializable object describing the defined schema

    Returns {
        isRequired: boolean;
        preprocessors: readonly Preprocessor<TResult>[];
        type: string;
        validators: readonly Validator<TResult>[];
    }

    • isRequired: boolean

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

    • preprocessors: readonly Preprocessor<TResult>[]

      Array of preprocessor functions

    • type: string

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

    • validators: readonly Validator<TResult>[]

      Array of validator functions

  • Parameters

    • object: any

      Object to validate

    • Optional context: ValidationContext

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

  • Performs validation of the schema over object. Basically runs validators, preprocessors and checks for required (if schema is not optional).

    Parameters

    • object: TResult
    • Optional context: ValidationContext

      Optional ValidationContext settings.

    Returns Promise<ValidationResult<TResult>>

Generated using TypeDoc