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

required within a property is ignored by json2ts in a JSON Schema version draft-03 #396

Closed
mardukbp opened this issue May 24, 2021 · 1 comment
Labels

Comments

@mardukbp
Copy link

Given (an excerpt of) the Xray JSON Schema:

{
    "type": "object",
    "$schema": "http:https://json-schema.org/draft-03/schema",
    "required": false,
    "properties": {
        "testExecutionKey": {
            "type": "string",
            "required": false
        },
        "tests": {
            "type": "array",
            "required": true,
            "items": {
                "type": "object",
                "required": false,
                "properties": {
                    "status": {
                        "type": "string",
                        "required": true
                    },
                    "testKey": {
                        "type": "string",
                        "required": true
                    }
                }
            }
        }
    }
}

executing

json2ts xray.json > xray.d.ts

produces

export interface Example {
  testExecutionKey?: string;
  tests?: {
    status?: string;
    testKey?: string;
    [k: string]: unknown;
  }[];
  [k: string]: unknown;
}

Whereas converting the following schema:

{
    "type": "object",
    "$schema": "http:https://json-schema.org/draft-03/schema",
    "required": ["tests"],
    "properties": {
        "testExecutionKey": {
            "type": "string"
        },
        "tests": {
            "type": "array",
            "items": {
                "type": "object",
                "required": ["status", "testKey"],
                "properties": {
                    "status": {
                        "type": "string"
                    },
                    "testKey": {
                        "type": "string"
                    }
                }
            }
        }
    }
}

produces the expected result:

export interface Example {
  testExecutionKey?: string;
  tests: {
    status: string;
    testKey: string;
    [k: string]: unknown;
  }[];
  [k: string]: unknown;
}
@bcherny
Copy link
Owner

bcherny commented Sep 12, 2021

This isn't a valid JSONSchema. required must be an array of keys that are required. required: true isn't a thing.

See https://json-schema.org/draft/2020-12/json-schema-validation.html#rfc.section.6.5.3

6.5.3. required

The value of this keyword MUST be an array. Elements of this array, if any, MUST be strings, and MUST be unique.
An object instance is valid against this keyword if every item in the array is the name of a property in the instance.
Omitting this keyword has the same behavior as an empty array.

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

No branches or pull requests

2 participants