import { z } from 'zod'
import { zx } from '@traversable/zod'
const Address = z.object({
street1: z.string(),
street2: z.optional(z.string()),
city: z.string(),
})
const addressEquals = zx.deepEqual(Address)
addressEquals(
{ street1: '221B Baker St', city: 'London' },
{ street1: '221B Baker St', city: 'London' }
) // => true
addressEquals(
{ street1: '221B Baker St', city: 'London' },
{ street1: '4 Privet Dr', city: 'Little Whinging' }
) // => false
zx.deepEqualDerive a "deep equal" function from a zod schema (v4, classic).
A "deep equal" function" (see also,
Equal) is similar to lodash'sisEqualfunction, except more performant, because when the shape of the values being compared is known ahead of time, we can optimize ahead of time, and only check what's necessary.Note that the deep equal generated by
zx.deepEqualassumes that both values have already been validated. Passing unvalidated values to the function might result in undefined behavior.See also:
zx.deepEqual.writeablezx.deepEqual.classic