anatine / zod-plugins

Plugins and utilities for Zod
640 stars 89 forks source link

Turn unions of literals into enums #129

Closed A5rocks closed 1 year ago

A5rocks commented 1 year ago

This is useful especially for integer literals. Here's where I noticed the current behavior:

                          "type": {
                            "oneOf": [
                              { "type": "number", "enum": [1] },
                              { "type": "number", "enum": [2] },
                              { "type": "number", "enum": [3] },
                              { "type": "number", "enum": [4] },
                              { "type": "number", "enum": [5] },
                              { "type": "number", "enum": [6] },
                              { "type": "number", "enum": [7] },
                              { "type": "number", "enum": [8] },
                              { "type": "number", "enum": [9] },
                              { "type": "number", "enum": [10] },
                              { "type": "number", "enum": [11] }
                            ]
                          },

Debatably, it might be nicer to split cases like z.union(z.literal(4), z.literal(5), z.literal("h"), z.literal("i")) into 2 different groups and give those to the oneOf, which is really not possible the way I went around this. I suspect that case is significantly rarer, however.

nx-cloud[bot] commented 1 year ago

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 2b4981b134c52bfc53075826478492736fd23fa1. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


✅ Successfully ran 2 targets - [`nx affected:test --base=origin/main --codeCoverage`](https://cloud.nx.app/runs/L224jaJDx2) - [`nx affected:lint --base=origin/main`](https://cloud.nx.app/runs/a1IqZRszrL)

Sent with 💌 from NxCloud.

A5rocks commented 1 year ago

To provide some connection to the opened issues, this is sort of a special case for the inverse of https://github.com/anatine/zod-plugins/issues/70 (this is a special case for a join operation, vs that issue asking about implementing a meet operation)