Skip to content

Convert json schema draft 06 const values to typescript const values #264

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

Closed
wants to merge 4 commits into from

Conversation

djbeaumont
Copy link

Resolves #263

I'm a little unclear on the structure of the project so any feedback on the PR is gratefully received. This is a little exploratory and I have no idea if this can be said to implement the feature from json schema draft 06, but I think it covers enough for my needs.

@nicojs
Copy link
Contributor

nicojs commented Oct 22, 2019

Awesome! Open source at it's finest 👍. I like the simplicity of the solution.

The simple example now works:

export interface Foo {
  bar: true;
}

However, it seems that falsy values don't work yet 😈.

{
  "title": "Foo",
  "required": ["bar", "baz", "qux", "quux"],
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "bar": {
      "const": false
    },
    "baz": {
      "const": ""
    },
    "qux": {
      "const": 0
    },
    "quux": {
      "const": null
    }
  }
}

This results in:

export interface Foo {
  bar: {
    [k: string]: any;
  };
  baz: {
    [k: string]: any;
  };
  qux: {
    [k: string]: any;
  };
  quux: {
    [k: string]: any;
  };
}

@nicojs
Copy link
Contributor

nicojs commented Oct 22, 2019

I'll see your PR and I'll raise you another PR

The falsy value thingy should be fixed in djbeaumont#1

🤟

@djbeaumont
Copy link
Author

ahh great catch, thank you!

@djbeaumont
Copy link
Author

@bcherny what do you think of this? I suppose it would have to be a new major version as it will be a big change to the outputted types.

@nicojs
Copy link
Contributor

nicojs commented Oct 30, 2019

@bcherny I don't want to be rude, but could you please take a look? I would like to actually use it at our company. Thanks!

@qzb
Copy link

qzb commented Oct 31, 2019

@nicojs Unfortunately bcherny maintains this package pretty unfrequently, I guess he'll accept this PR, but it could take a while.

@djbeaumont
Copy link
Author

@nicojs FWIW I'm using this workaround in my codebase:

/**
 * Workaround until this is merged:
 * https://github.com/bcherny/json-schema-to-typescript/pull/264
 * @param {string} definition
 * @returns {string} Same defined interface with event_type made constant.
 */
function fudgeEventTypeInDefinition(eventType, definition) {
  const toReplace = 'event_type: string;';
  const replaceWith = `event_type: '${eventType}';`;
  return definition.replace(toReplace, replaceWith);
}

It works because the constant property I want to set happens to be the same as the filename of the schema that I'm parsing. Clearly not a general solution.

@qzb
Copy link

qzb commented Oct 31, 2019

@djbeaumont According to JSON Schema const is only a shorthand for enum with single element, so if you can modify original JSON Schema file, simply replace "const": "value" with "enum": [ "value" ]. Enums are handled correctly by json2ts, so it should workaround your issue.

Copy link
Owner

@bcherny bcherny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there @djbeaumont! Thanks for the contribution, and sorry about the delay in reviewing it.

Since const is just sugar for a single-member enum, could we try to implement this as normalizer step instead that de-sugars const to enum?

Copy link
Owner

@bcherny bcherny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes per my comment above^

@JonathanWilbur
Copy link

@djbeaumont I can't wait for this to make it in. Thank you so much for pursuing this! Do you expect to follow up soon with corrections so this can be merged in?

@jeffvandyke
Copy link

What's the status on this? I found myself wanting to use "const", and while single-member "enum" works, I'd be excited to see this make it in!

@L2jLiga
Copy link

L2jLiga commented Dec 16, 2020

Hi there? Any updates upcomming here?

@bcherny
Copy link
Owner

bcherny commented Dec 19, 2020

#290 is another shot at this, using the normalizer approach.

@bcherny bcherny closed this Dec 19, 2020
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 this pull request may close these issues.

Support for constant values
7 participants