@vinejs/vine - v4.0.0-next.1
    Preparing search index...

    Class SchemaBuilder

    SchemaBuilder exposes methods to construct Vine validation schemas. It provides a fluent API for creating all supported schema types and can be extended with custom methods using macros.

    const builder = new SchemaBuilder()
    const schema = builder.object({
    name: builder.string(),
    age: builder.number().min(0),
    email: builder.string().email()
    })

    Hierarchy (View Summary)

    • default
    Index

    Constructors

    • Constructor that applies all registered instance properties to the new instance. This method iterates through the instanceMacros set and assigns each property to the instance, binding functions to the instance context.

      Returns SchemaBuilder

    Properties

    group: typeof group = group

    Define a sub-object as a conditional union group. Groups allow creating conditional validations based on discriminator fields.

    vine.group([
    vine.group.if(vine.string(), vine.object({ name: vine.string() }))
    ])
    union: typeof union = union

    Define a union of multiple schema types. Union schemas attempt to validate against each schema until one succeeds.

    vine.union([
    vine.string(),
    vine.number()
    ])

    Methods

    • Define a boolean value schema with optional strict mode.

      Parameters

      • Optionaloptions: { strict: boolean }

        Configuration options for boolean validation

      Returns VineBoolean

      A VineBoolean schema instance

      vine.boolean({ strict: true }) // Only accepts true/false
      
    • Define a number value schema with optional strict mode.

      Parameters

      • Optionaloptions: { strict: boolean }

        Configuration options for number validation

      Returns VineNumber

      A VineNumber schema instance

      vine.number().min(0).max(100)
      
    • Define a schema type that validates input matches a specific literal value. Useful for validating exact strings, numbers, or boolean values.

      Type Parameters

      • const Value extends Literal

        The literal value type to validate against

      Parameters

      • value: Value

        The exact value that input must match

      Returns VineLiteral<Value>

      A VineLiteral schema instance

      vine.literal('hello') // Only accepts "hello"
      vine.literal(42) // Only accepts 42
      vine.literal(true) // Only accepts true
    • Define an optional value that can be undefined. Chain the "nullable" method to also allow null values in the output.

      Returns VineOptional<undefined>

      A VineOptional schema instance

      vine.optional() // Accepts undefined
      vine.optional().nullable() // Accepts undefined or null
    • Define an object schema with known properties and their validation schemas. You may call "allowUnknownProperties" to merge unknown properties into the output.

      Type Parameters

      • Properties extends Record<string, SchemaTypes>

        Record of property names to their schema types

      Parameters

      • properties: Properties

        Object defining the schema for each property

      Returns VineObject<
          Properties,
          Id<
              {
                  [K in string
                  | number
                  | symbol]?: {
                      [K in string | number | symbol]: Properties[K][typeof ITYPE]
                  }[K]
              } & {
                  [K in string
                  | number
                  | symbol]: {
                      [K in string | number | symbol]: Properties[K][typeof ITYPE]
                  }[K]
              },
          >,
          Id<
              {
                  [K in string
                  | number
                  | symbol]?: {
                      [K in string | number | symbol]: Properties[K][typeof OTYPE]
                  }[K]
              } & {
                  [K in string
                  | number
                  | symbol]: {
                      [K in string | number | symbol]: Properties[K][typeof OTYPE]
                  }[K]
              },
          >,
          Id<
              {
                  [K in string
                  | number
                  | symbol]?: {
                      [K in string | number | symbol as Join<
                          CamelCasify<Words<(...) & (...)>>,
                          "",
                      >]: Properties[K][typeof COTYPE]
                  }[K]
              } & {
                  [K in string
                  | number
                  | symbol]: {
                      [K in string | number | symbol as Join<
                          CamelCasify<Words<K & string>>,
                          "",
                      >]: Properties[K][typeof COTYPE]
                  }[K]
              },
          >,
      >

      A VineObject schema instance

      vine.object({
      name: vine.string().minLength(2),
      age: vine.number().min(0),
      email: vine.string().email().optional()
      })
    • Define an array schema that validates each element against a given schema. All elements in the array must conform to the same schema type.

      Type Parameters

      • Schema extends SchemaTypes

        The schema type for validating array elements

      Parameters

      • schema: Schema

        The schema to validate each array element against

      Returns VineArray<Schema>

      A VineArray schema instance

      vine.array(vine.string()) // Array of strings
      vine.array(vine.object({ id: vine.number() })) // Array of objects
    • Define a tuple schema with fixed length where each element can have its own validation schema.

      Type Parameters

      • Schema extends SchemaTypes[]

        Array of schema types for each tuple position

      Parameters

      • schemas: [...Schema[]]

        Array of schemas for each tuple element

      Returns VineTuple<
          Schema,
          { [K in string
          | number
          | symbol]: Schema[K<K>][typeof ITYPE] },
          { [K in string | number | symbol]: Schema[K<K>][typeof OTYPE] },
          { [K in string | number | symbol]: Schema[K<K>][typeof COTYPE] },
      >

      A VineTuple schema instance

      vine.tuple([
      vine.string(), // First element must be string
      vine.number(), // Second element must be number
      vine.boolean() // Third element must be boolean
      ])
    • Define a record (dictionary) schema with unknown string keys and values that conform to a specific schema type.

      Type Parameters

      • Schema extends SchemaTypes

        The schema type for validating record values

      Parameters

      • schema: Schema

        The schema to validate each record value against

      Returns VineRecord<Schema>

      A VineRecord schema instance

      vine.record(vine.string()) // { [key: string]: string }
      vine.record(vine.number()) // { [key: string]: number }
    • Define an enum schema that validates input against a predefined set of choices. Supports both array-based enums and TypeScript native enums.

      Type Parameters

      • const Values extends readonly unknown[]

        The enum values type (array or enum-like object)

      Parameters

      • values: Values | ((field: FieldContext) => Values)

        Array of allowed values, function returning values, or TypeScript enum

      Returns VineEnum<Values>

      VineEnum or VineNativeEnum schema instance

      // Array-based enum
      vine.enum(['red', 'green', 'blue'])

      // Dynamic enum with function
      vine.enum((field) => getUserRoles(field.meta.userId))

      // TypeScript enum
      enum Status { ACTIVE = 'active', INACTIVE = 'inactive' }
      vine.enum(Status)
    • Define an enum schema that validates input against a predefined set of choices. Supports both array-based enums and TypeScript native enums.

      Type Parameters

      • Values extends EnumLike

        The enum values type (array or enum-like object)

      Parameters

      • values: Values

        Array of allowed values, function returning values, or TypeScript enum

      Returns VineNativeEnum<Values>

      VineEnum or VineNativeEnum schema instance

      // Array-based enum
      vine.enum(['red', 'green', 'blue'])

      // Dynamic enum with function
      vine.enum((field) => getUserRoles(field.meta.userId))

      // TypeScript enum
      enum Status { ACTIVE = 'active', INACTIVE = 'inactive' }
      vine.enum(Status)
    • Define a schema that accepts any value without validation. Use sparingly as it bypasses type safety and validation.

      Returns VineAny

      A VineAny schema instance

      vine.any() // Accepts any value: string, number, object, etc.
      
    • Define a union of unique schema types with intelligent type discrimination. Unlike regular unions, this validates against the most specific matching schema using runtime type checking.

      Type Parameters

      • Schema extends SchemaTypes

        The schema types in the union

      Parameters

      • schemas: Schema[]

        Array of distinct schema types for the union

      Returns VineUnionOfTypes<Schema>

      A VineUnionOfTypes schema instance

      When schemas are not compatible or duplicated

      vine.unionOfTypes([
      vine.string(),
      vine.number(),
      vine.boolean()
      ])

      // For objects with discriminator fields
      vine.unionOfTypes([
      vine.object({ type: vine.literal('user'), name: vine.string() }),
      vine.object({ type: vine.literal('admin'), permissions: vine.array(vine.string()) })
      ])
    • Define a schema that validates native File instances. Useful for file upload validation in web applications.

      Returns VineNativeFile

      A VineNativeFile schema instance

      vine.nativeFile()
      .sizeLimit({ size: '2mb' })
      .extnames(['jpg', 'png', 'gif'])