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 for custom @format types #145

Merged
merged 7 commits into from Oct 20, 2023

Conversation

denk0403
Copy link
Contributor

@denk0403 denk0403 commented Jul 5, 2023

See #144.

TL;DR: Added support for more built-in @format types, as well as the ability to define custom string validation formats via the config file's customJSDocFormats property:

{
  "customJSDocFormats": {
    "phone-number": "^\\d{3}-\\d{3}-\\d{4}$",
    "date": {
      "regex": "^\\d{4}-\\d{2}-\\d{2}$",
      "errorMessage": "Must be in YYYY-MM-DD format."
    }
  }
}

A nice side-effect of this implementation is that custom format types are actually able to override the output of built-ins. For example, one can override the "email" format type like so:

// ts-to-zod.config.js
{ 
  // ...
  customJSDocFormats: {
    email: "^[A-Za-z]+@my_domain.com$",
  }
}

Then as a result, the generated Zod schema for properties with @format email will look like:

{
  /**
   * @format email
   */
  email: z.string().regex(/^[A-Za-z]+@my_domain.com$/)
}

What's left:

  • Built-in support for format types date-time, ip, ipv4, & ipv6.
  • Support for defining custom format types via the config file.
  • Updating the README with documentation for the new features.
  • A little cleanup and better code coverage.

@denk0403 denk0403 changed the title added support for "date-time", "ipv4", "ipv6", & "ip" @format types WIP: added support for "date-time", "ipv4", "ipv6", & "ip" @format types Jul 5, 2023
@denk0403 denk0403 changed the title WIP: added support for "date-time", "ipv4", "ipv6", & "ip" @format types WIP: aSupport for more @format types Jul 5, 2023
@denk0403 denk0403 changed the title WIP: aSupport for more @format types WIP: Support for more @format types Jul 5, 2023
@fabien0102
Copy link
Owner

Very cool, I can't wait to see this PR merge! Good job 👌

@codecov-commenter
Copy link

codecov-commenter commented Jul 5, 2023

Codecov Report

Merging #145 (89a56e3) into main (ac24784) will decrease coverage by 0.01%.
The diff coverage is 96.07%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

@@            Coverage Diff             @@
##             main     #145      +/-   ##
==========================================
- Coverage   97.32%   97.32%   -0.01%     
==========================================
  Files          14       14              
  Lines         674      711      +37     
  Branches      273      283      +10     
==========================================
+ Hits          656      692      +36     
- Misses         18       19       +1     
Files Coverage Δ
src/core/generate.ts 96.15% <100.00%> (+0.03%) ⬆️
src/core/generateZodSchema.ts 96.16% <ø> (ø)
src/core/jsDocTags.ts 98.50% <96.00%> (-0.48%) ⬇️

@denk0403 denk0403 marked this pull request as ready for review July 10, 2023 10:35
@denk0403 denk0403 changed the title WIP: Support for more @format types Support for more @format types Jul 10, 2023
@denk0403 denk0403 changed the title Support for more @format types WIP (Do not merge): Support for more @format types Jul 10, 2023
@denk0403 denk0403 changed the title WIP (Do not merge): Support for more @format types Support for more @format types Jul 10, 2023
@fabien0102 fabien0102 self-requested a review October 17, 2023 09:33
@fabien0102 fabien0102 self-assigned this Oct 17, 2023
@fabien0102 fabien0102 changed the title Support for more @format types feat: Support for custom @format types Oct 20, 2023
Copy link
Owner

@fabien0102 fabien0102 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing job!!! Thanks for this cool addition! I did modify the code style to fit the rest of the codebase.

This will be part of the next release! (and sorry for the very late review, I'm a totally out of my side-projects thoses days)

@fabien0102 fabien0102 merged commit acd9444 into fabien0102:main Oct 20, 2023
1 of 3 checks passed
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

3 participants