mrjono1 / joi-to-typescript

Convert Joi Schemas to TypeScript interfaces
MIT License
126 stars 40 forks source link

Joi.ref is breaking #233

Closed toddtarsi closed 2 years ago

toddtarsi commented 2 years ago

Schema:

const schema = Joi.object()
  .label('SignUp')
  .keys({
    password: Joi.string()
      .required()
      .description('The password of the authenticating user')
      .example('test-PASSWORD123'),
    repeatPassword: Joi.string()
      .required()
      .allow(Joi.ref('password'))
      .description('Repeat the password to ensure no typos')
      .example('test-PASSWORD123')
  });

Result:

joi-to-typescript/dist/main/utils.js:26
    return `'${value.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`;
                     ^

TypeError: value.replace is not a function
    at toStringLiteral (joi-to-typescript/dist/main/utils.js:26:22)
    at joi-to-typescript/dist/main/parse.js:341:92
    at Array.map (<anonymous>)
    at parseStringSchema (joi-to-typescript/dist/main/parse.js:339:42)
    at parseHelper (joi-to-typescript/dist/main/parse.js:189:20)
    at parseSchema (joi-to-typescript/dist/main/parse.js:265:26)
    at joi-to-typescript/dist/main/parse.js:423:30
    at joi-to-typescript/dist/main/utils.js:13:27
    at Array.reduce (<anonymous>)
    at filterMap (joi-to-typescript/dist/main/utils.js:12:17)

Shape returned at this line:

{ ref: { path: [ 'password' ] } }

Is it possible for me to just skip all processing on this field using a meta flag or something?

mrjono1 commented 2 years ago

its this line .allow(Joi.ref('password')) it looks like a null check is needed to resolve this issue

mrjono1 commented 2 years ago

A fix for this https://github.com/mrjono1/joi-to-typescript/pull/234

toddtarsi commented 2 years ago

🤯 - Holy cow! You resolved that so fast. Thank you so much! Also, please don't let any of the people on things I maintain know that you can fix things this fast 😆 😭

mrjono1 commented 2 years ago

I broke my npm releases, so i have to fix that before I can get the change to you

mrjono1 commented 2 years ago

fixed released under 4.0.3