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

allOf and additionalProperties #15

Closed
pihentagy opened this issue Oct 28, 2021 · 2 comments
Closed

allOf and additionalProperties #15

pihentagy opened this issue Oct 28, 2021 · 2 comments

Comments

@pihentagy
Copy link

Given the following typescript file:

interface A {
    a: string;
}

interface B {
    b: string;
}

export type C = A & B;

typeconv produces the following json schema:

{
  "definitions": {
    "C": {
      "allOf": [
        {
          "$ref": "#/definitions/A"
        },
        {
          "$ref": "#/definitions/B"
        }
      ],
      "title": "C"
    },
    "A": {
      "type": "object",
      "properties": {
        "a": {
          "type": "string",
          "title": "A.a"
        }
      },
      "required": [
        "a"
      ],
      "additionalProperties": false,
      "title": "A"
    },
    "B": {
      "type": "object",
      "properties": {
        "b": {
          "type": "string",
          "title": "B.b"
        }
      },
      "required": [
        "b"
      ],
      "additionalProperties": false,
      "title": "B"
    }
  },
  "$id": "dummy.json",
  "$comment": "Generated from src/api/dummy.ts by core-types-json-schema (https://github.com/grantila/core-types-json-schema) on behalf of typeconv (https://github.com/grantila/typeconv)"
}

However, the following json will not validate against type C in the schema:

{
  "a": "hello",
  "b": "world"
}

You can check it only at https://www.jsonschemavalidator.net/. You should add the following lines to the beginning of the json schema:

  "type": "object",
  "$ref": "#/definitions/C",
@ChuckJonas
Copy link

I'm also running into this same issue.

The incompatibility with how ajv validates this is documented here: ajv-validator/ajv#1496.

Apperently the solution is to use "unevaluatedProperties", but it's unclear to me if this would be part of the OpenAPI spec (or even if it's official json schema for that matter)

@grantila
Copy link
Owner

grantila commented Apr 3, 2023

Thanks for this!

Intersection types in JSON Schema works differently than in e.g. TypeScript. Merging A and B into one self-contained object C will make this work.

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

No branches or pull requests

3 participants