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

Adds support for use of enum types as literals and nativeEnums #40

Merged
merged 9 commits into from Jul 29, 2021

Conversation

anglinb
Copy link
Contributor

@anglinb anglinb commented Jul 29, 2021

Why

Fixes #39 and #35

Warnings ⚠️

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.

Looks good, let's just add a little unit test to make sure everything works as expected

src/core/generateZodSchema.ts Show resolved Hide resolved
@codecov-commenter
Copy link

Codecov Report

Merging #40 (814a5ce) into main (e5e4961) will decrease coverage by 0.39%.
The diff coverage is 98.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #40      +/-   ##
==========================================
- Coverage   98.49%   98.10%   -0.40%     
==========================================
  Files           8        9       +1     
  Lines         332      369      +37     
  Branches      102      113      +11     
==========================================
+ Hits          327      362      +35     
- Misses          5        6       +1     
- Partials        0        1       +1     
Impacted Files Coverage Δ
src/utils/resolveDefaultProperties.ts 95.23% <95.23%> (ø)
src/core/generate.ts 100.00% <100.00%> (ø)
src/core/generateZodSchema.ts 97.41% <100.00%> (+0.21%) ⬆️
src/core/jsDocTags.ts 98.46% <100.00%> (-1.54%) ⬇️
src/core/validateGeneratedTypes.ts 97.43% <100.00%> (+0.06%) ⬆️

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 d6ae0a1...814a5ce. Read the comment docs.

@anglinb anglinb changed the title Adds support for use of enum types as literals Adds support for use of enum types as literals and nativeEnums Jul 29, 2021
@anglinb
Copy link
Contributor Author

anglinb commented Jul 29, 2021

I think I addressed all the feedback, thanks again for taking a look @fabien0102 🙇

@anglinb anglinb requested a review from fabien0102 July 29, 2021 17:53
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.

Looks good, nice job! I will publish a new release just after 😀

@fabien0102 fabien0102 merged commit 8bf26ed into fabien0102:main Jul 29, 2021
fabien0102 pushed a commit that referenced this pull request Jul 29, 2021
…40)

* Checks in test documenting behavior

* Implements enum import & declartion

* Updates example to include enum

* Updates tests to document expected case

* Add handler for enum literals

* Updates example

* Addresses #38 (comment)

* Addresses #38 (comment)

* Addresses #40 (comment)
@dvv
Copy link

dvv commented Mar 27, 2023

I wonder whether the following is a bug:

export enum LoggerLevel { debug, info, warning, error, critical }
interface ILogger {
  /**
   * @default LoggerLevel.info
   */
  level: LoggerLevel
}

compiles down to

export const loggerLevelSchema = z.nativeEnum(LoggerLevel);
const iLoggerSchema = z.object({
  level: loggerLevelSchema.default("LoggerLevel.info"), //// <----- notice enum default is quoted
});

TIA

@fabien0102
Copy link
Owner

This looks like a bug, we didn’t think about this edge case. Good catch!

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.

Falls back to z.any() for enum type literals and fails validation
4 participants