Class FunctionSchemaBuilder<TRequired, TExplicitType, TResult>

Schema builder for functions. Allows to define a schema for a function. It can be: required or optional.

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

Example

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

See

func

Type Parameters

  • TRequired extends boolean = true

  • TExplicitType = undefined

  • TResult = TExplicitType extends undefined
        ? ((...args) => 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<((...args) => any)>[];
          type: string;
          validators: readonly Validator<((...args) => any)>[];
      }>

      arbitrary props object

    Returns FunctionSchemaBuilder<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;
    }>>

  • Parameters

    • props: Partial<{
          isRequired: boolean;
          preprocessors: readonly Preprocessor<((...args) => any)>[];
          type: string;
          validators: readonly Validator<((...args) => any)>[];
      }>

    Returns FunctionSchemaBuilder<true, undefined, ((...args) => any)>

Generated using TypeDoc