Class ArraySchemaBuilder<TElementSchema, TRequired, TExplicitType, TResult>

Similar to the Array type in TypeScript. It can be used to validate arrays of any type. It can also be used to validate arrays of specific type. For example, if you want to validate an array of numbers, you can use array(number()) to create a schema builder. If you want to validate an array of users, you can use array().of(object({ name: string(), age: number() })) to create a schema builder. If you want to validate an array of numbers or strings, you can use array(union(number()).or(string())).

Also you can limit the length of the array by using minLength and maxLength methods.

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.

See

array

Type Parameters

  • TElementSchema extends SchemaBuilder<any, any>

  • TRequired extends boolean = true

  • TExplicitType = undefined

  • TResult = TExplicitType extends undefined
        ? TElementSchema extends undefined
            ? any[]
            : TElementSchema extends SchemaBuilder<infer T1, infer T2>
                ? InferType<SchemaBuilder<T1, T2>>[]
                : never
        : 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

  • Clear max length of the valid array value for schema.

    Returns ArraySchemaBuilder<TElementSchema, TRequired, TExplicitType, TExplicitType extends undefined
        ? TElementSchema extends undefined
            ? any[]
            : TElementSchema extends SchemaBuilder<T1, T2>
                ? (T2 extends true
                    ? T1
                    : undefined | T1)[]
                : never
        : TExplicitType>

  • Clear minimal length of the valid array value for schema.

    Returns ArraySchemaBuilder<TElementSchema, TRequired, TExplicitType, TExplicitType extends undefined
        ? TElementSchema extends undefined
            ? any[]
            : TElementSchema extends SchemaBuilder<T1, T2>
                ? (T2 extends true
                    ? T1
                    : undefined | T1)[]
                : never
        : TExplicitType>

  • Generates a serializable object describing the defined schema

    Returns {
        elementSchema: undefined | TElementSchema;
        isRequired: boolean;
        maxLength: undefined | number;
        minLength: undefined | number;
        preprocessors: readonly Preprocessor<TResult>[];
        type: string;
        validators: readonly Validator<TResult>[];
    }

    • elementSchema: undefined | TElementSchema

      Schema of array item (if defined)

    • isRequired: boolean

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

    • maxLength: undefined | number

      Max length of a valid array

    • minLength: undefined | number

      Min length of a valid array

    • 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

  • Set max length of the valid array value for schema.

    Type Parameters

    • T extends number

    Parameters

    • length: T

    Returns ArraySchemaBuilder<TElementSchema, TRequired, TExplicitType, TExplicitType extends undefined
        ? TElementSchema extends undefined
            ? any[]
            : TElementSchema extends SchemaBuilder<T1, T2>
                ? (T2 extends true
                    ? T1
                    : undefined | T1)[]
                : never
        : TExplicitType>

  • Set minimal length of the valid array value for schema.

    Type Parameters

    • T extends number

    Parameters

    • length: T

    Returns ArraySchemaBuilder<TElementSchema, TRequired, TExplicitType, TExplicitType extends undefined
        ? TElementSchema extends undefined
            ? any[]
            : TElementSchema extends SchemaBuilder<T1, T2>
                ? (T2 extends true
                    ? T1
                    : undefined | T1)[]
                : never
        : TExplicitType>

  • Set a schema that every array item has to satisfy. If it is not set, Item of any type is allowed.

    Type Parameters

    Parameters

    • schema: TSchema

      Schema that every array item has to satisfy

    Returns ArraySchemaBuilder<TSchema, TRequired, TExplicitType, TExplicitType extends undefined
        ? TSchema extends undefined
            ? any[]
            : TSchema extends SchemaBuilder<T1, T2>
                ? (T2 extends true
                    ? T1
                    : undefined | T1)[]
                : never
        : TExplicitType>

  • Parameters

    • object: any

      Object to validate

    • Optional context: ValidationContext

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

Generated using TypeDoc