Skip to content

Commit

Permalink
feat: support custom zod error message (#73)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Carlo <nicolascarlo.espeon@gmail.com>
  • Loading branch information
fabien0102 and nicoespeon committed Feb 25, 2022
1 parent b2bb1af commit 36964b3
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 16 deletions.
100 changes: 99 additions & 1 deletion src/core/generateZodSchema.test.ts
Expand Up @@ -669,11 +669,109 @@ describe("generateZodSchema", () => {
`);
});

it("should generate custom error message for `format` tag", () => {
const source = `export interface HeroContact {
/**
* The email of the hero.
*
* @format email Should be an email
*/
heroEmail: string;
/**
* The email of the enemy.
*
* @format email, "Should be an email"
*/
enemyEmail: string;
/**
* The email of the superman.
*
* @format email "Should be an email"
*/
supermanEmail: string;
}`;
expect(generate(source)).toMatchInlineSnapshot(`
"export const heroContactSchema = z.object({
/**
* The email of the hero.
*
* @format email Should be an email
*/
heroEmail: z.string().email(\\"Should be an email\\"),
/**
* The email of the enemy.
*
* @format email, \\"Should be an email\\"
*/
enemyEmail: z.string().email(\\"Should be an email\\"),
/**
* The email of the superman.
*
* @format email \\"Should be an email\\"
*/
supermanEmail: z.string().email(\\"Should be an email\\")
});"
`);
});

it("should generate custom error message based on jsdoc tags", () => {
const source = `export interface HeroContact {
/**
* The email of the hero.
*
* @format email should be an email
*/
email: string;
/**
* The name of the hero.
*
* @minLength 2, should be more than 2
* @maxLength 50 should be less than 50
*/
name: string;
/**
* The age of the hero
*
* @minimum 0 you are too young
* @maximum 500, "you are too old"
*/
age: number;
}`;
expect(generate(source)).toMatchInlineSnapshot(`
"export const heroContactSchema = z.object({
/**
* The email of the hero.
*
* @format email should be an email
*/
email: z.string().email(\\"should be an email\\"),
/**
* The name of the hero.
*
* @minLength 2, should be more than 2
* @maxLength 50 should be less than 50
*/
name: z.string().min(2, \\"should be more than 2\\").max(50, \\"should be less than 50\\"),
/**
* The age of the hero
*
* @minimum 0 you are too young
* @maximum 500, \\"you are too old\\"
*/
age: z.number().min(0, \\"you are too young\\").max(500, \\"you are too old\\")
});"
`);
});

it("should generate validator on top-level types", () => {
const source = `/**
* @minLength 1
*/
export type NonEmptyString = string;`;
export type NonEmptyString = string;`;

expect(generate(source)).toMatchInlineSnapshot(`
"/**
Expand Down
81 changes: 66 additions & 15 deletions src/core/jsDocTags.ts
Expand Up @@ -14,16 +14,21 @@ const formats = [
// "date-time" as const,
];

type TagWithError<T> = {
value: T;
errorMessage?: string;
};

/**
* JSDoc special tags that can be converted in zod flags.
*/
export interface JSDocTags {
minimum?: number;
maximum?: number;
minimum?: TagWithError<number>;
maximum?: TagWithError<number>;
default?: number | string | boolean;
minLength?: number;
maxLength?: number;
format?: typeof formats[-1];
minLength?: TagWithError<number>;
maxLength?: TagWithError<number>;
format?: TagWithError<typeof formats[-1]>;
pattern?: string;
}

Expand Down Expand Up @@ -52,10 +57,32 @@ function isJSDocTagKey(tagName: string): tagName is keyof JSDocTags {
*/
function isSupportedFormat(
format = ""
): format is Required<JSDocTags>["format"] {
): format is Required<JSDocTags>["format"]["value"] {
return (formats as string[]).includes(format);
}

/**
* Parse js doc comment.
*
* @example
* parseJsDocComment("email should be an email");
* // {value: "email", errorMessage: "should be an email"}
*
* @param comment
*/
function parseJsDocComment(
comment: string
): { value: string; errorMessage?: string } {
const [value, ...rest] = comment.split(" ");
const errorMessage =
rest.join(" ").replace(/(^["']|["']$)/g, "") || undefined;

return {
value: value.replace(",", "").replace(/(^["']|["']$)/g, ""),
errorMessage,
};
}

/**
* Return parsed JSTags.
*
Expand All @@ -71,13 +98,15 @@ export function getJSDocTags(nodeType: ts.Node, sourceFile: ts.SourceFile) {
(doc.tags || []).forEach((tag) => {
const tagName = tag.tagName.escapedText.toString();
if (!isJSDocTagKey(tagName) || typeof tag.comment !== "string") return;
const { value, errorMessage } = parseJsDocComment(tag.comment);

switch (tagName) {
case "minimum":
case "maximum":
case "minLength":
case "maxLength":
if (tag.comment && !Number.isNaN(parseInt(tag.comment))) {
jsDocTags[tagName] = parseInt(tag.comment);
if (value && !Number.isNaN(parseInt(value))) {
jsDocTags[tagName] = { value: parseInt(value), errorMessage };
}
break;
case "pattern":
Expand All @@ -86,8 +115,8 @@ export function getJSDocTags(nodeType: ts.Node, sourceFile: ts.SourceFile) {
}
break;
case "format":
if (isSupportedFormat(tag.comment)) {
jsDocTags[tagName] = tag.comment;
if (isSupportedFormat(value)) {
jsDocTags[tagName] = { value, errorMessage };
}
break;
case "default":
Expand Down Expand Up @@ -146,30 +175,45 @@ export function jsDocTagToZodProperties(
if (jsDocTags.minimum !== undefined) {
zodProperties.push({
identifier: "min",
expressions: [f.createNumericLiteral(jsDocTags.minimum)],
expressions: withErrorMessage(
f.createNumericLiteral(jsDocTags.minimum.value),
jsDocTags.minimum.errorMessage
),
});
}
if (jsDocTags.maximum !== undefined) {
zodProperties.push({
identifier: "max",
expressions: [f.createNumericLiteral(jsDocTags.maximum)],
expressions: withErrorMessage(
f.createNumericLiteral(jsDocTags.maximum.value),
jsDocTags.maximum.errorMessage
),
});
}
if (jsDocTags.minLength !== undefined) {
zodProperties.push({
identifier: "min",
expressions: [f.createNumericLiteral(jsDocTags.minLength)],
expressions: withErrorMessage(
f.createNumericLiteral(jsDocTags.minLength.value),
jsDocTags.minLength.errorMessage
),
});
}
if (jsDocTags.maxLength !== undefined) {
zodProperties.push({
identifier: "max",
expressions: [f.createNumericLiteral(jsDocTags.maxLength)],
expressions: withErrorMessage(
f.createNumericLiteral(jsDocTags.maxLength.value),
jsDocTags.maxLength.errorMessage
),
});
}
if (jsDocTags.format) {
zodProperties.push({
identifier: jsDocTags.format,
identifier: jsDocTags.format.value,
expressions: jsDocTags.format.errorMessage
? [f.createStringLiteral(jsDocTags.format.errorMessage)]
: undefined,
});
}
if (jsDocTags.pattern) {
Expand Down Expand Up @@ -214,3 +258,10 @@ export function jsDocTagToZodProperties(

return zodProperties;
}

function withErrorMessage(expression: ts.Expression, errorMessage?: string) {
if (errorMessage) {
return [expression, f.createStringLiteral(errorMessage)];
}
return [expression];
}

0 comments on commit 36964b3

Please sign in to comment.