@vinejs/vine - v4.2.0
    Preparing search index...

    Class VineNumber

    VineNumber represents a numeric value in the validation schema. It accepts both string and number inputs and converts them to numbers, with comprehensive validation for ranges, decimals, and sign constraints.

    const schema = vine.number()
    .min(0)
    .max(100)
    .decimal([0, 2])

    const result = await vine.validate({
    schema,
    data: "42.5"
    })

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    "[ITYPE]": string | number

    Define the input type of the schema for TypeScript inference

    "[OTYPE]": number

    The output value type of the field after validation. The property points to a type only and not the real value.

    "[COTYPE]": number

    Type marker for camelCase output type inference

    dataTypeValidator?: Validation<any>

    The validation to use for validating the schema data type. Using a data type validator guards custom rules to only run when the data type validation passes.

    class StringSchema extends BaseLiteralType {
    dataTypeValidator = stringDataTypeRule
    }
    options: FieldOptions & { strict?: boolean }

    Configuration options for this field including bail mode, nullability, and parsing

    rules: {
        in: (
            ...options: [options: { values: number[] }],
        ) => Validation<{ values: number[] }>;
        max: (
            ...options: [options: { max: number }],
        ) => Validation<{ max: number }>;
        min: (
            ...options: [options: { min: number }],
        ) => Validation<{ min: number }>;
        range: (
            ...options: [options: { min: number; max: number }],
        ) => Validation<{ min: number; max: number }>;
        number: (
            ...options: [options: { strict?: boolean }],
        ) => Validation<{ strict?: boolean }>;
        decimal: (
            ...options: [options: { range: [number, number?] }],
        ) => Validation<{ range: [number, number?] }>;
        negative: (...options: [options?: undefined]) => Validation<undefined>;
        positive: (...options: [options?: undefined]) => Validation<undefined>;
        nonNegativeRule: (
            ...options: [options?: undefined],
        ) => Validation<undefined>;
        nonPositiveRule: (
            ...options: [options?: undefined],
        ) => Validation<undefined>;
        withoutDecimals: (
            ...options: [options?: undefined],
        ) => Validation<undefined>;
    } = ...

    Static collection of all available validation rules for numbers

    "[SUBTYPE]": string = 'number'

    The subtype identifier for the literal schema field

    "[UNIQUE_NAME]": string = 'vine.number'

    Unique name identifier for union type resolution

    Methods

    • Define a method to parse the input value. The method is invoked before any validation and hence you must perform type-checking to know the value you are working it.

      Parameters

      • callback: ParseFn

      Returns this

    • Adds a validation rule to the schema's validation chain. Rules are executed in the order they are added.

      Parameters

      Returns this

      This schema instance for method chaining

      vine.string().use(minLength({ length: 3 }))
      vine.number().use(customRule({ strict: true }))
    • Enable/disable bail mode for this field. In bail mode, field validations stop after the first error.

      Parameters

      • state: boolean

        Whether to enable bail mode

      Returns VineNumber

      This schema instance for method chaining

      vine.string().bail(false) // Continue validation after first error
      vine.number().bail(true) // Stop after first error (default)
    • Compiles the literal schema type into a compiler node. This method transforms the schema definition into a format that the validation compiler can process.

      Parameters

      • propertyName: string

        Name of the property being compiled

      • refs: RefsStore

        Reference store for the compiler

      • options: ParserOptions

        Parser options including camelCase conversion

      Returns FieldNode & {} & { subtype: string }

      Compiled literal node with subtype information

    • Type checker function to determine if a value can be converted to a number. Required for "unionOfTypes" functionality.

      Parameters

      • value: unknown

        The value to check

      Returns boolean

      True if the value can be converted to a valid number

    • Enforce value to be within the range of minimum and maximum values.

      Parameters

      • value: [min: number, max: number]

        Tuple containing [min, max] values

      Returns VineNumber

      This number schema instance for method chaining

    • Enforces the value to be a positive number (greater than 0).

      Returns VineNumber

      This number schema instance for method chaining

      vine.number().positive()  // Accepts 1, 100.5, etc. Rejects 0, -1
      
    • Enforces the value to be a negative number (less than 0).

      Returns VineNumber

      This number schema instance for method chaining

      vine.number().negative()  // Accepts -1, -100.5, etc. Rejects 0, 1
      
    • Enforces the value to be a non-negative number (greater than or equal to 0).

      Returns VineNumber

      This number schema instance for method chaining

      vine.number().nonNegative()  // Accepts 0, 1, 100.5, etc. Rejects -1
      
    • Enforces the value to be a non-positive number (less than or equal to 0).

      Returns VineNumber

      This number schema instance for method chaining

      vine.number().nonPositive()  // Accepts 0, -1, -100.5, etc. Rejects 1
      
    • Enforces the value to have a fixed number or range of decimal places.

      Parameters

      • range: number | [number, number]

        Exact number of decimal places or [min, max] range

      Returns VineNumber

      This number schema instance for method chaining

      vine.number().decimal(2)       // Accepts 1.23, rejects 1.2 or 1.234
      vine.number().decimal([0, 2]) // Accepts 1, 1.2, 1.23
    • Enforces the value to be an integer without decimal places.

      Returns VineNumber

      This number schema instance for method chaining

      vine.number().withoutDecimals()  // Accepts 42, -10, rejects 42.5
      
    • Enforces the value to be one of the specified allowed values.

      Parameters

      • values: number[]

        Array of allowed numeric values

      Returns VineNumber

      This number schema instance for method chaining

      vine.number().in([1, 2, 3, 5, 8, 13])  // Only Fibonacci numbers allowed