mrjono1 / joi-to-typescript

Convert Joi Schemas to TypeScript interfaces
MIT License
124 stars 38 forks source link

Ignore the `{"special": "deep"}` default value introduced by Joi when invoking the `default()` function without arguments. #433

Closed cmaster11 closed 4 months ago

cmaster11 commented 4 months ago

Ignore the {"special": "deep"} default value introduced by Joi when invoking the default() function without arguments.

Ref: https://github.com/hapijs/joi/blob/e7e9c5d18dafaa510a7ece02c225653db5fc998f/lib/manifest.js#L179

Without this fix, the generated TS code would look like:

export interface Test {"special":"deep"} | {
  num: 1 | number;
  str: "Test" | string;
  strWithSpecialChars: "Test\\World$Hello🚀Hey\nYay" | string;
}

Note: there is an edge case here where if someone were to use the {"special":"deep"} values as default, this would not be printed in the generated type default. I think it's an acceptable "bug" because the Joi library dumps this value in the result of the describe function without providing additional information.


Also, fix the generated types in case we have objects with default values, where we then need to use export type = instead of export interface because the latter one is invalid with a union.

So, instead of the invalid

export interface Test {"something":"deep"} | {
  something?: string;
}

we get the proper

export type Test = {"something":"deep"} | {
  something?: string;
}
codecov[bot] commented 4 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 98.48%. Comparing base (9eb921f) to head (2cb6f45). Report is 20 commits behind head on master.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #433 +/- ## ========================================== + Coverage 98.46% 98.48% +0.02% ========================================== Files 9 9 Lines 650 661 +11 Branches 251 266 +15 ========================================== + Hits 640 651 +11 Misses 10 10 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

cmaster11 commented 4 months ago

Ping @mrjono1? :D