Using objects like literals #5026
Replies: 2 comments 1 reply
|
Hi @Bugrabugra! I'm Dosu and I’m helping the zod team. To enforce that exactly one of const VehicleCommand = z.strictObject({
_id: _ID,
status: Status,
audit: AuditSchema,
vehicle: ID,
command: Command,
});
const ForkliftCommand = z.strictObject({
_id: _ID,
status: Status,
audit: AuditSchema,
forklift: ID,
command: Command,
});
const GatewayCommand = z.strictObject({
_id: _ID,
status: Status,
audit: AuditSchema,
gateway: ID,
command: Command,
});
const CommandSchema = z.union([
VehicleCommand,
ForkliftCommand,
GatewayCommand,
]);This pattern enforces mutual exclusivity at the key level—if an object has more than one of If you still can't see the If this solves your problem, please close the issue. To reply, just mention @dosu. Share context across your team and agents. Try Dosu. |
|
I have problems with intellisense. Your solution didn't solve my problem. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hello there,
We are migrating our codebase to TS and we will use Zod 4. I am creating our schemas for forms and backend responses. Here I got stuck.
In one of our GET endpoint, the result is like this:
Here what I do:
My aim was if I create an object from Command, when I parse it, I would like to see vehicle, forklift or gateway object too. A response should have ONLY ONE of these objects ({vehicle: {_id: string}, command: string} | {forklift: {_id: string}, command: string} | {gateway: {_id: string}, command: string}) and the other base schema (_id, status, audit). But when I do this:
and put "." after obj in console.log, I cannot see vehicle object. How can I force my schema to have ONE of these CommandValidationSchema items (vehicle or forklift or gateway) ?
Thank you in advance!
All reactions