Validation 是否有人可以帮助我在Postman中创建一个测试,以验证密钥在嵌套组中是否具有预期值?

Validation 是否有人可以帮助我在Postman中创建一个测试,以验证密钥在嵌套组中是否具有预期值?,validation,testing,nested,postman,response,Validation,Testing,Nested,Postman,Response,有没有人能告诉我在Postman中编写测试的正确方向,以便验证以下响应中的某个键或值是否存在。响应有许多嵌套对象。 具体而言,我想验证以下内容: 我想验证output.treatments中有一个值为“Diet”的键“type” 我还想验证output.treatments.details中有一个键“name”,其值为“磺胺甲恶唑/甲氧苄啶” 谢谢 API响应如下所示: { "additionalInformation": { "knownDrugAll

有没有人能告诉我在Postman中编写测试的正确方向,以便验证以下响应中的某个键或值是否存在。响应有许多嵌套对象。 具体而言,我想验证以下内容:

  • 我想验证output.treatments中有一个值为“Diet”的键“type”
  • 我还想验证output.treatments.details中有一个键“name”,其值为“磺胺甲恶唑/甲氧苄啶”
  • 谢谢

    API响应如下所示:

    {
      "additionalInformation": {
        "knownDrugAllergies": [],
        "knownDrugConflicts": [
          {
            "conflict": "OXYBUTYNIN CHLORIDE",
            "potentialDrugs": [
              "item1",
              "item2"
            ]
          }
        ],
        "knownPrecautionConflicts": [
          {
            "conflict": "OXYBUTYNIN CHLORIDE",
            "potentialDrugs": [
              "Drowsy",
              "Gastroesophageal Reflux",
              "Myasthenia Gravis",
              "Parkinsonism",
              "Ulcerative Colitis"
            ]
          }
        ]
      },
      "output": [
        {
          "icdCode": "N39.3",
          "icdDesc": "Stress incontinence (female) (male)",
          "isIllness": true,
          "treatments": [
            {
              "details": [],
              "type": "Physical Exam"
            },
            {
              "details": [
                {
                  "directions": [],
                  "dosage": "",
                  "groupName": "Group Placeholder",
                  "longName": "Normal Diet",
                  "name": "Normal Diet",
                  "nameDetails": "The patient has no dietary restrictions",
                  "priority": 16,
                  "rank": 1,
                  "reasons": [],
                  "sources": [
                    [
                      "TEST",
                      "https://google.com"
                    ],
                    [
                      "TEST",
                      "https://google.com"
                    ]
                  ]
                }
              ],
              "type": "Diet"
            },
            {
              "details": [
                {
                  "directions": [],
                  "dosage": "",
                  "groupName": "Group Placeholder",
                  "longName": "No Restrictions",
                  "name": "No Restrictions",
                  "nameDetails": "The patient has no activity restrictions",
                  "priority": 16,
                  "rank": 1,
                  "reasons": [],
                  "sources": [
                    [
                      "TEST",
                      "https://google.com"
                    ],
                    [
                      "TEST",
                      "https://google.com"
                    ]
                  ]
                }
              ],
              "type": "Activity"
            },
            {
              "details": [
                {
                  "directions": [],
                  "dosage": "",
                  "groupName": "Group Placeholder",
                  "longName": "None Needed ",
                  "name": "None Needed",
                  "nameDetails": "No physical therapy is needed.",
                  "priority": 20,
                  "rank": 1,
                  "reasons": [],
                  "sources": [
                    [
                      "TEST",
                      "https://google.com"
                    ],
                    [
                      "TEST",
                      "https://google.com"
                    ]
                  ]
                }
              ],
              "type": "Physical Therapy"
            },
            {
              "details": [
                {
                  "directions": [],
                  "dosage": "",
                  "groupName": "Group Placeholder",
                  "longName": "Kegel Exercises",
                  "name": "Kegel Exercises",
                  "nameDetails": "INCOMPLETE",
                  "priority": 20,
                  "rank": 1,
                  "reasons": [],
                  "sources": [
                    [
                      "TEST",
                      "https://google.com"
                    ],
                    [
                      "TEST",
                      "https://google.com"
                    ]
                  ]
                }
              ],
              "type": "Counseling"
            },
            {
              "details": [
                {
                  "directions": [],
                  "dosage": "",
                  "groupName": "Group Placeholder",
                  "longName": "Patient Can Resume Work/School Immediately",
                  "name": "Immediately",
                  "nameDetails": "Patient Can Resume Work/School Immediately",
                  "priority": 8,
                  "rank": 1,
                  "reasons": [],
                  "sources": [
                    [
                      "TEST",
                      "https://google.com"
                    ],
                    [
                      "TEST",
                      "https://google.com"
                    ]
                  ]
                }
              ],
              "type": "Return to Work/School Status"
            },
            {
              "details": [
                {
                  "directions": [],
                  "dosage": "",
                  "groupName": "Group Placeholder",
                  "longName": "Wound Care Not Needed",
                  "name": "Not Needed",
                  "nameDetails": "Wound Care Not Needed",
                  "priority": 20,
                  "rank": 1,
                  "reasons": [],
                  "sources": [
                    [
                      "TEST",
                      "https://google.com"
                    ],
                    [
                      "TEST",
                      "https://google.com"
                    ]
                  ]
                }
              ],
              "type": "Wound Care"
            },
            {
              "details": [
                {
                  "directions": [],
                  "dosage": "",
                  "groupName": "Group Placeholder",
                  "longName": "Urine Analysis",
                  "name": "Urine Analysis",
                  "nameDetails": "Evaluate the urine for electrolytes and pH.",
                  "priority": 20,
                  "rank": 1,
                  "reasons": [],
                  "sources": [
                    [
                      "TEST",
                      "https://google.com"
                    ]
                  ]
                }
              ],
              "type": "Labs"
            },
            {
              "details": [
                {
                  "directions": [],
                  "dosage": "",
                  "groupName": "Group Placeholder",
                  "longName": "Specialist Consultation",
                  "name": "Specialist Consult",
                  "nameDetails": "The patient should consult with a specialist in the appropriate field.",
                  "priority": 29,
                  "rank": 1,
                  "reasons": [],
                  "sources": [
                    [
                      "TEST",
                      "https://google.com"
                    ]
                  ]
                }
              ],
              "type": "Discharge Disposition"
            },
            {
              "details": [
                {
                  "directions": [],
                  "dosage": "",
                  "groupName": "Group 2",
                  "longName": "placeholder",
                  "name": "DULOXETINE HCL",
                  "nameDetails": "Name Details Placeholder",
                  "priority": 20,
                  "protocol": [
                    "example"
                  ],
                  "rank": 1,
                  "reasons": [],
                  "sources": [
                    [
                      "TEST",
                      "https://google.com"
                    ]
                  ]
                },
                {
                  "directions": [],
                  "dosage": "",
                  "groupName": "Standard",
                  "longName": "placeholder",
                  "name": "SULFAMETHOXAZOLE/TRIMETHOPRIM",
                  "nameDetails": "Name Details Placeholder",
                  "priority": 20,
                  "protocol": [
                    "example"
                  ],
                  "rank": 3,
                  "reasons": [],
                  "sources": [
                    [
                      "TEST",
                      "https://google.com"
                    ]
                  ]
                },
                {
                  "directions": [],
                  "dosage": "",
                  "groupName": "Standard",
                  "longName": "placeholder",
                  "name": "TOLTERODINE TARTRATE",
                  "nameDetails": "Name Details Placeholder",
                  "priority": 20,
                  "protocol": [
                    "example"
                  ],
                  "rank": 1,
                  "reasons": [],
                  "sources": [
                    [
                      "no sources",
                      "no URL"
                    ]
                  ]
                }
              ],
              "type": "Prescription Drugs"
            },
            {
              "details": [
                {
                  "directions": [],
                  "dosage": "",
                  "groupName": "Standard",
                  "longName": "placeholder",
                  "name": "OXYBUTYNIN",
                  "nameDetails": "Name Details Placeholder",
                  "priority": 20,
                  "protocol": [
                    "example"
                  ],
                  "rank": 2,
                  "reasons": [],
                  "sources": [
                    [
                      "TEST",
                      "https://google.com"
                    ]
                  ]
                }
              ],
              "type": "OTC Drugs"
            }
          ]
        }
      ],
      "status": "Complete",
      "version": 3
    } 
    
    找到解决方案:

    var jsonData = pm.response.json();
    
    pm.test("OXYBUTYNIN present", function () {
        pm.expect(_isContains(jsonData.output, "name" ,"OXYBUTYNIN")).to.be.true;
    });
    
    pm.test("TOLTERODINE TARTRATE present", function () {
        pm.expect(_isContains(jsonData.output, "name" ,"TOLTERODINE TARTRATE")).to.be.true;
    });
    
    pm.test("SULFAMETHOXAZOLE/TRIMETHOPRIM present", function () {
        pm.expect(_isContains(jsonData.output, "name" ,"SULFAMETHOXAZOLE/TRIMETHOPRIM")).to.be.true;
    });
    
    pm.test("DULOXETINE HCL present", function () {
        pm.expect(_isContains(jsonData.output, "name" ,"DULOXETINE HCL")).to.be.true;
    });
    
    function _isContains(json, keyname, value) {
    return Object.keys(json).some(key => {
            return typeof json[key] === 'object' ? 
            _isContains(json[key], keyname, value) : key === keyname && json[key] === value;
        });
    }