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

New TJS.PartialArgs setting sortEnums #574

Closed
Elliot-Ball-Tray opened this issue Sep 29, 2023 · 8 comments
Closed

New TJS.PartialArgs setting sortEnums #574

Elliot-Ball-Tray opened this issue Sep 29, 2023 · 8 comments

Comments

@Elliot-Ball-Tray
Copy link
Contributor

I have a use case where I need the order of enums defined in a type to be preserved in the resulting JSON schema.

I was wondering if you would be open to a new property in TJS.PartialArgs called sortEnums. This would default to true to ensure backwards compatibility and setting it to false would bypass the sorting done here:

https://github.com/YousefED/typescript-json-schema/blob/655d7741a6e0514855b36f94cc1a517d23159f19/typescript-json-schema.ts#L929C28-L929C28

@domoritz
Copy link
Collaborator

Hmm, I would be okay with even removing the sort. It may be there for de-duplication so we need to be careful. The tricky thing is that sometimes the enum may be the result of multiple types being combined. We definitely don't want different orders when files are moved around for example.

@Elliot-Ball-Tray
Copy link
Contributor Author

How could the sorting enable deduplication? If I use the same key in an enum it is invalid typescript and if I use the same value, I get the duplicated value in the resulting JSON schema.

And can you share or describe the scenario you are thinking of and I can try testing it to see how the lib behaves currently?

@domoritz
Copy link
Collaborator

I don't recall how we store the enum set. If we store it as a sorted list, the deduplication code would be different than for an unsorted list.

I'm thinking of having something where a type is the combination of two other types which are enums.

@Elliot-Ball-Tray
Copy link
Contributor Author

Is this how you would combine two enums? I get a runtime error TypeError: Unsupported type: never

enum Enum {
    X = "x" as any,
}

enum EnumContinued {
    Y = "y" as any,
}

type ExtendedEnum = Enum & EnumContinued;

@domoritz
Copy link
Collaborator

domoritz commented Oct 2, 2023

@Elliot-Ball-Tray
Copy link
Contributor Author

thanks, got that working now, appreciate your patience here.
This type:

enum Enum {
    X = "x",
    Z = "z",
}

enum EnumContinued {
    Y = "y",
    X = "x",
    B = "b",
}

type MyObject = (typeof Enum) & (typeof EnumContinued);

Produces this schema:

{
  allOf: [
    { type: 'string', enum: [ 'x', 'z' ] },
    { type: 'string', enum: [ 'b', 'x', 'y' ] }
  ],
  '$schema': 'http://json-schema.org/draft-07/schema#'
}

But I'm still struggling to reproduce any deduplication, the two enums appear as different objects within an allOf and both contain the duplicate x value. I get similar results even after removing definition.enum = enumValues.sort(); from the lib.

@domoritz
Copy link
Collaborator

domoritz commented Oct 3, 2023

I probably misremembered then. I also work on https://github.com/vega/ts-json-schema-generator so maybe that was there.

In either way, let's try to remove the sort. Can you send a pull request?

@Elliot-Ball-Tray
Copy link
Contributor Author

here is the PR: #576

@domoritz domoritz closed this as completed Oct 4, 2023
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

2 participants