An enum, or a string?
A generator sees "admin" and writes string. Sometimes that is right and sometimes it throws away the most useful fact in your data.
Here is a field with three legal values:
{ "id": 1, "role": "admin", "status": "active" }
Open this example in the tool →
A generator reads one sample and sees a string. It writes role: string, and
the fact that only three values are allowed is gone.
That fact is worth more than most of the type. With it, the compiler can tell
you that you forgot the suspended case. Without it, you find out when a
suspended user sees the admin panel.
When a generator will make an enum
Give it several samples, and most generators notice a small set of repeated strings:
[
{ "role": "admin" },
{ "role": "editor" },
{ "role": "viewer" },
{ "role": "admin" }
]
Open this example in the tool →
Now TypeScript may give you:
export enum Role {
Admin = 'admin',
Editor = 'editor',
Viewer = 'viewer',
}
and Rust, Swift, Java and the rest give their own form. The option A union,
not an enum switches TypeScript between the enum above and
'admin' | 'editor' | 'viewer', which most TypeScript projects now prefer,
because it needs no runtime object.
But this only works if your samples happen to contain every value. Three out of four means a type that is confidently wrong.
The danger of a closed enum
An enum says: these are all the values. If the API adds owner next
month, what happens?
| Language | What happens |
|---|---|
| TypeScript, union type | Nothing at runtime. The check at the edge fails, if you have one |
| Rust | serde returns an error for the whole document |
| Swift | The decode throws, unless the enum is optional |
| Java, Jackson | Throws, by default |
| Go | A plain string type accepts it silently |
This is a real trade. A strict enum turns a new value into a loud failure at the edge, which is correct if you must not act on a value you do not understand. It is wrong if the sensible behaviour is to ignore the unknown case and carry on.
Pick on purpose:
- Closed for something that controls permission, money or safety. You want the failure.
- Open for a label, a category, a display field. Keep the unknown value as a string and show it.
Swift's option null for an unknown case does exactly this, and Rust has
#[serde(other)] for a catch-all variant.
Where the truth lives
A sample cannot tell you the full set. Three places can:
- The API documentation. Read it and write the enum by hand.
- A JSON Schema, with
enum:
{
"type": "object",
"properties": {
"role": { "enum": ["admin", "editor", "viewer", "owner"] }
}
}
Open this example in the tool →
Feed that schema to the tool instead of a sample and every language gets the complete enum, because the complete set is in the document.
- The database. If it is your own API, the set is in a column constraint or in the code that writes the value.
A note on a number used as an enum
{ "status": 2 }
Open this example in the tool →
This is worse than the string version, because nothing in the data hints at
the meaning. A generator writes int64 and the knowledge stays in a wiki
page nobody reads. If you own the API, send the string. It costs a few bytes
and saves an hour every time somebody new reads a log line.
What to do
- Generate from several samples, so the generator has a chance.
- Look at every
stringin the output and ask: is this free text, or a small set? - For a small set, write the enum by hand, or put it in a schema and generate again.
- Decide, for each enum, whether an unknown value should fail or pass.
Try it
Paste the four-record array above and watch the enum appear. Then change one
"admin" to "owner" and watch the enum grow. That is the generator doing
all it can with the evidence it has.
Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open JSON to CodeRead next
A type is not a check
TypeScript, Flow and Elm types vanish when the program runs. If your data comes from outside, you need something that is still there at run time.
Zod or a JSON Schema — which one should you keep?
Both check data. One lives in your TypeScript, one lives in a file every language can read. The choice depends on how many languages you have.