-
Notifications
You must be signed in to change notification settings - Fork 70
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
add some drafts #125
base: main
Are you sure you want to change the base?
add some drafts #125
Conversation
96f36d3
to
5f55cd6
Compare
This looks cool, let’s remove the noises (commented code & todo) and ship some features from this PR :) |
Cleaned up and rebased. |
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.
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 😇 )
/** | ||
* Generated file header | ||
* | ||
* @default "// Generated by ts-to-zod\nimport { z } from \"zod\";" | ||
*/ | ||
headerText?: string; |
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.
👌
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 | ||
); | ||
} | ||
} |
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.
Let's add some unit tests on this
if (schema.startsWith(".")) { | ||
return f.createPropertyAccessExpression(generatedSchema, f.createIdentifier(schema.slice(1))); | ||
} |
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.
Let's add a unit test on this
// 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 | ||
); |
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.
Let's add a unit test on this
jsDocTags.discriminator !== undefined | ||
? "discriminatedUnion" | ||
: "union", | ||
jsDocTags.discriminator !== undefined | ||
? [f.createStringLiteral(jsDocTags.discriminator), | ||
f.createArrayLiteralExpression(values)] | ||
: [f.createArrayLiteralExpression(values)], |
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.
Same here, a little unit test would be perfect :)
implementation taken from #125
implementation taken from #125
implementation taken from #125
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 kind
specifiedI believe some of them can be pulled.