Skip to content

Partial objects, shorthand for creating object validators, new vat rule, and global transforms

Choose a tag to compare

@github-actions github-actions released this 11 Dec 06:50

4.2.0 (2025-12-11)

Shorthand method to create object validators

Introduced vine.create() method to create validators directly from top-level objects (7960e28). As a result of this, you do not have to use vine.compile(vine.object({})), just vine.create({}) will give the same result.

  import vine from '@vinejs/vine'

  // Create a validator from a plain object schema
-  const validator = vine.compile(
-    vine.object({
-      username: vine.string().minLength(3),
-      password: vine.string().minLength(8),
-      terms: vine.boolean().isTrue()
-    })
-  )
+  const validator = vine.create({
+    username: vine.string().minLength(3),
+    password: vine.string().minLength(8),
+    terms: vine.boolean().isTrue()
+  })

  // Use the validator
  const data = await validator.validate({
    username: 'johndoe',
    password: 'secret123',
    terms: true
  })

Object Schema Enhancement

Added partial() method (initially named toOptional) to make all properties of an object schema optional (#127, 08a3472)

  import vine from '@vinejs/vine'

  // Define a base schema
  const createUserValidator = vine.create({
    name: vine.string(),
    email: vine.string().email(),
    age: vine.number()
  })

  // Make all properties optional
  const updateUserValidator = vine.create(
    createUserValidator.schema.partial()
  )

  // Now all fields are optional - useful for updates/patches
  const result = await updateUserValidator.validate({
    { email: 'user@example.com' }
  })

Global Transforms

Added support for global transforms to transform date objects globally across all validators (f368feb). This will allow AdonisJS projects to always return a Luxon DateTime object when validating dates.

  import { DateTime } from 'luxon'
  import { VineDate } from '@vinejs/vine'

  declare module '@vinejs/vine/types' {
    interface VineGlobalTransforms {
      date: DateTime
    }
  }

  VineDate.transform((value) => {
    return new DateTime.fromJSDate(value)
  })

  vine.create({
    birthDate: vine.date(), // instanceof DateTime
    createdAt: vine.date() // instanceof DateTime
  })

Bug Fixes

  • allow vine.nativeFile to work with vine.unionOfTypes b34f1b0
  • object toOptional types (#130) d28c949, closes #130

Features

  • add toOptional to object (#127) 08a3472, closes #127
  • add vine.create method to create validators from top-level objects 7960e28
  • introduce global transforms to transform the date objects globally f368feb
  • make validator schema public to allow cloning it 752acff
  • rename toOptional to partial b6e3461
  • rules: add strongPassword rule (#132) b443638, closes #132
  • rules: add vat rule (#131) 999b667, closes #131
  • update types and write types benchmarks 2fea19c

Reverts

What's Changed

New Contributors

Full Changelog: v4.1.0...v4.2.0