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

[FEATURE]: Force enum inference on certain fields #2643

Open
ASTRELION opened this issue Aug 27, 2024 · 0 comments
Open

[FEATURE]: Force enum inference on certain fields #2643

ASTRELION opened this issue Aug 27, 2024 · 0 comments

Comments

@ASTRELION
Copy link

ASTRELION commented Aug 27, 2024

It would be useful to turn on enum inference for certain fields in types, like "index" fields, since they may become numerous but are often short and unique. At the moment if there are too many unique values then at some point it will not generate enums, which is not desired in my case of using indexes.

Context (Input, Language)

Input Format: JSON
Output Language: Any, but mainly Typescript (with Zod)

Description

I have a large JSON array of many objects, all of them following this format:

{
    "index": "foo",
    ...
}

There are a lot of elements in this array (100s), but it would be very useful to force an enum to be created for the index field specifically, since all relevant queries and code against the data will use index as the identifier.

Current Behaviour / Output

Right now it seems to cut off at some point where if there are too many items then index is just assumed to be a generic string, but I'd like to force an enum for index fields specifically.

Proposed Behaviour / Output

An output would be generated with certain specified fields guaranteed to be a zod (or other language equivalent) enum.

Solution

I'm generating these types through the TypeScript NPM library, so maybe an additional option like "forceEnumOnFields" like so:

const qt = await quicktype({
    inputData,
    lang: "typescript",
    // if this is false, then only generate fields on enums specified by the below array
    // if this is true, do the normal behavior but also guarantee enums on the fields in the below array
    inferEnums: false, // existing option
    forceEnumOnFields: [ // proposed additional option
        "index",
    ],
});

Alternatives

A flag to also tweak the ceiling of amount of unique items before Quicktype stops trying to infer enums would be useful as well (this might be related to #757), but would not solve this particular issue very well and would lead to too much data becoming an enum.

Context

N/A

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

1 participant