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

Fixes to allow const assertions in nativeEnum #24

Merged
merged 1 commit into from
Nov 13, 2022

Conversation

mokocm
Copy link
Contributor

@mokocm mokocm commented Oct 20, 2022

Bug Description

Zod can use const enum in z.nativeEnum() , but zod-to-json-schema did not parse them correctly.

// input
const values = {
  val1: 1,
  val2: 2,
} as const

const actualValues = Object.values(values).filter(
    (_, i) => i >= numberValues.length
);

console.log({
    type:
      numberValues.length === 0
        ? "string"
        : numberValues.length === actualValues.length
        ? "number"
        : ["string", "number"],
    enum: actualValues,
  })
// output
// {
//   "type": [
//     "string",
//     "number"
//   ],
//   "enum": []
// } 

Fix

Fixed to correctly parse both in the case of enum and const enums object.

@StefanTerdell StefanTerdell merged commit 7b91b51 into StefanTerdell:master Nov 13, 2022
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

Successfully merging this pull request may close these issues.

None yet

2 participants