ricokahler / sanity-codegen

Generate TypeScript types from your Sanity.io schemas
sanity-codegen-dev.vercel.app
MIT License
270 stars 19 forks source link

Property name validation breaks on Sanity official template #249

Open joeswann opened 2 years ago

joeswann commented 2 years ago

In the process of installing a reasonably official sanity template -- sanity-io/sanity-shopify-studio (https://github.com/sanity-io/sanity-shopify-studio) template the cli validation for document names appears to reject the templates own naming system (specifically the use of periods).

Error: Name "article.editorial" is not valid. Ensure camel case, alphanumeric, and underscore characters only
    at validatePropertyName (repro-sanity-codegen/node_modules/sanity-codegen/generate-types.js:14:11)
    at createTypeName (repro-sanity-codegen/node_modules/sanity-codegen/generate-types.js:36:5)
    at generateTypeForDocument (repro-sanity-codegen/node_modules/sanity-codegen/generate-types.js:233:24)
    at Array.map (<anonymous>)
    at generateTypes (repro-sanity-codegen/node_modules/sanity-codegen/generate-types.js:284:50)
    at cli (repro-sanity-codegen/node_modules/sanity-codegen/cli.js:102:51)

Documentation on the sanity website says that documents are objects and field names for objects don't have periods - so this appears to contradict that. There are also objects with names like "seo.product" - documentation at https://www.sanity.io/docs/object-type#9a9a8b5c6404

There also appears to be a related discussion at https://github.com/ricokahler/sanity-codegen/issues/55

It seems like the Sanity documentation is more likely to be out of date than the template is to be wrong(since the template works and has been created within the last three months), but this seems like a potential 'use other door' situation in terms of getting it resolved.

In the meantime - is there a way to override the allowPeriod validation without forking the entire repo?

joeswann commented 2 years ago

There is a reproduction of this issue at https://github.com/1of1studio/repro-sanity-codegen - the only customisation is to add an alias for a sanity ui component to no-op in order to get sanity-codegen to run.

Note: Will require a projectId in sanity.json to run sanity of course

joeswann commented 2 years ago

Note: Have opened a discussion on the other repo too -- https://github.com/sanity-io/sanity-shopify-studio/issues/4

joeswann commented 2 years ago

Update: After some discussion on the other repo (link in previous comment) it appears that according to them periods should be fine in these cases and that their use of periods is not the cause of the issue.

Hopefully this isn't a use other door situation in the making!

joeswann commented 2 years ago

Hoping to get some feedback on this if possible!