Skip to content

Commit

Permalink
feat: Add skipParseJSDoc option (#62)
Browse files Browse the repository at this point in the history
* Update oclif

* Add `skipParseJSDoc` option

* Fix jest snapshot generation

* Test generation without jsDoc

* Add unit test on validateGeneratedTypes

* Fix default value
  • Loading branch information
fabien0102 committed Dec 3, 2021
1 parent 1d9d8c4 commit e48c5fe
Show file tree
Hide file tree
Showing 11 changed files with 1,114 additions and 1,695 deletions.
8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -37,7 +37,7 @@
],
"license": "MIT",
"dependencies": {
"@oclif/command": "^1.8.4",
"@oclif/command": "^1.8.6",
"@oclif/config": "^1.17.1",
"@oclif/plugin-help": "^3.2.7",
"@typescript/vfs": "^1.3.5",
Expand All @@ -49,7 +49,7 @@
"lodash": "^4.17.21",
"lz-string": "^1.4.4",
"ora": "^5.4.0",
"prettier": "^2.5.0",
"prettier": "2.2.1",
"rxjs": "^7.4.0",
"slash": "^3.0.0",
"threads": "^1.7.0",
Expand All @@ -74,10 +74,10 @@
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^3.3.1",
"husky": "^5.2.0",
"jest": "^26.6.3",
"jest": "^27.4.3",
"pretty-quick": "^3.1.0",
"rimraf": "^3.0.2",
"ts-jest": "^26.5.3",
"ts-jest": "^27.0.7",
"ts-node": "^9.1.1"
},
"jest": {
Expand Down
8 changes: 8 additions & 0 deletions src/cli.ts
Expand Up @@ -80,6 +80,10 @@ class TsToZod extends Command {
char: "i",
description: "Create a ts-to-zod.config.js file",
}),
skipParseJSDoc: flags.boolean({
default: false,
description: "Skip the creation of zod validators from JSDoc annotations",
}),
skipValidation: flags.boolean({
default: false,
description: "Skip the validation step (not recommended)",
Expand Down Expand Up @@ -240,6 +244,9 @@ See more help with --help`,
if (typeof flags.keepComments === "boolean") {
generateOptions.keepComments = flags.keepComments;
}
if (typeof flags.skipParseJSDoc === "boolean") {
generateOptions.skipParseJSDoc = flags.skipParseJSDoc;
}

const {
errors,
Expand Down Expand Up @@ -275,6 +282,7 @@ See more help with --help`,
sourceText: getZodSchemasFile("./source"),
relativePath: "./source.zod.ts",
},
skipParseJSDoc: Boolean(generateOptions.skipParseJSDoc),
});

generationErrors.length
Expand Down
7 changes: 7 additions & 0 deletions src/config.ts
Expand Up @@ -37,6 +37,13 @@ export type Config = {
* @default false
*/
keepComments?: boolean;

/**
* Skip the creation of zod validators from JSDoc annotations
*
* @default false
*/
skipParseJSDoc?: boolean;
};

export type Configs = Array<
Expand Down
1 change: 1 addition & 0 deletions src/config.zod.ts
Expand Up @@ -19,6 +19,7 @@ export const configSchema = z.object({
nameFilter: nameFilterSchema.optional(),
getSchemaName: getSchemaNameSchema.optional(),
keepComments: z.boolean().optional().default(false),
skipParseJSDoc: z.boolean().optional().default(false),
});

export const configsSchema = z.array(
Expand Down
9 changes: 9 additions & 0 deletions src/core/generate.ts
Expand Up @@ -32,6 +32,13 @@ export interface GenerateProps {
* @default false
*/
keepComments?: boolean;

/**
* Skip the creation of zod validators from JSDoc annotations
*
* @default false
*/
skipParseJSDoc?: boolean;
}

/**
Expand All @@ -45,6 +52,7 @@ export function generate({
nameFilter = () => true,
getSchemaName = (id) => camel(id) + "Schema",
keepComments = false,
skipParseJSDoc = false,
}: GenerateProps) {
// Create a source file and deal with modules
const sourceFile = resolveModules(sourceText);
Expand Down Expand Up @@ -77,6 +85,7 @@ export function generate({
sourceFile,
varName,
getDependencyName: getSchemaName,
skipParseJSDoc,
});

return { typeName, varName, ...zodSchema };
Expand Down
25 changes: 24 additions & 1 deletion src/core/generateZodSchema.test.ts
Expand Up @@ -807,6 +807,28 @@ describe("generateZodSchema", () => {
`"export const theLastTestSchema = zod.literal(true);"`
);
});

it("should not generate any zod validation if `skipParseJSDoc` is `true`", () => {
const source = `export interface A {
/** @minimum 0 */
a: number | null;
/** @minLength 1 */
b: string | null;
/** @pattern ^c$ */
c: string | null;
}`;

expect(generate(source, "z", true)).toMatchInlineSnapshot(`
"export const aSchema = z.object({
/** @minimum 0 */
a: z.number().nullable(),
/** @minLength 1 */
b: z.string().nullable(),
/** @pattern ^c$ */
c: z.string().nullable()
});"
`);
});
});

/**
Expand All @@ -815,7 +837,7 @@ describe("generateZodSchema", () => {
* @param sourceText Typescript interface or type
* @returns Generated Zod schema
*/
function generate(sourceText: string, z?: string) {
function generate(sourceText: string, z?: string, skipParseJSDoc?: boolean) {
const sourceFile = ts.createSourceFile(
"index.ts",
sourceText,
Expand Down Expand Up @@ -844,6 +866,7 @@ function generate(sourceText: string, z?: string) {
node: declaration,
sourceFile,
varName: zodConstName,
skipParseJSDoc,
});
return ts
.createPrinter({ newLine: ts.NewLineKind.LineFeed })
Expand Down

0 comments on commit e48c5fe

Please sign in to comment.