Array of objects with some of them literals
See original GitHub issueHow are you? I’m thinking about the following validation with zod and I have no clue on how to do it (or if it’s possible with zod). I want an array of objects, all with the same shape of course with some of them with literal props, I need these always present in the array.
Example: I need always in the array the objects those with name required1 and required2, and then other objects optionals following the same shape.
[
{
name: z.literal('required1'),
otherprop: z.number()
},
{
name: z.literal('required2'),
otherprop: z.number()
},
// I want to include one or more of the following too.
{
name: z.string(),
otherprop: z.number()
},
]
This other example will throw because required2 is missing
[
{
name: z.literal('required1'),
otherprop: z.number()
},
// I want to include one or more of the following too.
{
name: z.string(),
otherprop: z.number()
},
]
Any clue?
Thanks.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Top Results From Across the Web
Is there a way to have an array of objects with some of them ...
I want an array of objects, all with the same shape, with some of them with literal props, I need these always present...
Read more >JavaScript Fundamentals: Object Literals and Nested Arrays
An associative array indexes elements differently; usually with a word for each slot, making it far more structured and readable. Unfortunately, ...
Read more >JavaScript Literals - w3resource
In Javascript, an array literal is a list of expressions, each of which represents an array element, enclosed in a pair of square...
Read more >JavaScript Object Literals Simplified - Standardista
The Object literal notation is basically an array of key:value pairs, with a colon separating the keys and values, and a comma after...
Read more >Data Structures: Objects and Arrays - Eloquent JavaScript
Arrays, then, are just a kind of object specialized for storing sequences of things. If you evaluate typeof [] , it produces "object"...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m going to close this, but feel free to post your solution here when you’re done if you come up with something that you like!
I solved it doing the following (simplified here)
Thanks!