TCMiranda / joi-extract-type

Provides native type extraction from Joi schemas for Typescript
MIT License
151 stars 27 forks source link

Breaks when joi.extend is used #29

Closed azai91 closed 4 years ago

azai91 commented 4 years ago

Love this library. I currently have custom extensions that I use to extend the joi library. However, as soon as I use the Joi.extend function to add custom joi types then joi-extract-types no longer works.

const numberExtensions = (joi : any) : Extension => ({
  base: joi.number(),
  name: 'number',
  language: {
    cLatitude: 'needs to be a valid latitude',
    cLongitude: 'need to be valid longitude'
  },
  rules: [
    {
      name: 'cLatitude',
      validate(params, value, state, options) {
        if (value >= -90 && value <= 90) {
          return value;
        }
        // Generate an error, state and options need to be passed
        return this.createError('number.cLatitude', {value}, state, options);
      }
    },
    {
      name: 'cLongitude',
      validate(params, value, state, options) {
        if (value >= -180 && value <= 180) {
          return value;
        }
        // Generate an error, state and options need to be passed
        return this.createError('number.cLongitude', {value}, state, options);
      }
    }
  ]
});

module.exports = joi => (joi.extend(numberExtensions));

If I extend Joi to use the above extension then all types become any. Please let me know if there is a workaround for this. Thanks!

wspringer commented 4 years ago

@azai91 Did you manage to work around it?