sanity-io / sanity

Sanity Studio – Rapidly configure content workspaces powered by structured content
https://www.sanity.io
MIT License
5.28k stars 427 forks source link

Schema extract fails if "flat" custom type #7801

Open heggemsnes opened 3 days ago

heggemsnes commented 3 days ago

Describe the bug

Using a field like

defineType({
    name: 'externalLink',
    type: 'url',
    title: 'External Link',
  }),

Somewhere directly like:

defineType({
    name: 'pageWithExternalLink',
    type: 'document',
    title: 'Page With External Link',
    fields: [
      {
        name: 'title',
        type: 'string',
        title: 'Title',
      },
      {
        name: 'link',
        type: 'externalLink',
        title: 'External Link',
      },
    ],
  }),

Causes error on schema extract

> npm run extract-types

> starter-template@1.0.0 extract-types
> sanity schema extract --enforce-required-fields

❌️ Failed to extract schema, with enforced required fields

Error: Type "externalLink" not found
    at convertSchemaType (~/Documents/GitHub/sanity-template-nextjs-clean/studio/node_modules/@sanity/schema/lib/_internal.js:120:11)
    at createObject (~/Documents/GitHub/sanity-template-nextjs-clean/studio/node_modules/@sanity/schema/lib/_internal.js:125:63)
    at convertBaseType (~/Documents/GitHub/sanity-template-nextjs-clean/studio/node_modules/@sanity/schema/lib/_internal.js:64:83)
    at ~/Documents/GitHub/sanity-template-nextjs-clean/studio/node_modules/@sanity/schema/lib/_internal.js:58:18
    at Array.forEach (<anonymous>)
    at Object.extractSchema (~/Documents/GitHub/sanity-template-nextjs-clean/studio/node_modules/@sanity/schema/lib/_internal.js:54:33)
    at main (~/Documents/GitHub/sanity-template-nextjs-clean/studio/node_modules/sanity/lib/_internal/cli/threads/extractSchema.js:10:195)

To Reproduce

Steps to reproduce the behavior:

Check my forked repo from the clean next.js template: https://github.com/heggemsnes/sanity-template-nextjs-clean

I've changed the schemaTypes to the examples before.

Add a valid project ID and run npm run extract-types

Expected behavior

This is valid Sanity code (I think?) and works fine in the studio. Useful for reusable validations etc.

Which versions of Sanity are you using?

@sanity/cli (global) 3.62.0 (latest: 3.63.0) @sanity/assist 3.0.8 (up to date) @sanity/eslint-config-studio 4.0.0 (up to date) @sanity/icons 3.4.0 (up to date) @sanity/vision 3.61.0 (latest: 3.63.0) sanity 3.62.2 (latest: 3.63.0)

What operating system are you using?

MacOS 14.3.1

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

10.2.4 v20.11.0

basharalidreesi commented 2 days ago

I've encountered this problem as well, but with a boolean type. To make TypeGen work, I had to comment out all instances of this custom type, run the TypeGen commands, then manually add the correct type definitions to the output file (which defeats the purpose).