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

class bar: "ok" | "fail" | "abort" | string; should not only map to a string #164

Open
gdimauro opened this issue Nov 6, 2017 · 12 comments

Comments

@gdimauro
Copy link

gdimauro commented Nov 6, 2017

Hi to everyone, test "string-literals-inline" field bar:

class MyObject {
    ...
    bar: "ok" | "fail" | "abort" | string;
}

maps to schema:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "properties": {
        "bar": {
            "type": "string"
        }, ...

but should map to schema:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "properties": {
        "bar": {
            "anyOf": [{
                    "custom": "string"
                },
                {
                    "enum": [
                        "ok",
                        "fail",
                        "abort"
                    ],
                    "default": "string"
                }
            ]
        },...

Cheers,
Giuseppe

@domoritz
Copy link
Collaborator

domoritz commented Nov 6, 2017

Hmm, I think this may be a restriction of the typescript compiler if we use symbols. Can you try https://github.com/vega/ts-json-schema-generator and see whether it does the trick. The other library uses the AST.

@gdimauro
Copy link
Author

gdimauro commented Nov 6, 2017

hmm... seems to generate exactly the same:

{
    "type": "object",
    "properties": {
        "foo": {
            "type": "string",
            "enum": [
                "abort",
                "fail",
                "ok"
            ]
        },
        "bar": {
            "type": "string"
        }
    },
    "required": [
        "foo",
        "bar"
    ],
    "$schema": "http://json-schema.org/draft-04/schema#"
}

@domoritz
Copy link
Collaborator

domoritz commented Nov 6, 2017

Okay. I'd say that's not really a bug since string literals are also strings and so 'foo' | string == string.

Since this is not a bug, I am marking this as an enhancement.

@gdimauro
Copy link
Author

gdimauro commented Nov 6, 2017

It will be a great enhancement. In visual studio code, for example, you never see with the auto completion strings "abort", "fail" and "ok" ... you see other unnecessary contextual auto generated strings and you're obviously free to insert also a string.

With this enhancement you'll see "abort", "fail" and "ok" and you're also free to insert a common string. I used this schema combination for a very big schema before discover this great tool/library and it's absolutely super!

Cheers,
Giuseppe

@domoritz
Copy link
Collaborator

domoritz commented Nov 6, 2017

I just tested https://github.com/vega/ts-json-schema-generator and it produces something different from what you said (and already does what you ask for :-))

> [email protected] run /Users/domoritz/Developer/UW/ts-json-schema-generator
> ts-node ts-json-schema-generator.ts "--path" "test.ts" "--type" "MyObject"

{
  "$ref": "#/definitions/MyObject",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "definitions": {
    "MyObject": {
      "additionalProperties": false,
      "properties": {
        "bar": {
          "anyOf": [
            {
              "enum": [
                "ok"
              ],
              "type": "string"
            },
            {
              "enum": [
                "fail"
              ],
              "type": "string"
            },
            {
              "enum": [
                "abort"
              ],
              "type": "string"
            },
            {
              "type": "string"
            }
          ]
        }
      },
      "required": [
        "bar"
      ],
      "type": "object"
    }
  }
}

@domoritz
Copy link
Collaborator

domoritz commented Nov 6, 2017

I created an issue for making the schema output a but nicer: vega/ts-json-schema-generator#20.

@gdimauro
Copy link
Author

gdimauro commented Nov 7, 2017

I just gave a look at the string-literal-in-line test where I see that the expected result for the bar field is:
"bar": {
"type": "string"
}

... will give a try tomorrow with my real case. Tnx in advance. Giuseppe

@domoritz
Copy link
Collaborator

domoritz commented Nov 7, 2017

Thanks for trying it. Let me know how it goes.

@gdimauro
Copy link
Author

gdimauro commented Nov 7, 2017

ok Dominik, it does the job. I just forked your git. Thank you so much! Giuseppe

@gdimauro
Copy link
Author

gdimauro commented Nov 7, 2017

Dominik, quick question: how can I redirect the output to a file? (original --output or -o parameter)

G.

@domoritz
Copy link
Collaborator

domoritz commented Nov 7, 2017

Pipe it with >. You don't need to fork the repo. The library is available on npm.

@gdimauro
Copy link
Author

gdimauro commented Nov 8, 2017

ok just like I did (pipe). Thank you so much again. Giuseppe

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

No branches or pull requests

2 participants