Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: Fix optional array (#20)
closed #18
  • Loading branch information
fabien0102 committed May 3, 2021
1 parent 6cf4a32 commit ae61041
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
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

0 comments on commit ae61041

Please sign in to comment.