Skip to content

Commit

Permalink
fix: detect a ZodUnion in a different way
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdbd committed Apr 25, 2024
1 parent 63e5ca0 commit b2d2d75
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"readme": "npm run example && tsm readme.ts",
"release:dry": "semantic-release --debug --dry-run --no-ci",
"size": "pkg-size ./dist --sort-by=brotli --ignore-files '{*.d.ts,*.map}'",
"test": "node --test --experimental-test-coverage --test-name-pattern=cli",
"test": "node --test --experimental-test-coverage",
"test:ci": "node --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage/lcov.info",
"test:watch": "node --test --watch"
},
Expand Down
17 changes: 15 additions & 2 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ export const defaultZodValue = (value: any) => {
* @experimental
*/
export const stringsFromZodAnyType = (x: ZodTypeAny) => {
if ((x as any).options) {
// x should be a z.ZodUnion of some type
const arr: string[] = (x as any).options.map((opt: z.ZodAny) => {
return stringsFromZodAnyType(opt)
})
const strings = arr.flat()
strings.sort()
return strings
}

if (x instanceof z.ZodBigInt) {
return x.description ? [x.description] : ['A BigInt']
} else if (x instanceof z.ZodBoolean) {
Expand Down Expand Up @@ -65,8 +75,11 @@ export const stringsFromZodAnyType = (x: ZodTypeAny) => {
strings.sort()
return strings
} else {
// console.log('=== stringFromZodAnyType x._def ===', x._def)
return x.description ? [x.description] : ['TODO']
let not_handled = 'unknown'
if (x._def && x._def.typeName) {
not_handled = x._def.typeName
}
return x.description ? [`${not_handled} (${x.description})`] : [not_handled]
}
}

Expand Down

0 comments on commit b2d2d75

Please sign in to comment.