amritashan
amritashan

Reputation: 583

OpenAPI: Mandatory properties of an Optional Property

Pretty much what the title says. I have an optional object in the request body. However, if that object is given, it should mandatorily contain a few child properties.

My OpenAPI component config looks like this:

UserDetails:
  type: object
  properties:
    surname:
      type: string
    givenName:
      type: string
    dob:
      type: string
      format: date
    gender:
      type: string
      enum:
        - male
        - female
        - others
    partner:
      type: object
      properties:
        name:
          type: string
        phone:
          type: string
          maxLength: 10
          minLength: 10
          pattern: ^[1-9]+[0-9]{9}$
      required: [name, phone]
  required:
    - surname
    - givenName
    - dob

I am using express-openapi-validator to validate this. Now, I don't understand if this is the problem of the express-openapi-validator package or not, but the required fields (name, phone) of the optional field (partner) is never validated. I can just provide partner: {} and it slips in right through, or even, partner: { name: 'some name', phone: '123' }. Phone number should be validated for the length and the regex.

Am I missing anything in the definition?

Upvotes: 1

Views: 2278

Answers (1)

amritashan
amritashan

Reputation: 583

There does not seem to be a solution to this, but I am closing this just for peace to my mind.

like what @Helen has replied, this seems to be an issue with the library itself. In the process of developing my application, I discovered more problems, which make the the library express-openapi-validator and another library swagger-express-middleware I used, even lesser reliable for validating requests.

The other problem that I discovered was that a value is validated with a provided enum only if it is provided as an array:

eg. The following is validated correctly:

UserLiabilities:
  type: object
  properties:
    liabilities:
      type: object
      properties:
        creditCards:
          type: array
          items:
            allOf:
              - $ref: '#/components/schemas/Provider'
              - type: object
                properties:
                  limit:
                    type: number
                    minimum: 0

Here Provider is:

Provider:
  type: object
  properties:
    provider:
      type: string
      enum: ['ABC', 'DEF', 'GHI', 'Others']

However, if I need provider as a plain string in an object (not in an array like above), it is not validated. And, it allows any arbitrary string:

homeLoan:
  allOf:
    - $ref: '#/components/schemas/Provider'
    - type: object
      properties:
        amount:
          type: number
          minimum: 0

This behavior is consistent with atleast two libraries that I have tried and mentioned above. I am not sure I am doing something wrong here, but I have lost enough time trying to debug this and had to finally resolve this in my server itself.

Upvotes: 1

Related Questions