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

feat: support custom zod error message #73

Merged
merged 1 commit into from Feb 25, 2022

Conversation

fabien0102
Copy link
Owner

@fabien0102 fabien0102 commented Feb 25, 2022

Summary

Add the possibility to add custom error message to zod validation.

Example:

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;
    }

will produce

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")
      });

Related issue

#55

Notes

  • The pattern is not supported, it's actually really hard to differenciate the pattern and the error message.
  • I didn't add required keyword also, this is touching a bit more files and I'm not totally sure about the pattern

In the context of an express middleware, as in the original issue, I think (and this is totally personnal) I would go on a global error management logic (https://github.com/colinhacks/zod/blob/master/ERROR_HANDLING.md) rather than specify everything manually in a JSDoc tag. So the error messages are standard and you can just tweak the edge cases.

Co-authored-by: Nicolas Carlo <nicolascarlo.espeon@gmail.com>
@codecov-commenter
Copy link

Codecov Report

Merging #73 (ff8e7dd) into main (e5e4961) will decrease coverage by 1.27%.
The diff coverage is 96.07%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #73      +/-   ##
==========================================
- Coverage   98.49%   97.22%   -1.28%     
==========================================
  Files           8       12       +4     
  Lines         332      504     +172     
  Branches      102      162      +60     
==========================================
+ Hits          327      490     +163     
- Misses          5       11       +6     
- Partials        0        3       +3     
Impacted Files Coverage Δ
src/core/generateZodSchema.ts 95.23% <92.40%> (-1.97%) ⬇️
src/utils/resolveDefaultProperties.ts 95.23% <95.23%> (ø)
src/utils/resolveModules.ts 97.87% <97.87%> (ø)
src/core/generate.ts 100.00% <100.00%> (ø)
src/core/jsDocTags.ts 98.66% <100.00%> (-1.34%) ⬇️
src/core/validateGeneratedTypes.ts 97.50% <100.00%> (+0.13%) ⬆️
src/utils/getSimplifiedJsDocTags.ts 100.00% <100.00%> (ø)
src/utils/isNotNull.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b2bb1af...ff8e7dd. Read the comment docs.

@fabien0102 fabien0102 merged commit 36964b3 into main Feb 25, 2022
@fabien0102 fabien0102 deleted the error-message-on-validation branch February 25, 2022 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants