简体   繁体   English

如何使用 Mule 4.4 运行时验证 OpenAPI 3.0 中的 JSON 有效负载

[英]how do I validate a JSON payload in OpenAPI 3.0 with Mule 4.4 Runtime

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:我需要使用 openapi: 3.0.0 开发一个 Mule API ( 4.4 Runtime ) 端点是一个具有以下请求负载的 POST:

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

Here is the relevant section of the OpenAPI 3.0 spec:这是 OpenAPI 3.0 规范的相关部分:

    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?因此,我可以验证“城市”和“国家/地区”等单个元素不是 null,但如何防止以下请求? ( presently its not being flagged as invalid: ) (目前它没有被标记为无效:)

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

You can define the Employee wrapper as a required property and also disallow unknown properties by adding additionalProperties: false .您可以将Employee包装器定义为必需属性,也可以通过添加additionalProperties: false来禁止未知属性。 Note that required is not a property-level attribute , but an object-level attribute - it's a list of required properties.请注意, required不是属性级属性,而是对象级属性 - 它是必需属性的列表。

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  # <-----

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM