Open
Conversation
96f36d3 to
5f55cd6
Compare
Owner
|
This looks cool, let’s remove the noises (commented code & todo) and ship some features from this PR :) |
Author
|
Cleaned up and rebased. |
fabien0102
requested changes
May 16, 2023
Owner
fabien0102
left a comment
There was a problem hiding this comment.
Looks good, let's add few unit tests to ensure we are fixing real issues, and nobody breaks those later
(Sorry for the delay, I had some vacations 😇 )
Comment on lines
+51
to
+56
| /** | ||
| * Generated file header | ||
| * | ||
| * @default "// Generated by ts-to-zod\nimport { z } from \"zod\";" | ||
| */ | ||
| headerText?: string; |
Comment on lines
+132
to
+162
| if (ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node)) { | ||
| if (schema !== undefined && node.typeParameters) { | ||
| const genericTypes = node | ||
| .typeParameters.map((p) => `${p.name.escapedText}`) | ||
| const genericDependencies = genericTypes.map((p) => getDependencyName(p)) | ||
| dependencies = dependencies | ||
| .filter((dep) => !genericDependencies.includes(dep)); | ||
| schema = f.createArrowFunction( | ||
| undefined, | ||
| genericTypes.map((dep) => f.createIdentifier(dep)), | ||
| genericTypes.map((dep) => f.createParameterDeclaration( | ||
| undefined, | ||
| undefined, | ||
| undefined, | ||
| f.createIdentifier(getDependencyName(dep)), | ||
| undefined, | ||
| f.createTypeReferenceNode( | ||
| f.createQualifiedName( | ||
| f.createIdentifier(zodImportValue), | ||
| f.createIdentifier(`ZodSchema<${dep}> = z.any()`) | ||
| ), | ||
| undefined | ||
| ), | ||
| undefined | ||
| )), | ||
| undefined, | ||
| f.createToken(ts.SyntaxKind.EqualsGreaterThanToken), | ||
| schema | ||
| ); | ||
| } | ||
| } |
Owner
There was a problem hiding this comment.
Let's add some unit tests on this
Comment on lines
+256
to
+258
| if (schema.startsWith(".")) { | ||
| return f.createPropertyAccessExpression(generatedSchema, f.createIdentifier(schema.slice(1))); | ||
| } |
Owner
There was a problem hiding this comment.
Let's add a unit test on this
Comment on lines
+554
to
+563
| // string-only enum? issue z.enum | ||
| if (typeNode.types.every((i) => | ||
| ts.isLiteralTypeNode(i) && i.literal.kind === ts.SyntaxKind.StringLiteral | ||
| )) { | ||
| return buildZodSchema( | ||
| z, | ||
| "enum", | ||
| [f.createArrayLiteralExpression(nodes.map((i) => (i as any)["literal"]))], | ||
| zodProperties | ||
| ); |
Owner
There was a problem hiding this comment.
Let's add a unit test on this
Comment on lines
+606
to
+612
| jsDocTags.discriminator !== undefined | ||
| ? "discriminatedUnion" | ||
| : "union", | ||
| jsDocTags.discriminator !== undefined | ||
| ? [f.createStringLiteral(jsDocTags.discriminator), | ||
| f.createArrayLiteralExpression(values)] | ||
| : [f.createArrayLiteralExpression(values)], |
Owner
There was a problem hiding this comment.
Same here, a little unit test would be perfect :)
Closed
schiller-manuel
added a commit
that referenced
this pull request
Sep 3, 2024
implementation taken from #125
schiller-manuel
added a commit
that referenced
this pull request
Sep 3, 2024
implementation taken from #125
schiller-manuel
added a commit
that referenced
this pull request
Sep 3, 2024
implementation taken from #125
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Why
To not let #124 go in vain.
Problems addressed
@schema .trim().email()or overriding via@schema z.coerce.int()z.enum([...])z.discriminatedUnion(kind, ...)if@discriminator kindspecifiedI believe some of them can be pulled.