sanity-typed-schema-builder
sanity-typed-schema-builder copied to clipboard
Possible to pick/omit?
Hi, I'm trying to utilize my existing sanity-typed-schema-builder document to create a new Zod document. I want to pick fields from the former to add to the latter. Is this possible somehow?
Example:
export const programSanitySchema = s.document({
name: 'program',
title: 'Program',
fields: [
{
name: 'title',
title: 'Title',
description: 'The title of the program',
type: s.string(),
},
{
name: 'description',
title: 'Description',
description: 'The description of the program',
type: s.string(),
}
],
});
export const programPreviewZodSchema = z.object({
title: programSanitySchema.pick({title: true})
});
Thanks
const zodOfDocument: ZodType = s.document({ ... }).zod;
// Do what you want with zodOfDocument
const zodOfDocument: ZodType = s.document({ ... }).zod; // Do what you want with zodOfDocument
Thanks, I tried that, but TypeScript says:
Property 'pick' does not exist on type ZodType ...
can you show me a repro? Your code snippet has programSanitySchema.pick({title: true})
which is incorrect.
Here is a codesandbox: https://codesandbox.io/s/silent-thunder-7pg960?file=/src/App.tsx

ah, I see. the object returned is of type ZodType
, not ZodObject
, so it doesn't have the object specific methods. I think that's a change we can make!
That would be great, thank you. Awesome library btw!
FYI until we fix it, you should just be able to cast it into a ZodObject. I think it actually is a ZodObject so this shouldn’t be an issue.
Yeah I actually tried that for awhile the other day. I settled on using as ZodObject<any>
, which works, but doesn't give me the options when I use pick
. Not a huge problem while I wait for a fix, but I'm curious if there is a way to fix this. Seems the only way (other than recreating a Zod object) is as ZodObject<programSchema.zod>
, but that obviously doesnt work because if it did none of this would be a problem lol