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

Use only one value for the enum type of a component (dynamic zone) #10595

Closed
wants to merge 1 commit into from

Conversation

baptadn
Copy link

@baptadn baptadn commented Jul 10, 2021

What does it do?

Use only the current component name in the enum instead of all component names. Enum with one value describe a constant parameter (see https://swagger.io/docs/specification/describing-parameters/)

You can define a constant parameter as a required parameter with only one possible value

Blocks config example:

image

Example of generated code in the documentation, before/after:

{
  "cmsBlocks": {
    "type": "array",
    "items": {
      "oneOf": [
        {
          "properties": {
            "__component": {
              "type": "string",
              "enum": [
                "pages.hero-block"
-               "pages.title-block",
-               "pages.social-block"
              ]
            },
            "id": {
              "type": "string"
            },
            "title": {
              "type": "string"
            }
          },
          "required": [
            "id"
          ]
        },
        {
          "properties": {
            "__component": {
              "type": "string",
              "enum": [
-               "pages.hero-block",
                "pages.title-block",
-               "pages.social-block"
              ]
            },
            "id": {
              "type": "string"
            },
            "title": {
              "type": "string"
            }
          },
          "required": [
            "id"
          ]
        },
        {
          "properties": {
            "__component": {
              "type": "string",
              "enum": [
-              "pages.hero-block",
-              "pages.title-block",
                "pages.social-block"
              ]
            },
            "id": {
              "type": "string"
            },
            "facebookLink": {
              "type": "string"
            },
            "twitterLink": {
              "type": "string"
            }
          },
          "required": [
            "id"
          ]
        }
      ]
    }
  }
}

Why is it needed?

That allows us to get the right TypeScript types when using librairies like swagger-typescript-api:

const BlockRenderer = ({ page }: Props) => {
  return (
    <>
      {page.cmsBlocks.map((block, index) => {
        if (block.__component === 'pages.hero-block') {
          return (
            <HeroBlock
              key={index}
              title={block.title}             
            />
          )
        }
        if (block.__component === 'pages.social-block') {
          return (
            <SocialBlock
              key={index}
              facebookLink={block.facebookLink}
              twitterLink={block.twitterLink}           
            />
          )
        }
    </>
  )
}

Generated types before / after:

 cmsBlocks?: (
    | { __component?: "pages.hero-block" | "pages.title-block" | "pages.social-block"; id: string; title?: string }
    | { __component?: "pages.hero-block" | "pages.title-block" | "pages.social-block"; id: string; title?: string }
    | {
        __component?: "pages.hero-block" | "pages.title-block" | "pages.social-block";
        id: string;
        facebookLink?: string;
        twitterLink?: string;
      }
  )[];
 cmsBlocks?: (
    | { __component?: "pages.hero-block"; id: string; title?: string }
    | { __component?: "pages.title-block"; id: string; title?: string }
    | { __component?: "pages.social-block"; id: string; facebookLink?: string; twitterLink?: string }
  )[];

@strapi-cla
Copy link

strapi-cla commented Jul 10, 2021

CLA assistant check
All committers have signed the CLA.

@derrickmehaffy
Copy link
Member

Hi 👋
We now have released the v4. As of now, the v3 is in maintenance mode for 6 months. We will only be fixing critical and security issues on v3 from now on.
I will go ahead and close this PR. Thank you for contributing to Strapi 🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants