人体特性和多个示例

时间:2019-11-01 18:42:08

标签: api raml

我是RAML的新手,最近继承了我需要更新以反映新规范的项目。

这是一个相当简单的RAML文档,我所做的只是添加一个新的body属性,我想展示多个示例。

以前,该帖子需要一个“ codeType1”属性。

现在,有了一个新的“ codeType2”属性,并且这两个属性之一是必需的。因此,codeType1或codeType2必须在帖子正文中。我不知道如何在RAML中表达这一要求。

对于每种情况,我还要有两个示例。

我能够添加新的codeType,但是我不知道如何表达验证规则。

这是我目前拥有的:

#%RAML 1.0
title: Auth Service
version: v1
protocols: [HTTPS]
mediaType: application/json

/auth:
    post:
      description: Authenticate a client to the API.
      body:
        application/json:
          properties:
            {
            "codeType1": {
                type: string,
                required: true
              },
            "codeType2":{
                type: string,
                required: true
              },
            "userId":{
                type: string,
                required: true
              },
            "password":{
                type: string,
                required: true
              },
            }
          example:
            {
              "codeType1":"994056",
              "codeType2":"##0023",
              "userId":"name@email.com",
              "password":"Abc123!",
            }
      responses:
        200:
          description: Successful authentication.
          body:
            application/json:
              example:
                {
                  "status": "success",
                  "message": "Authentication success",
                  "data": {
                    "token": "SDKFHDSLFDJSFDKJFDHSFLJKFHLSKFSFLKFLSDFJHSLHFSDF"
                  }
                }

1 个答案:

答案 0 :(得分:0)

尝试一下:

#%RAML 1.0
title: Auth Service
version: v1
protocols: [HTTPS]
mediaType: application/json

types:
  UserCred:
    properties:
      userId:
      password:
  Type1:
    type: UserCred
    properties:
      codeType1:
  Type2:
    type: UserCred
    properties:
      codeType2:

/auth:
    post:
      description: Authenticate a client to the API.
      body:
        application/json:
          type: Type1 | Type2
          examples:
            ex1: 
              codeType1: "994056"
              userId: "name@email.com"
              password : "Abc123!"
            ex2:
              codeType2: "12345"
              userId: "anothername@email.com"
              password: "ZYX098"

我只复制了更改的部分。

以下是我在上面使用的RAML 1.0规范的一些参考: