Skip to content
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

Fix optional array #20

Merged
merged 1 commit into from May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/core/generateZodSchema.test.ts
Expand Up @@ -68,9 +68,9 @@ describe("generateZodSchema", () => {
});

it("should generate a literal schema (number)", () => {
const source = `export type IdentiesCount = 2;`;
const source = `export type IdentitiesCount = 2;`;
expect(generate(source)).toMatchInlineSnapshot(
`"export const identiesCountSchema = z.literal(2);"`
`"export const identitiesCountSchema = z.literal(2);"`
);
});

Expand Down Expand Up @@ -164,9 +164,9 @@ describe("generateZodSchema", () => {
});

it("should generate a record schema", () => {
const source = `export type EnnemiesPowers = Record<string, Power>;`;
const source = `export type EnemiesPowers = Record<string, Power>;`;
expect(generate(source)).toMatchInlineSnapshot(
`"export const ennemiesPowersSchema = z.record(powerSchema);"`
`"export const enemiesPowersSchema = z.record(powerSchema);"`
);
});

Expand Down Expand Up @@ -368,6 +368,17 @@ describe("generateZodSchema", () => {
`);
});

it("should deal with optional array", () => {
const source = `export interface Collection {
movies?: Array<string>
}`;
expect(generate(source)).toMatchInlineSnapshot(`
"export const collectionSchema = z.object({
movies: z.array(z.string()).optional()
});"
`);
});

it("should generate an empty object schema", () => {
const source = `export type Empty = {};`;
expect(generate(source)).toMatchInlineSnapshot(
Expand Down
2 changes: 1 addition & 1 deletion src/core/generateZodSchema.ts
Expand Up @@ -212,7 +212,7 @@ function buildZodPrimitive({
return buildZodPrimitive({
z,
typeNode: f.createArrayTypeNode(typeNode.typeArguments[0]),
isOptional: false,
isOptional,
jsDocTags: {},
sourceFile,
dependencies,
Expand Down