xddq / schema2typebox

Creating TypeBox code from JSON schemas
MIT License
66 stars 15 forks source link

feat!: default to union types and preserve configurability for enum, union or both #24

Closed pdfowler closed 1 year ago

pdfowler commented 1 year ago

Summary

Split from my PR #23, this PR focuses on changing the default behavior of schema2typebox from generating TypeBox Enum types to defaulting to TypeBox Union types. This is inline with the typescript community which has shifted to using union types over enums.

Example

schemaenum modeunion mode
``` { "type": "object", "properties": { "status": { "enum": [ "unknown", "accepted", "denied" ]} }, "required": ["status"] } ``` ``` export enum StatusEnum { UNKNOWN = "unknown", ACCEPTED = "accepted", DENIED = "denied", } Type.Object({ status: Type.Enum(StatusEnum), }) ``` ``` export const StatusUnion = Type.Union([ Type.Literal("unknown"), Type.Literal("accepted"), Type.Literal("denied"), ]) Type.Object({ status: StatusUnion, }) ```

Checklist

Discussion & Open Questions

Configurability

I agree with the goal of keeping the number of configuration options to a minimum, but I have to say that I have found it incredibly useful to have control over things like this. While not rising to the level of debate seen on other topics, I think choosing one or the other alienates potential users unnecessarily. I have felt this pressure internally, and while I haven't migrated our entire codebase, I believe there are some scenarios where I will face resistance if I don't generate an Enum type.

xddq commented 1 year ago

closing as mentioned here