Interface for validating arrays with various constraints.

example

const arraySchema = val.ArraySchema(val.NumberSchema()); arraySchema.minLength(2).maxLength(5).validate([1, 2, 3]); // Validates an array with 2 to 5 elements.

throws

ValidationError If the array does not meet the specified constraints.

example

// Example error message: // "Array Failed Validation for: ✖ Array must have at least 2 elements."

Type parameters

  • T

    The type of elements in the array.

Hierarchy

Index

Properties

isOptional

isOptional: boolean

Indicates whether the schema allows undefined as a valid value.

example

const schema = val.string().optional(); console.log(schema.isOptional); // true

type

type: string

The type of data that the schema validates, represented as a string.

example

const schema = val.string(); console.log(schema.type); // "string"

Methods

coerce

  • coerce(): this
  • Coerces the input into the desired array format.

    example

    const arraySchema = val.ArraySchema(val.NumberSchema()).coerce(); arraySchema.validate(["1", "2", "3"]); // Coerces strings into numbers and validates.

    throws

    ValidationError If coercion is not supported for the element schema.

    example

    // Example error message: // "Array Failed Validation for: ✖ Coercion is only supported for number schemas."

    Returns this

    The current schema instance.

maxLength

  • maxLength(length: number): this
  • Sets the maximum length constraint for the array.

    throws

    ValidationError If the array has more elements than the specified length.

    example

    const arraySchema = val.ArraySchema(val.NumberSchema()); arraySchema.maxLength(2).validate([1, 2, 3]); // Throws ValidationError // Example error message: // "Array Failed Validation for: ✖ Array must have at most 2 elements."

    Parameters

    • length: number

      The maximum number of elements allowed in the array.

    Returns this

    The current schema instance.

minLength

  • minLength(length: number): this
  • Sets the minimum length constraint for the array.

    throws

    ValidationError If the array has fewer elements than the specified length.

    example

    const arraySchema = val.ArraySchema(val.NumberSchema()); arraySchema.minLength(3).validate([1, 2]); // Throws ValidationError // Example error message: // "Array Failed Validation for: ✖ Array must have at least 3 elements."

    Parameters

    • length: number

      The minimum number of elements allowed in the array.

    Returns this

    The current schema instance.

nonEmpty

  • nonEmpty(): this
  • Ensures the array is not empty.

    throws

    ValidationError If the array is empty.

    example

    const arraySchema = val.ArraySchema(val.NumberSchema()); arraySchema.nonEmpty().validate([]); // Throws ValidationError // Example error message: // "Array Failed Validation for: ✖ Array must not be empty."

    Returns this

    The current schema instance.

optional

  • optional(): this
  • Marks the array as optional, allowing undefined as a valid value.

    example

    const arraySchema = val.ArraySchema(val.NumberSchema()); arraySchema.optional().validate(undefined); // Passes validation

    Returns this

    The current schema instance.

parse

  • parse(data: unknown): T[]
  • Parses the input data and returns the parsed value.

    This method is typically used to coerce or transform the input data into the desired format.

    throws

    ValidationError If the input data cannot be parsed into the desired format.

    example

    const schema = val.number().coerce(); const parsedValue = schema.parse("42"); // Returns 42 as a number

    Parameters

    • data: unknown

      The input data to parse.

    Returns T[]

    The parsed value.

unique

  • unique(): this
  • Ensures all elements in the array are unique.

    throws

    ValidationError If the array contains duplicate elements.

    example

    const arraySchema = val.ArraySchema(val.NumberSchema()); arraySchema.unique().validate([1, 2, 2]); // Throws ValidationError // Example error message: // "Array Failed Validation for: ✖ Array elements must be unique."

    Returns this

    The current schema instance.

validate

  • validate(data: unknown): T[]
  • Validates the input data against the array schema.

    throws

    ValidationError If the input is not a valid array or does not meet the constraints.

    example

    const arraySchema = val.ArraySchema(val.NumberSchema()); arraySchema.validate("not-an-array"); // Throws ValidationError // Example error message: // "Array Failed Validation for: ✖ Expected an array, but received a string."

    Parameters

    • data: unknown

      The data to validate.

    Returns T[]

    The validated array.

Generated using TypeDoc