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: handle undefined properties as optional during validation phase #240

Merged
merged 1 commit into from
May 13, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions src/core/validateGeneratedTypes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,106 @@ describe("validateGeneratedTypes", () => {
expect(errors).toEqual([]);
});

it("should return no error if we use a non-optional undefined", () => {
const sourceTypes = {
sourceText: `
export interface Citizen {
villain: undefined;
};
`,
relativePath: "source.ts",
};

const zodSchemas = {
sourceText: `// Generated by ts-to-zod
import { z } from "zod";
export const citizenSchema = z.object({
villain: z.undefined()
});
`,
relativePath: "source.zod.ts",
};

const integrationTests = {
sourceText: `// Generated by ts-to-zod
import { z } from "zod";

import * as spec from "./${sourceTypes.relativePath.slice(0, -3)}";
import * as generated from "./${zodSchemas.relativePath.slice(0, -3)}";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function expectType<T>(_: T) {
/* noop */
}

export type CitizenInferredType = z.infer<typeof generated.citizenSchema>;

expectType<CitizenInferredType>({} as spec.Citizen);
expectType<spec.Citizen>({} as CitizenInferredType);
`,
relativePath: "source.integration.ts",
};

const errors = validateGeneratedTypes({
sourceTypes,
zodSchemas,
integrationTests,
skipParseJSDoc: false,
});

expect(errors).toEqual([]);
});

it("should return no error if we use a non-optional undefined in union type", () => {
const sourceTypes = {
sourceText: `
export interface Citizen {
villain: string | undefined;
};
`,
relativePath: "source.ts",
};

const zodSchemas = {
sourceText: `// Generated by ts-to-zod
import { z } from "zod";
export const citizenSchema = z.object({
villain: z.union([z.string(), z.undefined()])
});
`,
relativePath: "source.zod.ts",
};

const integrationTests = {
sourceText: `// Generated by ts-to-zod
import { z } from "zod";

import * as spec from "./${sourceTypes.relativePath.slice(0, -3)}";
import * as generated from "./${zodSchemas.relativePath.slice(0, -3)}";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function expectType<T>(_: T) {
/* noop */
}

export type CitizenInferredType = z.infer<typeof generated.citizenSchema>;

expectType<CitizenInferredType>({} as spec.Citizen);
expectType<spec.Citizen>({} as CitizenInferredType);
`,
relativePath: "source.integration.ts",
};

const errors = validateGeneratedTypes({
sourceTypes,
zodSchemas,
integrationTests,
skipParseJSDoc: false,
});

expect(errors).toEqual([]);
});

it("should return an error if the types doesn't match", () => {
const sourceTypes = {
sourceText: `
Expand Down
7 changes: 6 additions & 1 deletion src/utils/fixOptionalAny.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import ts, { factory as f } from "typescript";

/**
* Add optional property to `any` or imported types to workaround comparison issue.
* Add optional property to `any`, `undefined` or imported types to workaround comparison issue.
*
* ref:
* -> https://github.com/fabien0102/ts-to-zod/issues/140
* -> https://github.com/fabien0102/ts-to-zod/issues/203
* -> https://github.com/fabien0102/ts-to-zod/issues/239
*
*/
export function fixOptionalAny(
Expand All @@ -14,8 +15,12 @@ export function fixOptionalAny(
) {
function shouldAddQuestionToken(node: ts.TypeNode) {
return (
// https://github.com/fabien0102/ts-to-zod/issues/140
node.kind === ts.SyntaxKind.AnyKeyword ||
// https://github.com/fabien0102/ts-to-zod/issues/239
node.kind === ts.SyntaxKind.UndefinedKeyword ||
// Handling type referencing imported types
// https://github.com/fabien0102/ts-to-zod/issues/203
(ts.isTypeReferenceNode(node) &&
importsToHandleAsAny.has(node.typeName.getText(sourceFile)))
);
Expand Down