-
Notifications
You must be signed in to change notification settings - Fork 92
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
Zod chain for union types #312
Open
spencermaxfield
wants to merge
2
commits into
astahmer:main
Choose a base branch
from
spencermaxfield:zod_chain_for_union_types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"openapi-zod-client": minor | ||
--- | ||
|
||
Add support for chained validation to composed types (oneOf, anyOf, allOf) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,7 +84,12 @@ export function getZodSchema({ schema: $schema, ctx, meta: inheritedMeta, option | |
if (schema.oneOf) { | ||
if (schema.oneOf.length === 1) { | ||
const type = getZodSchema({ schema: schema.oneOf[0]!, ctx, meta, options }); | ||
return code.assign(type.toString()); | ||
const chain = getZodChain({ | ||
schema: type.schema as SchemaObject, | ||
meta: { ...meta, isRequired: true }, | ||
options | ||
}); | ||
return code.assign(`${type.toString()}${chain}`); | ||
} | ||
|
||
/* when there are multiple allOf we are unable to use a discriminatedUnion as this library adds an | ||
|
@@ -101,32 +106,39 @@ export function getZodSchema({ schema: $schema, ctx, meta: inheritedMeta, option | |
} | ||
|
||
return code.assign( | ||
`z.union([${schema.oneOf.map((prop) => getZodSchema({ schema: prop, ctx, meta, options })).join(", ")}])` | ||
`z.union([${schema.oneOf.map((prop) => { | ||
const type = getZodSchema({ schema: prop, ctx, meta, options }); | ||
const chain = getZodChain({ | ||
schema: prop as SchemaObject, | ||
meta: { ...meta, isRequired: true }, | ||
options | ||
}); | ||
return `${type.toString()}${chain}`; | ||
}).join(", ")}])` | ||
); | ||
} | ||
|
||
// anyOf = oneOf but with 1 or more = `T extends oneOf ? T | T[] : never` | ||
if (schema.anyOf) { | ||
if (schema.anyOf.length === 1) { | ||
const type = getZodSchema({ schema: schema.anyOf[0]!, ctx, meta, options }); | ||
return code.assign(type.toString()); | ||
const chain = getZodChain({ | ||
schema: type.schema as SchemaObject, | ||
meta: { ...meta, isRequired: true }, | ||
options | ||
}); | ||
return code.assign(`${type.toString()}${chain}`); | ||
} | ||
|
||
const types = schema.anyOf | ||
.map((prop) => getZodSchema({ schema: prop, ctx, meta, options })) | ||
.map((type) => { | ||
let isObject = true; | ||
|
||
if ("type" in type.schema) { | ||
if (Array.isArray(type.schema.type)) { | ||
isObject = false; | ||
} else { | ||
const schemaType = type.schema.type.toLowerCase() as NonNullable<typeof schema.type>; | ||
isObject = !isPrimitiveType(schemaType); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
return type.toString(); | ||
const chain = getZodChain({ | ||
schema: type.schema as SchemaObject, | ||
meta: { ...meta, isRequired: true }, | ||
options | ||
}); | ||
return `${type.toString()}${chain}`; | ||
}) | ||
.join(", "); | ||
|
||
|
@@ -136,7 +148,12 @@ export function getZodSchema({ schema: $schema, ctx, meta: inheritedMeta, option | |
if (schema.allOf) { | ||
if (schema.allOf.length === 1) { | ||
const type = getZodSchema({ schema: schema.allOf[0]!, ctx, meta, options }); | ||
return code.assign(type.toString()); | ||
const chain = getZodChain({ | ||
schema: type.schema as SchemaObject, | ||
meta: { ...meta, isRequired: true }, | ||
options | ||
}); | ||
return code.assign(`${type.toString()}${chain}`); | ||
} | ||
const { patchRequiredSchemaInLoop, noRequiredOnlyAllof, composedRequiredSchema } = inferRequiredSchema(schema); | ||
|
||
|
@@ -159,10 +176,22 @@ export function getZodSchema({ schema: $schema, ctx, meta: inheritedMeta, option | |
const first = types.at(0)!; | ||
const rest = types | ||
.slice(1) | ||
.map((type) => `and(${type.toString()})`) | ||
.map((type) => { | ||
const chain = getZodChain({ | ||
schema: type.schema as SchemaObject, | ||
meta: { ...meta, isRequired: true }, | ||
options, | ||
}); | ||
return `and(${type.toString()}${chain})`; | ||
}) | ||
.join("."); | ||
|
||
return code.assign(`${first.toString()}.${rest}`); | ||
const firstChain = getZodChain({ | ||
schema: first.schema as SchemaObject, | ||
meta: { ...meta, isRequired: true }, | ||
options, | ||
}); | ||
return code.assign(`${first.toString()}${firstChain}.${rest}`); | ||
} | ||
|
||
const schemaType = schema.type ? (schema.type.toLowerCase() as NonNullable<typeof schema.type>) : undefined; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm admittendly not entirely sure why
isRequired: true
is needed here and in the other places I addedgetZodChain
calls. I tried excluding it at first, but it resulted in many test failures, and including it gave the results I was expecting.