Replies: 3 comments 1 reply
-
It might help your question get answered if you were to add some code examples of what you are referring to or maybe a few links that further explain what you mean. |
Beta Was this translation helpful? Give feedback.
0 replies
-
You right. I should have provided an example. I would love for zod schemas to provide an equality function: const User = z.object({ firstName: z.string(), lastName: z.string() });
const user = { firstName: 'Paul', lastName: 'Chavard' };
User.eq(user, { firstName: 'Paul', lastName: 'Chavard' }); // => true
User.eq(user, { firstName: 'Jacob', lastName: 'Weisenburger' }); // => false
const users = [{ firstName: 'Jacob', lastName: 'Weisenburger' }, { firstName: 'Paul', lastName: 'Chavard' }];
users.sort(User.ord) // => [{ firstName: 'Paul', lastName: 'Chavard' }, { firstName: 'Jacob', lastName: 'Weisenburger' }] |
Beta Was this translation helpful? Give feedback.
1 reply
-
You can kinda do this already const User = z.object({ firstName: z.string(), lastName: z.string() });
const user = { firstName: 'Paul', lastName: 'Chavard' };
User.eq(user, { firstName: 'Paul', lastName: 'Chavard' }); // => true
User.eq(user, { firstName: 'Jacob', lastName: 'Weisenburger' }); // => false This would work const paulChavardSchema = z.object( { firstName: z.literal( 'Paul' ), lastName: z.literal( 'Chavard' ) } )
paulChavardSchema.safeParse( { firstName: 'Paul', lastName: 'Chavard' } ).success // => true
paulChavardSchema.safeParse( { firstName: 'Jacob', lastName: 'Weisenburger' } ).success // => false As far as this part, I'm not sure how sorting is related to structural equality. Please explain. const users = [{ firstName: 'Jacob', lastName: 'Weisenburger' }, { firstName: 'Paul', lastName: 'Chavard' }];
users.sort(User.ord) // => [{ firstName: 'Paul', lastName: 'Chavard' }, { firstName: 'Jacob', lastName: 'Weisenburger' }] |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
One feature I really miss from
io-ts
is the ability to generate structural equality helpers from schema. Is it somethingzod
might consider in the future?Beta Was this translation helpful? Give feedback.
All reactions