Interface for validating enumerated values.

example

const enumSchema = val.EnumSchema(['A', 'B', 'C']); enumSchema.validate('A'); // Validates that the input is one of the enumerated values.

throws

ValidationError If the input is not in the enumeration.

example

// Example error message: // "Enum Failed Validation for: ✖ Expected one of ['A', 'B', 'C'], but received 'D'."

Type parameters

  • T: string | number

    The type of the enumerated values (string or number).

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

  • Coerces the input into one of the enumerated values.

    example

    const enumSchema = val.EnumSchema(['A', 'B', 'C']).coerce(); enumSchema.validate('a'); // Coerces the input to match one of the enumerated values.

    throws

    ValidationError If coercion fails or the input cannot be converted to a valid enumerated value.

    example

    // Example error message: // "Enum Failed Validation for: ✖ Expected one of ['A', 'B', 'C'], but received an invalid value."

    Returns EnumSchema<T>

    The current schema instance.

getValues

  • getValues(): T[]
  • Retrieves the list of valid enumerated values.

    example

    const enumSchema = val.EnumSchema(['A', 'B', 'C']); console.log(enumSchema.getValues()); // Outputs: ['A', 'B', 'C']

    Returns T[]

    An array of valid enumerated values.

optional

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

    example

    const enumSchema = val.EnumSchema(['A', 'B', 'C']).optional(); enumSchema.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 enumeration schema.

    throws

    ValidationError If the input is not in the enumeration.

    example

    const enumSchema = val.EnumSchema(['A', 'B', 'C']); enumSchema.validate('D'); // Throws ValidationError // Example error message: // "Enum Failed Validation for: ✖ Expected one of ['A', 'B', 'C'], but received 'D'."

    Parameters

    • data: unknown

      The data to validate.

    Returns T

    The validated enumerated value.

Generated using TypeDoc