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

Schema for intersection types are invalid #64

Closed
Andy2003 opened this issue May 9, 2023 · 0 comments · Fixed by #65
Closed

Schema for intersection types are invalid #64

Andy2003 opened this issue May 9, 2023 · 0 comments · Fixed by #65

Comments

@Andy2003
Copy link
Contributor

Andy2003 commented May 9, 2023

Given:

const schema1 = z.object({
    foo: z.string()
});
const schema2= z.object({
    bar: z.string()
});
const intersection = z.intersection(schema1, schema2);
const jsonSchema = parseIntersectionDef(intersection._def, getRefs());

the following schema is generated:

{
    "allOf": [
        {
            "additionalProperties": false,
            "properties": {
                "foo": {
                    "type": "string"
                }
            },
            "required": [
                "foo"
            ],
            "type": "object"
        },
        {
            "additionalProperties": false,
            "properties": {
                "bar": {
                    "type": "string"
                }
            },
            "required": [
                "bar"
            ],
            "type": "object"
        }
    ]
}

The following input:

{ foo: 'foo', bar: 'bar' }

results in an validation error:

image

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.

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

{
  "allOf": [
    {
      "type": "object",
      "properties": {
        "foo": {
          "type": "string"
        }
      },
      "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.

image

This issue was found in backstage/backstage#17721

Andy2003 added a commit to Andy2003/zod-to-json-schema that referenced this issue May 9, 2023
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 zod-to-json-schema project [1].

The following actions has been fixed:

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

[1]  StefanTerdell/zod-to-json-schema#64

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>
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>
StefanTerdell added a commit that referenced this issue May 11, 2023
…ypes

Remove additionalProperties from intersected objects.
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 a pull request may close this issue.

1 participant