Skip to content
This repository has been archived by the owner on May 6, 2023. It is now read-only.

Possible to pick/omit? #210

Open
Kolby-Udacity opened this issue Apr 11, 2023 · 8 comments
Open

Possible to pick/omit? #210

Kolby-Udacity opened this issue Apr 11, 2023 · 8 comments

Comments

@Kolby-Udacity
Copy link

Kolby-Udacity commented Apr 11, 2023

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

@saiichihashimoto
Copy link
Owner

const zodOfDocument: ZodType = s.document({ ... }).zod;

// Do what you want with zodOfDocument

@Kolby-Udacity
Copy link
Author

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 ...

@saiichihashimoto
Copy link
Owner

can you show me a repro? Your code snippet has programSanitySchema.pick({title: true}) which is incorrect.

@Kolby-Udacity
Copy link
Author

@saiichihashimoto
Copy link
Owner

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!

@Kolby-Udacity
Copy link
Author

That would be great, thank you. Awesome library btw!

@saiichihashimoto
Copy link
Owner

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.

@Kolby-Udacity
Copy link
Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants