Vinyl
    Preparing search index...

    A base class for validation state. Provides methods for chaining.

    Subclasses should not override constructors.

    Type Parameters

    • T extends readonly unknown[]

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    __type: Invariant<T> = ...

    Enforces T is in an invariant position. This prevents a validator with more strict rules is assigned to a less strict type, which can cause unexpected validation errors.

    assert: (
        input: unknown,
        origin?: string,
        path?: readonly string[],
    ) => asserts input is T = ...

    Validates the given input, asserting that the input is type T. If the input is not valid, a ValidationError will be thrown.

    Type declaration

      • (input: unknown, origin?: string, path?: readonly string[]): asserts input is T
      • Parameters

        • input: unknown

          The input to validate.

        • Optionalorigin: string

          The error origin. ErrorOrigin.API by default.

        • Optionalpath: readonly string[]

          The current field path to this validator.

        Returns asserts input is T

        Returns the input, cast as the validated type T

    isValid: (input: unknown) => input is T = ...

    Returns true if the input is valid according to this validator.

    Accessors

    • get description(): string

      A short description of the validator.

      Returns string

    • get options(): { optional: false }

      Returns { optional: false }

    • get base(): ArraySchema<unknown[]>

      The base ArraySchema instance validating that the input is an Array.

      Returns ArraySchema<unknown[]>

    Methods

    • Returns a validator that asserts if this validator and the next passes.

      Type Parameters

      • U

      Parameters

      • nextValidator: Validator<U>

        The validator to run after the current.

      Returns ValueSchema<T & U>

      Returns a new ValueSchema intersecting this validator and the next.

      andValidators

    • Casts the type parameter.

      Type Parameters

      • T extends readonly unknown[]

      Returns ArraySchema<T>

    • Constructs a new schema with a validator chaining the current validator with the provided next in an AND operation.

      Parameters

      • nextValidator: Validator<any, any>

        The validator to run after the current.

      Returns any

      Returns a new Schema instance the same class with a new validator chaining the current validator with the given next in an AND operation.

    • Clones this schema, transforming all validators with the given mutator.

      Parameters

      • transform: (validator: Validator<any>) => Validator<any> = ...

        A transformer which takes an existing validator in the chain and returns a new one. Validators should be considered to be immutable; transform should not change the input but instead return a new validator.

      Returns any

    • Creates a validator that validates the input array has the given maximum length.

      Parameters

      • max: number

        The maximum number of elements (inclusive)

      Returns ArraySchema<T>

    • Creates a validator that validates the input array has the given minimum length.

      Parameters

      • min: number

        The minimum number of elements (inclusive)

      Returns ArraySchema<T>

    • Returns a validator that asserts if this validator or the next passes.

      Implementation note: subclasses should not change the schema instance returned unless all chainable validators accept input type 'unknown'.

      Type Parameters

      • U

      Parameters

      • nextValidator: Validator<U, any>

        The validator to run after the current.

      Returns ValueSchema<T | U>

      Returns a new ValueSchema providing the union of this validator and the next.

    • Casts to a readonly array. This is a shallow cast, elements of the array will not be changed.

      Returns ArraySchema<Readonly<T>>

    • Parameters

      Returns any

    • Parameters

      • currentValidator: Validator<any, any>
      • previousSchema: undefined | SchemaBase<any>
      • operation:
            | undefined
            | (
                (
                    validatorA: Validator<any>,
                    validatorB: Validator<any, any>,
                ) => Validator<T>
            )

      Returns any

    • Creates a validator that asserts that the input array is a tuple whose elements match the given validators.

      Type Parameters

      • U extends readonly any[]

      Parameters

      • ...validators: { [Index in string | number | symbol]: Validator<U[Index<Index>], unknown> }

      Returns ArraySchema<{ [Index in string | number | symbol]: U[Index<Index>] }>

    • Parameters

      • input: unknown

        The input to validate.

      • Optionaloptions: ValidationOptions

        Validation options.

      • Optionalpath: readonly string[]

        The current field path to this validator.

      Returns readonly ValidationErrorMessage[]

      Returns an array of validation failures. If options.all is not true, only the first error will be returned.