πŸ‘€ Our most exciting product launch yet πŸš€ Join us May 8th for Sanity Connect

Issue with required validation field in Sanity schema extraction and type generation.

11 replies
Last updated: Apr 2, 2024
User
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
, just tested it locally and it worked πŸ€”
Apr 2, 2024, 4:18 PM
so here is my groq

    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,
    });
and here is the full type


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
oh, wait. Mixing bugs/discussions here!
Apr 2, 2024, 4:21 PM
You need to run
sanity schema extract
with the
--enforce-required-fields
flag, then you can run
sanity typegen generate
Apr 2, 2024, 4:21 PM
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
oh, the docs are wrong! I'll get that sorted
Apr 2, 2024, 4:22 PM
yup, that fixed it and makes more sense now
Apr 2, 2024, 4:23 PM
i was wondering if i misunderstood how the schema was working lol
Apr 2, 2024, 4:23 PM
seemed weird that field was set as optional
Apr 2, 2024, 4:24 PM
 _rev: string;
  pageData: PageData;
  prismicData?: PrismicData;
ok we are cooking with fire, thanks! this is so much nicer than manually creating this with zod lol
Apr 2, 2024, 4:25 PM
Great to hear πŸ˜„
Apr 2, 2024, 6:09 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.

Was this answer helpful?