import * as T from '@sinclair/typebox'
import { deepEqual } from '@traversable/typebox'
const addressEquals = deepEqual.writeable(
T.Object({
street1: T.String(),
street2: T.Optional(T.String()),
city: T.String(),
}),
{ typeName: 'Address' }
)
console.log(addressEquals)
// =>
// 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;
// }
box.deepEqual.writeableDerive a "writeable" (stringified) "deep equal" function from a Json Schema spec.
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 box.deepEqual
assumes that both values have already been validated. Passing
unvalidated values to the function might result in undefined behavior.
box.deepEqual.writeable accepts an optional
configuration object as its second argument; documentation for those
options are available via hover on autocompletion.
See also:
import * as T from '@sinclair/typebox'
import { deepEqual } from '@traversable/typebox'
const addressEquals = deepEqual.writeable(
T.Object({
street1: T.String(),
street2: T.Optional(T.String()),
city: T.String(),
}),
{ typeName: 'Address' }
)
console.log(addressEquals)
// =>
// 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;
// }
box.deepEqual.writeableDerive a "writeable" (stringified) "deep equal" function from a Json Schema spec.
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 function generated by
box.deepEqualassumes that both values have already been validated. Passing unvalidated values to the function might result in undefined behavior.box.deepEqual.writeableaccepts an optional configuration object as its second argument; documentation for those options are available via hover on autocompletion.See also:
box.deepEqual