mrjono1 / joi-to-typescript

Convert Joi Schemas to TypeScript interfaces
MIT License
125 stars 39 forks source link

joi object pattern outputting type unknown #351

Closed dkhunt27 closed 1 year ago

dkhunt27 commented 1 year ago

When using joi object pattern, seems to be outputting that the pattern is unknown in the outputted type/interface

To Reproduce Run the javascript below. Will need to install joi-to-typescript and joi as dependencies of project. I am using

{
  ...
  "dependencies": {
    "joi": "^17.9.2",
    "joi-to-typescript": "^4.5.0"
  }
}
const { convertSchema } = require('joi-to-typescript');
const joi = require('joi');

const schema = joi.object().pattern(
  joi.string(),
  joi.string(),
).meta({ className: 'TestType' });

joi.attempt({'a': 'b'}, schema);  // correctly allows {'a': 'b'}; this will error correctly on {'a': 1}

const result = convertSchema({}, schema);
console.log(result.content)

Expected behavior Expect the output to be

export interface TestType {
  [x: string]: string;
}

Actual behavior actually getting the following

export interface TestType {
  /**
   * Unknown Property
   */
  [x: string]: unknown;
}
mrjono1 commented 1 year ago

Hi, not much work as been done on .pattern() as a workaround this should work

 const schema = joi.object().pattern(
  joi.string(),
  joi.string(),
).meta({ className: 'TestType' },{ unknownType: 'string' });
mrjono1 commented 1 year ago

closing due to no response