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

🐛 Bug Report: Regression scaffolder action with combided zod schemas fails validation #17721

Closed
2 tasks done
Andy2003 opened this issue May 9, 2023 · 1 comment · Fixed by #17749
Closed
2 tasks done
Labels
area:scaffolder Everything and all things related to the scaffolder project area bug Something isn't working

Comments

@Andy2003
Copy link
Contributor

Andy2003 commented May 9, 2023

📜 Description

Given the schema (adjusted from tests):

      createTemplateAction({
        id: 'jest-zod-validated-action',
        description: 'Mock action for testing',
        handler: fakeActionHandler,
        supportsDryRun: true,
        schema: {
          input: z
            .object({
              foo: z.number(),
            })
            .and(z.object({ bar: z.string() })),
        },
      })

The following input:

{ foo: 1, bar: 'bar' }

results in an validation error:

InputError: Invalid input passed to action jest-zod-validated-action, instance does not match allOf schema [subschema 0] with 1 error[s]:, instance is not allowed to have the additional property "bar", instance does not match allOf schema [subschema 1] with 1 error[s]:, instance is not allowed to have the additional property "foo"

This is because the zod schema is converted to the following JsonSchema:

{
  "allOf": [
    {
      "type": "object",
      "properties": {
        "foo": {
          "type": "number"
        }
      },
      "required": [
        "foo"
      ],
      "additionalProperties": false
    },
    {
      "type": "object",
      "properties": {
        "bar": {
          "type": "string"
        }
      },
      "required": [
        "bar"
      ],
      "additionalProperties": false
    }
  ],
  "$schema": "http://json-schema.org/draft-07/schema#"
}

The Problem is, that the additionalProperties in both allOf-schemas resullts in failing each separate sub-schema validation. This seems to be a common problem in JsonSchema-Validation.

To fix such issues, one can use nonstrict() on each of the combined zod schemas, like:

z
    .object({
        foo: z.number(),
    })
    .nonstrict()
.and(
    z.object({ bar: z.string() })
      .nonstrict()
)

But with this in place, the user can add additional keys to the input, like:

{ foo: 1, bar: 'bar', x: 1 }

The JSON-Schema which is validating everything as expected looks like:

{
  "allOf": [
    {
      "type": "object",
      "properties": {
        "foo": {
          "type": "number"
        }
      },
      "required": [
        "foo"
      ],
    },
    {
      "type": "object",
      "properties": {
        "bar": {
          "type": "string"
        }
      },
      "required": [
        "bar"
      ],
    }
  ],
  "$schema": "http://json-schema.org/draft-07/schema#",
  "unevaluatedProperties": false
}

Note the unevaluatedProperties and the missing additionalProperties.


Since we already introduced the and operation in #16990 for the next release, this issue is an regression for:

  • gitlab:group:ensureExists
  • gitlab:projectAccessToken:create
  • gitlab:projectDeployToken:create
  • gitlab:projectVariable:create

👍 Expected behavior

scaffolder actions with intersected zod schemas as input should validate correctly

👎 Actual Behavior with Screenshots

see description

👟 Reproduction steps

see description

📃 Provide the context for the Bug.

Zod-Schema with intersected input types

🖥️ Your Environment

backstage 1.13, 1.14-next

👀 Have you spent some time to check if this bug has been raised before?

  • I checked and didn't find similar issue

🏢 Have you read the Code of Conduct?

Are you willing to submit PR?

Yes I am willing to submit a PR!

@Andy2003 Andy2003 added the bug Something isn't working label May 9, 2023
@github-actions github-actions bot added the area:scaffolder Everything and all things related to the scaffolder project area label May 9, 2023
@freben
Copy link
Member

freben commented May 11, 2023

Yeah I see. Indeed, .and doesn't look to be the right thing here. Looking at your ticket, I believe .intersection is the exact same. The way that zod works, it totally makes sense that it'd generate the JSONSchema it does.

Could you look into using merge instead of and? 🙏 That's the semantic operation we want to perform, not multiple strict but distinct schemas applying at the same time.

Andy2003 added a commit to Andy2003/backstage that referenced this issue May 11, 2023
Using zod intersections results in an invalid JsonSchema.
The issue has been reported at StefanTerdell/zod-to-json-schema#64.

The following actions has been fixed:

- gitlab:group:ensureExists
- gitlab:projectAccessToken:create
- gitlab:projectDeployToken:create
- gitlab:projectVariable:create

resolves backstage#17721

Signed-off-by: Andreas Berger <andreas@berger-ecommerce.com>
Andy2003 added a commit to Andy2003/backstage that referenced this issue May 11, 2023
Using zod intersections results in an invalid JsonSchema.
The issue has been reported at StefanTerdell/zod-to-json-schema#64.

The following actions has been fixed:

- gitlab:group:ensureExists
- gitlab:projectAccessToken:create
- gitlab:projectDeployToken:create
- gitlab:projectVariable:create

resolves backstage#17721

Signed-off-by: Andreas Berger <andreas@berger-ecommerce.com>
Rugvip added a commit that referenced this issue May 11, 2023
…son-schema

Fix input schema validation issue for gitlab actions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:scaffolder Everything and all things related to the scaffolder project area bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants