ricokahler / sanity-codegen

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

Error if using along with seo tools plugin #55

Closed antonmihaylov closed 3 years ago

antonmihaylov commented 3 years ago

Hello, first of all, this is a great tool that you have here, It can be very useful for using Sanity with Typescript!

I understand that right now there is no support for additional types, which is not the problem I'm facing.

When I use the SEO plugin I need to include a "seo-tools" type in the document, like this:

{
      name: "seo",
      title: "SEO",
      type: "seo-tools",
      options: {...},
}

It seems that the generator doesn't like that naming and throws

Error: Name "seo-tools" is not valid. Ensure camel case, alphanumeric and underscore characters only

I'm not very familiar if snake case is a valid type name, but since SEO tools are using it, I'm guessing it is. Would you consider adding this to the validation rules?

ricokahler commented 3 years ago

This one is kind of interesting actually. Looking at the sanity docs for object names, they actually explicitly say that fields cannot include a hyphen.

Field names

A field name must start with a letter from a-z, and can can only include:

  • Letters
  • Numbers
  • Underscores

This means field names can't contain hyphens. We also recommend using the camel case naming convention for field names.

https://www.sanity.io/docs/object-type#field-names-9a9a8b5c6404


I mean it works and that's a sanity-backed plugin so I guess those same rules don't apply to document names?

Let me think on this for a sec. Thanks for bringing this to my attention!

antonmihaylov commented 3 years ago

Maybe I misunderstand it but isn't this for the object's name. Like the name: "seo" part of the snippet above. Not the actual type of the document

ricokahler commented 3 years ago

Oh sorry I forgot some context. I followed the link you sent to the seo-tools repo and my confusion (and maybe this is just me complaining 😅) is that Sanity disallows hyphens in object field names but allows it in document names.

Referring to this:

https://github.com/LiamMartens/sanity-plugin-seo-tools/blob/88c116fc24cf41d86d584d1a506532ed7c3ec43b/src/schema/index.tsx#L4

antonmihaylov commented 3 years ago

Ohh, yeah, that makes sense now. It's not an official plugin, but it's "Featured" on their website. And as you said - it works, so I'm guessing the naming is legit.

Perhaps they disallow it only in the fields? E.g. this is ok:

{
  type: 'object',
  name: 'this-is-a-person',
  fields: [
    {
      title: 'Name',
      name: 'name',
      type: 'string'
    }
  ]
}

This is not:

{
  type: 'object',
  name: 'this-is-a-person',
  fields: [
    {
      title: 'Name',
      name: 'this-is-a-name',
      type: 'string'
    }
  ]
}
ricokahler commented 3 years ago

https://github.com/ricokahler/sanity-codegen/pull/61#issuecomment-762434472

nirjan-dev commented 3 years ago

@ricokahler I'm still getting an issue when using the SEO plugin with 0.8.2

"sanity-codegen": "^0.8.2"
import path from 'path';
import { SanityCodegenConfig } from 'sanity-codegen';

const config: SanityCodegenConfig = {
  schemaPath: path.resolve(
    __dirname,
    '../../../universal-content/schemas/schema.js'
  ),
  outputPath: path.resolve(__dirname, './schema.ts'),
  babelOptions: {
    ignore: [], // <-- this resets the ignore option (but makes codegen super slow)
  },
};

export default config;
Error: Name "seo-tools" is not valid. Ensure camel case, alphanumeric, and underscore characters only
    at validatePropertyName (/home/nirjan/work/digital-media/manifold/packages/manifold-services/node_modules/sanity-codegen/generate-types.js:14:11)
    at getTypeName (/home/nirjan/work/digital-media/manifold/packages/manifold-services/node_modules/sanity-codegen/generate-types.js:48:5)
    at convertType (/home/nirjan/work/digital-media/manifold/packages/manifold-services/node_modules/sanity-codegen/generate-types.js:164:12)
    at /home/nirjan/work/digital-media/manifold/packages/manifold-services/node_modules/sanity-codegen/generate-types.js:251:13
    at Array.map (<anonymous>)
    at generateTypes (/home/nirjan/work/digital-media/manifold/packages/manifold-services/node_modules/sanity-codegen/generate-types.js:247:116)
    at cli (/home/nirjan/work/digital-media/manifold/packages/manifold-services/node_modules/sanity-codegen/cli.js:81:51)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed.
Exit code: 1
Command: /home/nirjan/.local/share/nvm/v14.15.3/bin/node
Arguments: /usr/share/yarn/lib/cli.js generate:types
Directory: /home/nirjan/work/digital-media/manifold/packages/manifold-services
Output:
ricokahler commented 3 years ago

@NK-WebDev let me know how v0.8.3 works for you

nirjan-dev commented 3 years ago

@ricokahler Thank you for all your hard work and quick response ❤️ It's working for me now.

I did get this log, but I don't think It's gonna be a problem for our project right now

Could not find types for: "SeoTools", "Table". Ensure they are present in your schema. Future versions of sanity-codegen will allow you to type them separately.
[SanityCodeGen]: types written out to /home/nirjan/work/digital-media/manifold/packages/manifold-services/schema.ts)