Help need with validating response that it is a string and contains a value

I have a response for a PUT request as below which is an array and can have 2 elements in the array
Response

“customers”: [
{
“verificationNumber”: “z2jYF52tPC7VPtk”,
“verificationDate”: “29/05/2021”,
“customerNumber”: “Customer_12345”,
“verificationStatus”: “XXXXXX”
}
{
“verificationNumber”: “KKKKKKKKK”,
“verificationDate”: “29/05/2021”,
“customerNumber”: “Customer_12345678”,
“verificationStatus”: “XXXXXXYYY”
}
]

How can we validate and test in Tests, that in the response array
First Customer Number as sent in the request.
Second verificationNumber and verification status is String and has some value
Third verificationDate with type date-only is today’s date in DD/MM/YYYY format

I have tried below

var x= pm.response.json();

pm.test('Test Verification number is a string ’ ,function(){

pm.expect(x.customers[0].verificationNumber).to.be.a(‘string’)

});

Also how can we test the value is oneOf the string
Like for example in above if Verification status can take 5 values (Pass, Fail, No result, Error, On hold), how can we ensure value is one of them.

Thanks

@KKKK1947 Welcome the community :partying_face:

Kindly refer the similar post here.

You need to use to.be.oneOf([“Pass”, “Fail”, “No result”, “Error”, “On hold”])

1 Like

Hello @bpricilla
Thanks for the reply.
I have tried pm.expect(jsonData.deleates[i].refusal.code).to.be.oneOf([“1306”, “502”]);, but it doesn’t work for string value, works for number, unless I am doing something wrong :frowning:

Thanks

1 Like

@KKKK1947 ,
It should work for both numbers and strings.

Numbers don’t need the quotes, but it matters for string.

For example:

pm.test("Successful POST request", function () {
    pm.expect(pm.response.code).to.be.oneOf([201, 202]);
})

Screen shot of using it and error

@KKKK1947 ahhh, it’s just a typo here :stuck_out_tongue:

Check the bottom of your screenshot. System is suggesting you the correct method :blush:

It should “oneOf” and not “Oneof”.

1 Like

Got is @bpricilla .Thank you so much,
Silly me. works now :slight_smile: Thank you so much.
Can you please also help with the validating response is string and has some value.
I mean I was able to get it is a string from below
pm.expect(x.customers[0].verificationNumber).to.be.a(‘string’) , how can validate it contains a value as well?
Thanks

@KKKK1947 Great to know :sunglasses:

Since I don’t have the full response of yours I suggest you to read through this post which I have mentioned above. You can tweak this based on your needs.

1 Like

Thanks @bpricilla for your kind help.
I have gone through post. I have tried and gone through the post, I am unable to figure out at this stage
I need some help on below scenarios if you can help please. Below is my response


So it array with 2 elements .

I need to first check if iDNumber in response matches to Id number sent in request.
Once it matches, then check
Id type matches what is sent in request
text is either “Check passed” or “Check failed”
status is “Pass” or “Fail”

Thanks.

Hi @KKKK1947,

For this, you may need to loop through the array and extract the values, then have an if-else check.

Once all your conditions are met, have a variable and assign it a boolean value. This variable can be used in pm.expect to check if it comes to true.

Hope this helps :slight_smile:

1 Like