Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeGen marks assets of images inside arrays as optional, even with assetRequired() #6773

Open
maxijonson opened this issue May 25, 2024 · 0 comments
Labels
bug KTLO typegen Issues related to TypeScript types generation

Comments

@maxijonson
Copy link

maxijonson commented May 25, 2024

Describe the bug

Adding a Rule.required().assetRequired() validation works as expected when the image is a field (in an object). However, when it's an array member, this validation is not respected by TypeGen. Instead, it is always marked as optional (asset?: ...)

To Reproduce

The following schema:

import { defineField, defineType, defineArrayMember } from "sanity";
 
const imageField = defineField({
  type: "image",
  name: "image",
  title: "Image",
  validation: (rule) => [rule.required().assetRequired()],
});
 
export default defineType({
  name: "homeHero",
  title: "Hero",
  type: "object",
  fields: [
    imageField,
    defineField({
      name: "logos",
      title: "Logos",
      type: "array",
      of: [defineArrayMember(imageField)],
      validation: (rule) => [rule.required()],
    }),
  ]
});

Running:

sanity schema extract --path './sanity/generated/.sanity-generated.schema.json' --enforce-required-fields --workspace=staging && sanity typegen generate

Produces the following type:

export type HomeHero = {
  _type: "homeHero";
  image: {
    asset: {
      _ref: string;
      _type: "reference";
      _weak?: boolean;
      [internalGroqTypeReferenceTo]?: "sanity.imageAsset";
    };
    hotspot?: SanityImageHotspot;
    crop?: SanityImageCrop;
    _type: "image";
  };
  logos: Array<{
    asset?: {
      _ref: string;
      _type: "reference";
      _weak?: boolean;
      [internalGroqTypeReferenceTo]?: "sanity.imageAsset";
    };
    hotspot?: SanityImageHotspot;
    crop?: SanityImageCrop;
    _type: "image";
    _key: string;
  }>;
};

As you can observe, although both images are using the exact same field, only the one in the array (logos) has its asset marked as optional with asset?.

Expected behavior

TypeGen should respect the rule.required().assetRequired() on assets of images that are inside arrays.

Which versions of Sanity are you using?

@sanity/cli (global)        3.43.0 (up to date)
@sanity/asset-utils          1.3.0 (up to date)
@sanity/cli                 3.43.0 (up to date)
@sanity/code-input           4.1.4 (up to date)
@sanity/color-input          3.1.1 (up to date)
@sanity/icons               2.11.8 (up to date)
@sanity/image-url            1.0.2 (up to date)
@sanity/preview-url-secret  1.6.13 (up to date)
@sanity/ui                  2.1.12 (up to date)
@sanity/vision              3.43.0 (up to date)
sanity                      3.43.0 (up to date)

Not sure why @sanity/cli is marked as global here. I am using Volta, not sure if that's any help. Running npm list -g:

├── corepack@0.23.0
└── npm@10.2.4

What operating system are you using?

ProductName:            macOS
ProductVersion:         14.4.1
BuildVersion:           23E224

Which versions of Node.js / npm are you running?

10.2.4 # NPM
v20.11.1 # Node
@sgulseth sgulseth added bug typegen Issues related to TypeScript types generation labels May 27, 2024 — with Linear
@sgulseth sgulseth added the KTLO label Jun 10, 2024 — with Linear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug KTLO typegen Issues related to TypeScript types generation
Projects
None yet
Development

No branches or pull requests

2 participants