Issue with required validation field in Sanity schema extraction and type generation.
11 replies
Last updated: Apr 2, 2024
J
User
in short though it looks like when i extract the schema it looks like it might be ignoring the required validation field? or im misunderstanding the reason i get
user Y
i'll move this convo here: for reference: https://sanity-io-land.slack.com/archives/C9Z7RC3V1/p1712014795716969 in short though it looks like when i extract the schema it looks like it might be ignoring the required validation field? or im misunderstanding the reason i get
pageData: _PageData_ | _null_;on a field that is required
Apr 2, 2024, 4:14 PM
user V
What's the output of sanity schema extract --enforce-required-fields
Apr 2, 2024, 4:18 PM
J
so here is my groq
and here is the full type
const getPageData = groq`*[_type in ['product', 'page', 'blogPost'] && pageData.slug.current == $slug][0]{ _type == 'product' => { ..., pageData }, _type == 'page' => { ..., pageData, }, _type == 'blogPost' => { ..., pageData } }`; const pageData = await client.fetch<GetPageDataResult>(getPageData, { slug, });
export type GetPageDataResult = { _id: string; _type: "blogPost"; _createdAt: string; _updatedAt: string; _rev: string; pageData: PageData | null; prismicData?: PrismicData; author?: { _ref: string; _type: "reference"; _weak?: boolean; [internalGroqTypeReferenceTo]?: "blogAuthor"; }; category?: { _ref: string; _type: "reference"; _weak?: boolean; [internalGroqTypeReferenceTo]?: "blogCategory"; }; publishedAt?: string; content?: RichText; } | { _id: string; _type: "page"; _createdAt: string; _updatedAt: string; _rev: string; pageData: PageData | null; prismicData?: PrismicData; content?: Array<({ _key: string; } & CodeComponent) | ({ _key: string; } & CollectionComponent) | ({ _key: string; } & CtaImageComponent) | ({ _key: string; } & CtaTemplateComponent) | ({ _key: string; } & CtaVideoComponent) | ({ _key: string; } & FaqComponent) | ({ _key: string; } & HeaderComponent) | ({ _key: string; } & HeroComponent) | ({ _key: string; } & ImageGalleryComponent) | ({ _key: string; } & LinkComponent) | ({ _key: string; } & ListsComponent) | ({ _key: string; } & ProductHeroComponent) | ({ _key: string; } & ReviewsComponent) | ({ _key: string; } & RichTextComponent) | ({ _key: string; } & RootCollectionComponent) | ({ _key: string; } & YotpoImageGalleryComponent) | ({ _key: string; } & YoutubeVideoComponent)>; } | { _id: string; _type: "product"; _createdAt: string; _updatedAt: string; _rev: string; pageData: PageData | null; prismicData?: PrismicData; linkedProduct?: LinkedProduct; description?: Array<{ children?: Array<{ marks?: Array<string>; text?: string; _type: "span"; _key: string; }>; style?: "blockquote" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "normal"; listItem?: "bullet" | "number"; markDefs?: Array<{ href?: string; _type: "link"; _key: string; }>; level?: number; _type: "block"; _key: string; }>; details?: Array<{ children?: Array<{ marks?: Array<string>; text?: string; _type: "span"; _key: string; }>; style?: "blockquote" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "normal"; listItem?: "bullet" | "number"; markDefs?: Array<{ href?: string; _type: "link"; _key: string; }>; level?: number; _type: "block"; _key: string; }>; templateId?: string; hasFrontImage?: boolean; hasBackImage?: boolean; hasSideImage?: boolean; productImages?: Array<{ asset?: { _ref: string; _type: "reference"; _weak?: boolean; [internalGroqTypeReferenceTo]?: "sanity.imageAsset"; }; hotspot?: SanityImageHotspot; crop?: SanityImageCrop; _type: "image"; _key: string; }>; productVariants?: Array<{ _ref: string; _type: "reference"; _weak?: boolean; _key: string; [internalGroqTypeReferenceTo]?: "productVariant"; }>; } | null;
Apr 2, 2024, 4:20 PM
You need to run
sanity schema extractwith the
--enforce-required-fieldsflag, then you can run
sanity typegen generate
Apr 2, 2024, 4:21 PM
J
ah let me give that a go, might need to clear that up in the docs, unless i read them wrong (which is a possibility lol)
Apr 2, 2024, 4:22 PM
J
yup, that fixed it and makes more sense now
Apr 2, 2024, 4:23 PM
J
i was wondering if i misunderstood how the schema was working lol
Apr 2, 2024, 4:23 PM
J
seemed weird that field was set as optional
Apr 2, 2024, 4:24 PM
J
_rev: string; pageData: PageData; prismicData?: PrismicData;
Apr 2, 2024, 4:25 PM
Sanityā build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.