Interface for validating numeric values.

example

const numberSchema = val.NumberSchema(); numberSchema.min(1).max(10).validate(5); // Validates a number between 1 and 10.

throws

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

example

// Example error message: // "Number Failed Validation for: ✖ Expected a number greater than or equal to 1, but received -1."

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 a valid number.

    example

    const numberSchema = val.NumberSchema().coerce(); numberSchema.validate("42"); // Coerces the string "42" into a number.

    throws

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

    example

    // Example error message: // "Number Failed Validation for: ✖ Expected a number, but received an invalid coercible value."

    Returns BaseSchema<number>

    The current schema instance.

max

  • max(value: number): this
  • Sets the maximum value constraint for the number.

    throws

    ValidationError If the number is greater than the specified maximum value.

    example

    const numberSchema = val.NumberSchema(); numberSchema.max(5).validate(6); // Throws ValidationError // Example error message: // "Number Failed Validation for: ✖ Expected a number less than or equal to 5, but received 6."

    Parameters

    • value: number

      The maximum value allowed.

    Returns this

    The current schema instance.

min

  • min(value: number): this
  • Sets the minimum value constraint for the number.

    throws

    ValidationError If the number is less than the specified minimum value.

    example

    const numberSchema = val.NumberSchema(); numberSchema.min(3).validate(2); // Throws ValidationError // Example error message: // "Number Failed Validation for: ✖ Expected a number greater than or equal to 3, but received 2."

    Parameters

    • value: number

      The minimum value allowed.

    Returns this

    The current schema instance.

optional

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

    example

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

    Returns this

    The current schema instance.

parse

  • parse(data: unknown): number
  • 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 number

    The parsed value.

validate

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

    throws

    ValidationError If the input is not a valid number.

    example

    const numberSchema = val.NumberSchema(); numberSchema.validate("not-a-number"); // Throws ValidationError // Example error message: // "Number Failed Validation for: ✖ Expected a number, but received a string."

    Parameters

    • data: unknown

      The data to validate.

    Returns number

    The validated number.

Generated using TypeDoc