Skip to content

Commit

Permalink
fix: support numeric literal keys (#120)
Browse files Browse the repository at this point in the history
* fix: support numeric literal keys

* Remove unused parameters
  • Loading branch information
fabien0102 committed Mar 13, 2023
1 parent 20e18d3 commit 7bbed16
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
16 changes: 7 additions & 9 deletions src/core/generate.ts
Expand Up @@ -198,15 +198,13 @@ export function generate({
!statements.has(varName) &&
!zodSchemasWithMissingDependencies.has(varName)
)
.forEach(
({ varName, dependencies, statement, typeName, requiresImport }) => {
typeImports.add(typeName);
statements.set(varName, {
value: transformRecursiveSchema("z", statement, typeName),
typeName,
});
}
);
.forEach(({ varName, statement, typeName }) => {
typeImports.add(typeName);
statements.set(varName, {
value: transformRecursiveSchema("z", statement, typeName),
typeName,
});
});

// Warn the user of possible not resolvable loops
const errors: string[] = [];
Expand Down
24 changes: 24 additions & 0 deletions src/core/generateZodSchema.test.ts
Expand Up @@ -177,6 +177,30 @@ describe("generateZodSchema", () => {
`);
});

it("should generate a numerical key", () => {
const source = `export type responses = {
200: {
content: {
"application/json": {
id: string
}
}
}
};`;

expect(generate(source)).toMatchInlineSnapshot(`
"export const responsesSchema = z.object({
200: z.object({
content: z.object({
\\"application/json\\": z.object({
id: z.string()
})
})
})
});"
`);
});

it("should generate a promise schema", () => {
const source = `export type KrytonResponse = Promise<boolean>`;
expect(generate(source)).toMatchInlineSnapshot(
Expand Down
14 changes: 9 additions & 5 deletions src/core/generateZodSchema.ts
Expand Up @@ -169,14 +169,18 @@ function buildZodProperties({
skipParseJSDoc: boolean;
}) {
const properties = new Map<
ts.Identifier | ts.StringLiteral,
ts.Identifier | ts.StringLiteral | ts.NumericLiteral,
ts.CallExpression | ts.Identifier | ts.PropertyAccessExpression
>();
members.forEach((member) => {
if (
!ts.isPropertySignature(member) ||
!member.type ||
!(ts.isIdentifier(member.name) || ts.isStringLiteral(member.name))
!(
ts.isIdentifier(member.name) ||
ts.isStringLiteral(member.name) ||
ts.isNumericLiteral(member.name)
)
) {
return;
}
Expand Down Expand Up @@ -894,15 +898,15 @@ function buildZodObject({
getDependencyName,
skipParseJSDoc,
})
: undefined;
: new Map();

if (schemaExtensionClauses && schemaExtensionClauses.length > 0) {
objectSchema = buildZodExtendedSchema(
schemaExtensionClauses,
properties.length > 0
? [
f.createObjectLiteralExpression(
Array.from(parsedProperties!.entries()).map(([key, tsCall]) => {
Array.from(parsedProperties.entries()).map(([key, tsCall]) => {
return f.createPropertyAssignment(key, tsCall);
}),
true
Expand All @@ -913,7 +917,7 @@ function buildZodObject({
} else if (properties.length > 0) {
objectSchema = buildZodSchema(z, "object", [
f.createObjectLiteralExpression(
Array.from(parsedProperties!.entries()).map(([key, tsCall]) => {
Array.from(parsedProperties.entries()).map(([key, tsCall]) => {
return f.createPropertyAssignment(key, tsCall);
}),
true
Expand Down

0 comments on commit 7bbed16

Please sign in to comment.