Libraries (v1.1.10)
    Preparing search index...

    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 array() function instead.

    array

    Type Parameters

    Hierarchy (View Summary)

    Index

    Accessors

    • get isRequired(): TRequired

      Whether the schema requires a non-null/non-undefined value.

      Returns TRequired

    • set isRequired(value: boolean): void

      Sets the requirement flag. Must be a boolean.

      Parameters

      • value: boolean

      Returns void

    • get preprocessors(): Preprocessor<TResult>[]

      A list of preprocessors associated with the Builder

      Returns Preprocessor<TResult>[]

    • get type(): string

      The string identifier of the schema type (e.g. 'string', 'number', 'object').

      Returns string

    • set type(value: string): void

      Sets the schema type identifier. Must be a non-empty string.

      Parameters

      • value: string

      Returns void

    • get validators(): Validator<TResult>[]

      A list of validators associated with the Builder

      Returns Validator<TResult>[]

    Methods

    • Ensures a ValidationErrorMessageProvider is valid. If provider is undefined, falls back to defaultValue. Function providers are bound to this for access to schema state.

      Parameters

      • provider: ValidationErrorMessageProvider<any> | undefined

        the provider to validate, or undefined

      • defaultValue: ValidationErrorMessageProvider<any>

        fallback provider when provider is not supplied

      Returns ValidationErrorMessageProvider<any>

      a valid ValidationErrorMessageProvider

    • Resolves a ValidationErrorMessageProvider to a string error message. Handles both string providers and function providers (sync or async).

      Parameters

      • provider: ValidationErrorMessageProvider<any>

        the error message provider (string or function)

      • seenValue: TResult

        the value that caused the validation error

      Returns Promise<string>

      the resolved error message string

    • Generates a serializable object describing the defined schema

      Returns {
          elementSchema: TElementSchema | undefined;
          isRequired: boolean;
          maxLength: number | undefined;
          maxLengthValidationErrorMessageProvider: ValidationErrorMessageProvider<
              ArraySchemaBuilder<
                  TElementSchema,
                  TRequired,
                  TExplicitType,
                  TExplicitType extends undefined
                      ? TElementSchema extends undefined
                          ? any[]
                          : TElementSchema extends SchemaBuilder<T1, T2>
                              ? (T2 extends true ? T1 : T1 | undefined)[]
                              : never
                      : TExplicitType,
              >,
          >;
          minLength: number | undefined;
          minLengthValidationErrorMessageProvider: ValidationErrorMessageProvider<
              ArraySchemaBuilder<
                  TElementSchema,
                  TRequired,
                  TExplicitType,
                  TExplicitType extends undefined
                      ? TElementSchema extends undefined
                          ? any[]
                          : TElementSchema extends SchemaBuilder<T1, T2>
                              ? (T2 extends true ? T1 : T1 | undefined)[]
                              : never
                      : TExplicitType,
              >,
          >;
          preprocessors: readonly Preprocessor<TResult>[];
          requiredValidationErrorMessageProvider: ValidationErrorMessageProvider<
              SchemaBuilder<any, any>,
          >;
          type: string;
          validators: readonly Validator<TResult>[];
      }

      • elementSchema: TElementSchema | undefined

        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: number | undefined

        Max length of a valid array

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

        Max length validation error message provider. If not provided, default error message provider is used.

      • minLength: number | undefined

        Min length of a valid array

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

        Min length validation error message provider. If not provided, default error message provider is used.

      • preprocessors: readonly Preprocessor<TResult>[]

        Array of preprocessor functions

      • requiredValidationErrorMessageProvider: ValidationErrorMessageProvider<SchemaBuilder<any, any>>

        Custom error message provider for the 'is required' validation error.

      • type: string

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

      • validators: readonly Validator<TResult>[]

        Array of validator functions

    • Runs preprocessors, validators, and the required/optional check on object. Subclasses call this at the start of their validate() implementation to get a preprocessed value wrapped in a transaction, along with any early errors.

      Parameters

      • object: any

        the value to pre-validate

      • Optionalcontext: ValidationContext<SchemaBuilder<any, any>>

        optional validation context settings

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

      a PreValidationResult containing the preprocessed transaction, context, and any errors