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.deepEqual.classic
Derive an in-memory "deep equal" function from a zod schema (v4, classic).
A "deep equal" function (see also, Equal
) is similar to
Lodash's deepEquals
function, 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 "deepEqual function" generated by zx.deepEqual
assumes that both values have already been validated. Passing
unvalidated values to the function might result in undefined behavior.
See also:
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.deepEqual.writeable
Derive a writeable (stringified) "deep equal" function from a zod schema (v4, classic).
A "deep equal" function (see also, Equal
) is similar to
Lodash's isEqual
function, 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 function generated by zx.deepEqual
assumes that both values have already been validated. Passing
unvalidated values to the function might result in undefined behavior.
zx.deepEqual.writeable
accepts an optional
configuration object as its second argument; documentation for those
options are available via hover on autocompletion.
See also:
Optional
options: Optionsimport { z } from 'zod'
import { zx } from '@traversable/zod'
const deepEqual = zx.deepEqual.writeable(
z.object({
street1: z.string(),
street2: z.optional(z.string()),
city: z.string(),
}),
{ typeName: 'Address' }
)
console.log(deepEqual)
// =>
// type Address = { street1: string; street2?: string; city: string; }
// function deepEqual(x: Address, y: Address) {
// if (x === y) return true;
// if (x.street1 !== y.street1) return false;
// if (x.street2 !== y.street2) return false;
// if (x.city !== y.city) return false;
// return true;
// }
zx.deepEqual
Derive a "deep equal" function from a zod schema (v4, classic).
A "deep equal" function" (see also,
Equal
) is similar to lodash'sisEqual
function, 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.deepEqual
assumes that both values have already been validated. Passing unvalidated values to the function might result in undefined behavior.See also:
zx.deepEqual.writeable
zx.deepEqual.classic