The base interface for all schemas.

This interface defines the core methods and properties that all schemas must implement, such as parsing, validation, and optionality handling.

Type parameters

  • T

    The type of data that the schema validates.

Hierarchy

Index

Properties

Methods

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

optional

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

    example

    const schema = val.string().optional(); schema.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.

validate

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

    throws

    ValidationError If the input data does not meet the schema's constraints.

    example

    const schema = val.string(); schema.validate("hello"); // Passes validation schema.validate(123); // Throws ValidationError // Example error message: // "String Failed Validation for: ✖ Expected a string, but received a number."

    Parameters

    • data: unknown

      The data to validate.

    Returns T

    The validated value.

Generated using TypeDoc