有人可以帮我在 Postman 中创建测试以验证键在嵌套组中是否具有预期值吗?

Can someone help me create a test in Postman in order to validate that a key has the expected value inside a nested group?

有人可以指出我在 Postman 中编写测试的正确方向,以验证是否存在以下响应中的某个键或值。响应有很多嵌套对象。 具体来说,我想验证以下内容:

  1. 我想验证 output.treatments 中是否存在值为“Diet”的键“type”
  2. 而且我还想验证 output.treatments.details 中有一个键“name”,其值为“SULFAMETHOXAZOLE/TRIMETHOPRIM”

谢谢!

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;
    });
}