@jackdbd/zod-to-doc
Advanced tools
Comparing version 1.0.7 to 1.1.0
# CHANGELOG | ||
## [1.1.0](https://github.com/jackdbd/zod-to-doc/compare/v1.0.7...v1.1.0) (2024-04-25) | ||
### Features | ||
* render any z.union() as strings ([e95b108](https://github.com/jackdbd/zod-to-doc/commit/e95b10849ca2e00807fcb2d1f0b7ac3b99c7d01a)) | ||
## [1.0.7](https://github.com/jackdbd/zod-to-doc/compare/v1.0.6...v1.0.7) (2024-02-06) | ||
@@ -4,0 +11,0 @@ |
import { z } from 'zod'; | ||
import type { ZodTypeAny } from 'zod'; | ||
/** | ||
@@ -7,2 +8,22 @@ * @internal | ||
/** | ||
* Converts any Zod type into an array of strings. | ||
* | ||
* @public | ||
* @experimental | ||
*/ | ||
export declare const stringsFromZodAnyType: (x: ZodTypeAny) => string[]; | ||
/** | ||
* Converts a Zod union into an array of strings. | ||
* | ||
* @public | ||
* @experimental | ||
*/ | ||
export declare const arrayFromZodUnion: <S extends z.ZodUnion<readonly [z.ZodTypeAny, ...z.ZodTypeAny[]]>>(schema: S) => { | ||
error: Error; | ||
value?: undefined; | ||
} | { | ||
value: string[]; | ||
error?: undefined; | ||
}; | ||
/** | ||
* Converts a Zod schema into an array of objects. | ||
@@ -9,0 +30,0 @@ * |
@@ -24,2 +24,73 @@ /** | ||
/** | ||
* Converts any Zod type into an array of strings. | ||
* | ||
* @public | ||
* @experimental | ||
*/ | ||
export const stringsFromZodAnyType = (x) => { | ||
if (x instanceof z.ZodBigInt) { | ||
return x.description ? [x.description] : ['A BigInt']; | ||
} | ||
else if (x instanceof z.ZodBoolean) { | ||
return x.description ? [x.description] : ['A Boolean']; | ||
} | ||
else if (x instanceof z.ZodLiteral) { | ||
if (x.value) { | ||
if (x.description) { | ||
return [`${stringify(x.value)} (${x.description})`]; | ||
} | ||
else { | ||
return [stringify(x.value)]; | ||
} | ||
} | ||
else { | ||
if (x.description) { | ||
return [`A literal (${x.description})`]; | ||
} | ||
else { | ||
return [`A literal`]; | ||
} | ||
} | ||
} | ||
else if (x instanceof z.ZodNumber) { | ||
// TODO: get min,max from these checks? | ||
// console.log('=== x._def.checks ===', x._def.checks) | ||
return x.description ? [x.description] : ['A Number']; | ||
} | ||
else if (x instanceof z.ZodObject) { | ||
// const res = arrayFromZodSchema(x as any) | ||
return x.description ? [x.description] : ['An objects']; | ||
} | ||
else if (x instanceof z.ZodString) { | ||
return x.description ? [x.description] : ['A String']; | ||
} | ||
else if (x instanceof z.ZodUnion) { | ||
const arr = x.options.map((opt) => { | ||
return stringsFromZodAnyType(opt); | ||
}); | ||
const strings = arr.flat(); | ||
strings.sort(); | ||
return strings; | ||
} | ||
else { | ||
// console.log('=== stringFromZodAnyType x._def ===', x._def) | ||
return x.description ? [x.description] : ['TODO']; | ||
} | ||
}; | ||
/** | ||
* Converts a Zod union into an array of strings. | ||
* | ||
* @public | ||
* @experimental | ||
*/ | ||
export const arrayFromZodUnion = (schema) => { | ||
if (!schema.options) { | ||
return { error: new Error(`schema.options is not defined.`) }; | ||
} | ||
debug(`Zod schema.options => JS array`); | ||
const arr = schema.options.map(stringsFromZodAnyType).flat(); | ||
arr.sort(); | ||
return { value: arr }; | ||
}; | ||
/** | ||
* Converts a Zod schema into an array of objects. | ||
@@ -34,3 +105,3 @@ * | ||
} | ||
debug(`Zod schema => JS array`); | ||
debug(`Zod schema.shape => JS array`); | ||
const arr = Object.entries(schema.shape).map(([key, value]) => { | ||
@@ -91,2 +162,14 @@ // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} | ||
if (typeof x === 'bigint') { | ||
// The trailing "n" is not part of the string. | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/toString | ||
// https://github.com/GoogleChromeLabs/jsbi/issues/30#issuecomment-521449285 | ||
return x.toString(); | ||
} | ||
if (typeof x === 'symbol') { | ||
// Because Symbol has a [@@toPrimitive]() method, that method always takes | ||
// priority over toString() when a Symbol object is coerced to a string. | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toString | ||
return x.toString(); | ||
} | ||
if (x.length === 0) { | ||
@@ -93,0 +176,0 @@ return `\`[]\``; |
{ | ||
"name": "@jackdbd/zod-to-doc", | ||
"version": "1.0.7", | ||
"version": "1.1.0", | ||
"description": "Inject your [Zod](https://github.com/colinhacks/zod) schemas into your docs.", | ||
@@ -64,3 +64,3 @@ "author": { | ||
"commitlint": "commitlint --config ./config/commitlint.cjs --to HEAD --verbose", | ||
"dev": "run-p 'build:ts:watch' 'test:watch'", | ||
"dev": "DEBUG='' run-p 'build:ts:watch' 'test:watch'", | ||
"preexample": "chmod u+x ./dist/cli.js", | ||
@@ -67,0 +67,0 @@ "example": "run-s 'example:car'", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
52236
678