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

    Class VineObject<Properties, Input, Output, CamelCaseOutput>

    VineObject represents an object value in the validation schema. It validates objects with predefined properties, supports conditional groups, and provides control over unknown properties.

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

    const result = await vine.validate({
    schema,
    data: { name: 'John', email: 'john@example.com', age: 30 }
    })

    Type Parameters

    • Properties extends Record<string, SchemaTypes>

      Record of property names to their schema types

    • Input

      The expected input type for this object

    • Output

      The output type after validation and transformation

    • CamelCaseOutput

      The output type with camelCase property names

    Hierarchy (View Summary)

    Implements

    Index

    Constructors

    Properties

    "[ITYPE]": Input

    Define the input type of the schema for TypeScript inference

    "[OTYPE]": Output

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

    "[COTYPE]": CamelCaseOutput

    Type marker for camelCase output type inference

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

    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 with.

      Parameters

      • callback: ParseFn

        Parser function to transform the input value

      Returns this

      This schema instance for method chaining

      vine.string().parse((value) => {
      return typeof value === 'string' ? value.trim() : value
      })
    • Type checker function to determine if a value is an object. Required for "unionOfTypes" functionality.

      Parameters

      • value: unknown

        The value to check

      Returns boolean

      True if the value is a non-null object and not an array

    • Returns a cloned copy of all object properties with their validation schemas. Note: Object groups are not included to keep implementations simple.

      Returns Properties

      Cloned properties record

      const properties = schema.getProperties()
      
    • Returns a cloned subset of object properties containing only the specified keys.

      Type Parameters

      • Keys extends string | number | symbol

      Parameters

      • keys: Keys[] | readonly Keys[]

        Array of property keys to include

      Returns Pick<Properties, Keys>

      Picked properties record

      const userSchema = vine.object({
      name: vine.string(),
      email: vine.string().email(),
      password: vine.string()
      })

      const publicFields = userSchema.pick(['name', 'email'])
    • Returns a cloned copy of object properties excluding the specified keys.

      Type Parameters

      • Keys extends string | number | symbol

      Parameters

      • keys: Keys[] | readonly Keys[]

        Array of property keys to exclude

      Returns Omit<Properties, Keys>

      Omitted properties record

      const userSchema = vine.object({
      name: vine.string(),
      email: vine.string().email(),
      password: vine.string()
      })

      const withoutPassword = userSchema.omit(['password'])
    • Allows unknown properties to pass through validation and be included in the output. By default, objects with properties not defined in the schema will fail validation.

      Type Parameters

      • Value

      Returns VineObject<
          Properties,
          Input & { [K: string]: Value },
          Output & { [K: string]: Value },
          CamelCaseOutput & { [K: string]: Value },
      >

      This object schema with unknown properties allowed

      const schema = vine.object({
      name: vine.string()
      }).allowUnknownProperties()

      // Now { name: 'John', extra: 'value' } will pass validation
    • Clones the VineObject schema including all properties, validations, groups, and options.

      Returns this

      A cloned instance of this VineObject schema

    • Creates a new object schema with all properties (or specified properties) marked as optional. This is useful for update/PATCH operations where not all fields are required.

      Type Parameters

      Parameters

      • Optionalkeys: Keys[] | readonly Keys[]

        Optional array of property keys to make optional. If omitted, all properties become optional.

      Returns VineObject<
          { [K in string
          | number
          | symbol]: T[K] },
          {
              [K in string | number | symbol]: (
                  {
                      [K in string
                      | number
                      | symbol]?: { [K in string | number | symbol]: T[K][typeof ITYPE] }[K]
                  } & {
                      [K in string
                      | number
                      | symbol]: { [K in string | number | symbol]: T[K][typeof ITYPE] }[K]
                  }
              )[K]
          },
          {
              [K in string
              | number
              | symbol]: (
                  {
                      [K in string
                      | number
                      | symbol]?: { [K in string | number | symbol]: T[K][typeof OTYPE] }[K]
                  } & {
                      [K in string
                      | number
                      | symbol]: { [K in string | number | symbol]: T[K][typeof OTYPE] }[K]
                  }
              )[K]
          },
          {
              [K in string
              | number
              | symbol]: (
                  {
                      [K in string
                      | number
                      | symbol]?: {
                          [K in string | number | symbol as CamelCase<K & string>]: T[K][typeof COTYPE]
                      }[K]
                  } & {
                      [K in string
                      | number
                      | symbol]: {
                          [K in string | number | symbol as CamelCase<K & string>]: T[K][typeof COTYPE]
                      }[K]
                  }
              )[K]
          },
      >

      A new VineObject schema with optional properties

      // Make all properties optional
      const updateSchema = createSchema.partial()
      // Make only specific properties optional
      const userSchema = vine.object({
      name: vine.string(),
      email: vine.string().email(),
      age: vine.number()
      })

      const updateUserSchema = userSchema.partial(['name', 'age'])
      // email remains required, name and age become optional
    • Compiles the schema type to a compiler node for validation.

      Parameters

      • propertyName: string

        Name of the property being compiled

      • refs: RefsStore

        Reference store for the compiler

      • options: ParserOptions

        Parser options

      Returns ObjectNode

      Compiled object node for validation