Home > OS >  how do I validate a JSON payload in OpenAPI 3.0 with Mule 4.4 Runtime
how do I validate a JSON payload in OpenAPI 3.0 with Mule 4.4 Runtime

Time:04-17

I need to develop a Mule API ( 4.4 Runtime ) with openapi: 3.0.0 The endpoint is a POST with the following request payload :

{
  "Employee": {
     "Address": {
        "City": "a",
        "Country": "aaa"
      }
  }
}

Here is the relevant section of the OpenAPI 3.0 spec :

    openapi: "3.0.0"
paths:
  /search:
    post:
      tags:
      - SearchUser
      summary: Search for Users
      operationId: getUser
      requestBody:
        description: User Request Object
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/RequestComp'
        required: true
      responses:
        '200':
          description: successful operation
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/ResponseComp'
        '400':
          description: Bad request
          content: {}
        '404':
          description: User not found
          content: {}
        '405':
          description: Validation exception
          content: {}
      
components:
  schemas:
    RequestComp:
      type: object
      properties:
        Employee:
          $ref: '#/components/schemas/EmployeeComp'
    EmployeeComp:
      type: object
      properties:
        Address:
          $ref: '#/components/schemas/AddressComp'
    AddressComp:
      type: object
      properties:
        City:
          type: string
          required: true
          nullable: false
          minLength: 1
        Country:
          type: string
          required: true
          nullable: false
          minLength: 1
    ResponseComp:
      type: object
      properties:
        City:
          type: string
        Country:
          type: string

So I can validate individual elements like 'City' and 'Country' to not be null but how do I prevent following request ? ( presently its not being flagged as invalid : )

    {
        "Address": {
           "City": "a",
           "Country": "aaa"
        }
    }

CodePudding user response:

You can define the Employee wrapper as a required property and also disallow unknown properties by adding additionalProperties: false. Note that required is not a property-level attribute, but an object-level attribute - it's a list of required properties.

components:
  schemas:
    RequestComp:
      type: object
      required: [Employee]         # <-----
      properties:
        Employee:
          $ref: '#/components/schemas/EmployeeComp'
      additionalProperties: false  # <-----

    EmployeeComp:
      type: object
      properties:
        Address:
          $ref: '#/components/schemas/AddressComp'
      additionalProperties: false  # <-----

    AddressComp:
      type: object
      required: [City, Country]    # <-----
      properties:
        City:
          type: string
          # required: true    # <-- remove this
          nullable: false
          minLength: 1
        Country:
          type: string
          # required: true    # <-- remove this
          nullable: false
          minLength: 1
      additionalProperties: false  # <-----
  • Related