A schema for validating string values. Provides methods for coercion, optionality, and enforcing constraints like minimum/maximum length, email, UUID, URL, and ISO date formats.

example

const schema = val.string(); const data = "hello"; const validatedData = schema.validate(data); // Passes console.log(validatedData); // "hello"

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 a string value.

    example

    const schema = val.string().coerce(); schema.validate(123); // Coerces the number 123 into the string "123".

    Returns this

    The current schema instance.

email

  • email(options?: { pattern?: RegExp }): this
  • Validates that the string is a valid email address.

    throws

    ValidationError If the string is not a valid email address.

    example

    const schema = val.string().email(); schema.validate("invalid-email"); // Throws ValidationError // Example error message: // "String Failed Validation for: ✖ Expected a valid email address, but received 'invalid-email'."

    Parameters

    • Optional options: { pattern?: RegExp }

      Optional configuration for custom email patterns.

      • Optional pattern?: RegExp

    Returns this

    The current schema instance.

isoDate

  • isoDate(): this
  • Validates that the string is a valid ISO 8601 date.

    throws

    ValidationError If the string is not a valid ISO 8601 date.

    example

    const schema = val.string().isoDate(); schema.validate("not-a-date"); // Throws ValidationError // Example error message: // "String Failed Validation for: ✖ Expected a valid ISO 8601 date, but received 'not-a-date'."

    Returns this

    The current schema instance.

maxLength

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

    throws

    ValidationError If the string is longer than the specified length.

    example

    const schema = val.string().maxLength(10); schema.validate("this is too long"); // Throws ValidationError // Example error message: // "String Failed Validation for: ✖ Expected a string with at most 10 characters, but received a string with 17 characters."

    Parameters

    • value: number

      The maximum length allowed.

    Returns this

    The current schema instance.

minLength

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

    throws

    ValidationError If the string is shorter than the specified length.

    example

    const schema = val.string().minLength(5); schema.validate("hi"); // Throws ValidationError // Example error message: // "String Failed Validation for: ✖ Expected a string with at least 5 characters, but received a string with 2 characters."

    Parameters

    • value: number

      The minimum length allowed.

    Returns this

    The current schema instance.

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): string
  • 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 string

    The parsed value.

url

  • url(): this
  • Validates that the string is a valid URL.

    throws

    ValidationError If the string is not a valid URL.

    example

    const schema = val.string().url(); schema.validate("invalid-url"); // Throws ValidationError // Example error message: // "String Failed Validation for: ✖ Expected a valid URL, but received 'invalid-url'."

    Returns this

    The current schema instance.

uuid

  • uuid(): this
  • Validates that the string is a valid UUID.

    throws

    ValidationError If the string is not a valid UUID.

    example

    const schema = val.string().uuid(); schema.validate("not-a-uuid"); // Throws ValidationError // Example error message: // "String Failed Validation for: ✖ Expected a valid UUID, but received 'not-a-uuid'."

    Returns this

    The current schema instance.

validate

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

    throws

    ValidationError If the input is not a valid string.

    example

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

    The validated string.

Generated using TypeDoc