Twipped / joi-to-swagger

A library to convert Joi schema objects into Swagger schema definitions
Other
164 stars 63 forks source link

[FEATURE] Support for schema.tailor and schema.alter #66

Closed Aposhian closed 4 years ago

Aposhian commented 4 years ago

In Joi, you can create subtle variants of a schema using alter and tailor. This is not reflected in the joi-to-swagger conversion.

For example,

const MySchema = Joi.object({
  property1: Joi.string(),
  property2: Joi.string().alter({
    special: schema => schema.strip()
  })
})

const SpecialSchema = MySchema.tailor('special')

still produces

"SpecialSchema": {
  "type": "object",
  "properties": {
    "property1": {
      "type": "string"
    },
    "property2": {
      "type": "string"
    }
  }
}
Mairu commented 4 years ago

Any PRs are welcome ;)

Aposhian commented 4 years ago

Upon further investigation, I was mistaken in stating that alter and tailor were being ignored. I was unclear about the behavior of strip().

I actually think that joi-to-swagger is handling this correctly, since strip() removes the property after validation.

forbidden() successfully removes the property in the resulting swagger schema, even inside an alter.

So now my question is how to basically strip without validation, but without explicitly forbidding the property. But since that is more of a Joi question, I'm going to close this.

const MySchema = Joi.object({
  myProperty: Joi.string().alter({
    thisRemovesProperty: schema => schema.forbidden(),
    thisKeepsProperty: schema => schema.strip()
  })
})