Class UnionSchemaBuilder<TOptions, TRequired, TExplicitType>

Union schema builder class. Allows to create schemas containing alternatives. E.g. string | number | Date. Use it when you want to define a schema for a value that can be of different types. The type of the value will be determined by the first schema that succeeds validation. Any schema type can be supplied as variant. Which means that you are not limited to primitive types and can construct complex types as well, e.g. object | array.

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 = union(string('foo')).or(string('bar'));
const result = await schema.validate('foo');
// result.valid === true
// result.object === 'foo'

Example

const schema = union(string('foo')).or(string('bar'));
const result = await schema.validate('baz');
// result.valid === false

Example

const schema = union(string('yes')).or(string('no')).or(number(0)).or(number(1));
// equals to 'yes' | 'no' | 0 | 1 in TS
const result = await schema.validate('yes');
// result.valid === true
// result.object === 'yes'

const result2 = await schema.validate(0);
// result2.valid === true
// result2.object === 0

const result3 = await schema.validate('baz');
// result3.valid === false

const result4 = await schema.validate(2);
// result4.valid === false

See

union

Type Parameters

  • TOptions extends readonly SchemaBuilder<any, any>[]

  • TRequired extends boolean = true

  • TExplicitType = undefined

Hierarchy

  • SchemaBuilder<TExplicitType extends undefined
        ? SchemaArrayToUnion<TOptions>
        : TExplicitType, TRequired>
    • UnionSchemaBuilder

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 extends readonly SchemaBuilder<any, any>[]

    • TReq extends boolean

    Parameters

    • props: Partial<{
          isRequired: boolean;
          options: T;
          preprocessors: readonly Preprocessor<SchemaArrayToUnion<T>>[];
          type: string;
          validators: readonly Validator<SchemaArrayToUnion<T>>[];
      }>

      arbitrary props object

    Returns UnionSchemaBuilder<TOptions, TRequired, TExplicitType>

  • Generates a serializable object describing the defined schema

    Returns {
        isRequired: boolean;
        options: TOptions;
        preprocessors: readonly Preprocessor<TExplicitType extends undefined
            ? SchemaArrayToUnion<TOptions>
            : TExplicitType>[];
        type: string;
        validators: readonly Validator<TExplicitType extends undefined
            ? SchemaArrayToUnion<TOptions>
            : TExplicitType>[];
    }

    • isRequired: boolean

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

    • options: TOptions

      Array of schemas participating in the union.

    • preprocessors: readonly Preprocessor<TExplicitType extends undefined
          ? SchemaArrayToUnion<TOptions>
          : TExplicitType>[]

      Array of preprocessor functions

    • type: string

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

    • validators: readonly Validator<TExplicitType extends undefined
          ? SchemaArrayToUnion<TOptions>
          : TExplicitType>[]

      Array of validator functions

  • Parameters

    • object: any

      Object to validate

    • Optional context: ValidationContext

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

  • Removes option by its index. If index is out of bounds, an error is thrown.

    Type Parameters

    • T extends number

    Parameters

    • index: T

      index of the option, starting from 0.

    Returns UnionSchemaBuilder<[...TakeBeforeIndex<TOptions, T>[], ...TakeAfterIndex<TOptions, T, []>[]], TRequired, TExplicitType>

  • Removes all options and replaces them by single schema option. Equivalent to union(schema) function, but could be useful in some cases.

    Type Parameters

    Parameters

    • schema: T

      schema to be added as a single option to the new schema.

    Returns UnionSchemaBuilder<[T], TRequired, TExplicitType>

  • Performs validion of the union schema over object.

    Parameters

    • object: TExplicitType extends undefined
          ? SchemaArrayToUnion<TOptions>
          : TExplicitType
    • Optional context: ValidationContext

      Optional ValidationContext settings.

    Returns Promise<ValidationResult<TExplicitType extends undefined
        ? SchemaArrayToUnion<TOptions>
        : TExplicitType>>

  • Parameters

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

    Returns UnionSchemaBuilder<any, true, undefined>

Generated using TypeDoc