Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Terdell committed Jun 18, 2023
2 parents 6bae678 + 9c5bb42 commit 34da783
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 22 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,20 @@ You can pass a string as the second parameter of the main zodToJsonSchema functi

Instead of the schema name (or nothing), you can pass an options object as the second parameter. The following options are available:

| Option | Effect |
| ------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| **name**?: _string_ | As described above. |
| **basePath**?: string[] | The base path of the root reference builder. Defaults to ["#"]. |
| Option | Effect |
| ------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **name**?: _string_ | As described above. |
| **basePath**?: string[] | The base path of the root reference builder. Defaults to ["#"]. |
| **$refStrategy**?: "root" \| "relative" \| "seen" \| "none" | The reference builder strategy; <ul><li>**"root"** resolves $refs from the root up, ie: "#/definitions/mySchema".</li><li>**"relative"** uses [relative JSON pointers](https://tools.ietf.org/id/draft-handrews-relative-json-pointer-00.html). _See known issues!_</li><li>**"seen"** reuses the output of any "seen" Zod schema. In theory it's a more performant version of "none", but in practice this behaviour can cause issues with nested schemas and has now gotten its own option.</li> <li>**"none"** ignores referencing all together, creating a new schema branch even on "seen" schemas. Recursive references defaults to "any", ie `{}`.</li></ul> Defaults to "root". |
| **effectStrategy**?: "input" \| "any" | The effects output strategy. Defaults to "input". _See known issues!_ |
| **definitionPath**?: "definitions" \| "$defs" | The name of the definitions property when name is passed. Defaults to "definitions". |
| **target**?: "jsonSchema7" \| "jsonSchema2019-09" \| "openApi3" | Which spec to target. Defaults to "jsonSchema7" |
| **strictUnions**?: boolean | Scrubs unions of any-like json schemas, like `{}` or `true`. Multiple zod types may result in these out of necessity, such as z.instanceof() |
| **definitions**?: Record<string, ZodSchema> | See separate section below |
| **errorMessages**?: boolean | Include custom error messages created via chained function checks for supported zod types. See section below |
| **markdownDescription**?: boolean | Copies the `description` meta to `markdownDescription` |
| **emailStrategy**?: "format:email" \| "format:idn-email" \| "pattern:zod" | Choose how to handle the email string check. Defaults to "format:email". |
| **effectStrategy**?: "input" \| "any" | The effects output strategy. Defaults to "input". _See known issues!_ |
| **dateStrategy**?: "string" \| "integer" | Date strategy, integer allow to specify in unix-time min and max values. Defaults to "string". |
| **emailStrategy**?: "format:email" \| "format:idn-email" \| "pattern:zod" | Choose how to handle the email string check. Defaults to "format:email". |
| **definitionPath**?: "definitions" \| "$defs" | The name of the definitions property when name is passed. Defaults to "definitions". |
| **target**?: "jsonSchema7" \| "jsonSchema2019-09" \| "openApi3" | Which spec to target. Defaults to "jsonSchema7" |
| **strictUnions**?: boolean | Scrubs unions of any-like json schemas, like `{}` or `true`. Multiple zod types may result in these out of necessity, such as z.instanceof() |
| **definitions**?: Record<string, ZodSchema> | See separate section below |
| **errorMessages**?: boolean | Include custom error messages created via chained function checks for supported zod types. See section below |
| **markdownDescription**?: boolean | Copies the `description` meta to `markdownDescription` |

### Definitions

Expand Down
2 changes: 2 additions & 0 deletions src/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type Options<Target extends Targets = "jsonSchema7"> = {
basePath: string[];
effectStrategy: "input" | "any";
pipeStrategy: "input" | "all";
dateStrategy: "string" | "integer";
target: Target;
strictUnions: boolean;
definitionPath: string;
Expand All @@ -23,6 +24,7 @@ export const defaultOptions: Options = {
basePath: ["#"],
effectStrategy: "input",
pipeStrategy: "all",
dateStrategy: "string",
definitionPath: "definitions",
target: "jsonSchema7",
strictUnions: false,
Expand Down
2 changes: 1 addition & 1 deletion src/parseDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ const selectParser = (
case ZodFirstPartyTypeKind.ZodBoolean:
return parseBooleanDef();
case ZodFirstPartyTypeKind.ZodDate:
return parseDateDef();
return parseDateDef(def, refs);
case ZodFirstPartyTypeKind.ZodUndefined:
return parseUndefinedDef();
case ZodFirstPartyTypeKind.ZodNull:
Expand Down
Loading

0 comments on commit 34da783

Please sign in to comment.