zod
zod copied to clipboard
Question: How to extend/copy array schema but only change the containing type
Hello
I have a question regarding arrays.
Given following simple example
const myObjSchema = z.object({
name: z.string(),
});
const arrayOfMyObj = z.array(myObjSchema).min(1).max(2);
What i wanna do is kind of changing the containing type of the array like this.
const arrayOfMyObjExt = arrayOfMyObj.changeType(myObjSchema.extend({ foo:z.string()}));
but i want to keep the constraints of min and max of the original array.
Why do i need this?
I want to extend and existing schema with some OpenAPI documentation and i do not have access to the original schema definitions. I want to keep as much from the original schema as possible to not run out of sync. For z.object i can use extends but there is nothing like this for z.array.
Any hints?
Thank you so much for help.