0

Greatings,

I was wondering how to validate an array with postman

I have this answer:

{
"documents": [
            {
                "documentType": "FORM",
                "attributes": [
                    {
                        "kind": {
                            "code": "COMPANY",
                            "name": "Company name"
                        },
                        "value": "KKO",
                        "confidence": 0.0
                    },
                    {
                        "kind": {
                            "code": "AGREEMENT",
                            "name": "Agreement number"
                        },
                        "value": "123",
                        "confidence": 0.0
                    },
                    {
                        "kind": {
                            "code": "DATE_OF_ISSUE",
                            "name": "When it was created"
                        },
                        "value": "01/01/2019",
                        "confidence": 0.0
                    }
}

I want to validate the second attribute which is:

{
                        "kind": {
                            "code": "AGREEMENT",
                            "name": "Agreement number"
                        },

Including value field

                        "value": "123",
                        "confidence": 0.0
                    },

I was wondering what will be the easiest way to validate this?

I've tried to write paths like:

const response = pm.response.json()

attribute1 = response.data.documents[0];
attribute2 = response.data.documents[1];
attribute3 = response.data.documents[3];

pm.expect(attributes[1].kind.code).to.equal("AGREEMENT");

pm.expect(attributes[1].kind.name).to.equal("Agreement number");

but it is all too heavy and time consuming.

I would much appreciate if you share with me your experience and provide the clue on how to resolve my issue.

Thank you in advance

1 Answer 1

1
pm.expect([1,2,3,4,5]).to.deep.equal([1,2,3,4,5])

or

pm.expect(_.isEqual([1,2,3,4,5],[1,2,3,4,5])).to.be.true

so in your case:

  pm.expect([
    {
        "kind": {
            "code": "COMPANY",
            "name": "Company name"
        },
        "value": "KKO",
        "confidence": 0.0
    },
    {
        "kind": {
            "code": "AGREEMENT",
            "name": "Agreement number"
        },
        "value": "123",
        "confidence": 0.0
    },
    {
        "kind": {
            "code": "DATE_OF_ISSUE",
            "name": "When it was created"
        },
        "value": "01/01/2019",
        "confidence": 0.0
    }]).to.deep.equal([
                    {
                        "kind": {
                            "code": "COMPANY",
                            "name": "Company name"
                        },
                        "value": "KKO",
                        "confidence": 0.0
                    },
                    {
                        "kind": {
                            "code": "AGREEMENT",
                            "name": "Agreement number"
                        },
                        "value": "123",
                        "confidence": 0.0
                    },
                    {
                        "kind": {
                            "code": "DATE_OF_ISSUE",
                            "name": "When it was created"
                        },
                        "value": "01/01/2019",
                        "confidence": 0.0
                    }])

Update

name = ["Company name", "Agreement number", "When it was created"]
value = ["KKO", "123", "01/01/2019"]

pm.test("something", function () {
    a.forEach((a, i) => {
        pm.expect(a.kind.name).to.be.equal(name[i])

        pm.expect(a.value).to.be.equal(value[i])
    })
})
Sign up to request clarification or add additional context in comments.

10 Comments

Hello PDHide, I'm afraid I don't quite get the resolve
I only wanted to validate the second attribute's code, name and value.
added the answer use same logic , assuming a to be the attribute array
Can you tell me please... what if I have a duplicate in 'name' field ?
Array can have duplicate values
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.