I am testing an API which allows users to access current weather data for any location on Earth.
The parameters involved are:
q (city name)
appid (unique API key)
units (units of measurement)
lang (language)
All of these parameters have a String data format. How can I validate the data format of these params?
Below is what I have constructed in Postman. I have added the params with values.
This is the response:
{
"coord": {
"lon": 151.2073,
"lat": -33.8679
},
"weather": [
{
"id": 801,
"main": "Clouds",
"description": "few clouds",
"icon": "02n"
}
],
"base": "stations",
"main": {
"temp": 21.68,
"feels_like": 21.84,
"temp_min": 19.62,
"temp_max": 23.17,
"pressure": 1014,
"humidity": 74
},
"visibility": 10000,
"wind": {
"speed": 3.6,
"deg": 50
},
"clouds": {
"all": 20
},
"dt": 1675779647,
"sys": {
"type": 2,
"id": 2002865,
"country": "AU",
"sunrise": 1675797783,
"sunset": 1675846539
},
"timezone": 39600,
"id": 2147714,
"name": "Sydney",
"cod": 200
}
We can see that only the city (q param) is in the body (name). I donât see the other params as they seem to be background/general settings.
I have used this Test to validate the string format of name (q) property which passes:
pm.test(âData format is stringâ, () => {
const responseJson = pm.response.json();
pm.expect(responseJson.name).to.be.a(âstringâ);
});
I have also tried the below Test which fails (I get this message: Data format is string | AssertionError: expected [ { key: âqâ, value: âSydneyâ }, âŚ(3) ] to be a string):
pm.test(âData format is stringâ, () => {
console.log(pm.request.toJSON().url.query)
pm.expect(pm.request.toJSON().url.query).to.be.a(âstringâ);
});
Please advise on how to validate the data format of the params.