Schema validation when response is inconsistent

Hi There,

I am validating response through schema. We have situation whereas in response, few objects and there properties are inconsistent.

Below is the API example:

“fiblTeams”: {},
“gqv”: {
“current”: “1392.49”,
“target”: “1200.00”
},

OR

“fiblTeams”: {
“current”: “1392.49”,
“target”: “1200.00”
},
“gqv”: {},

Hope making sense through above example. In first response, “gqv” object have some properties whereas in second response, “gqv” object is blank.

Schema Validation:
“gqv”: {
“type”: “object”,
“properties”: {
“current”: { type: “string” },
“target”: { type: “string” },
},
“required”: [“current”,“target”]
},
But in next response test will fail since second gqv object is blank.

Looking forward!! Let me know if needed any additional details.

By what you’re explaining here, current and target are not required fields. You could just remove the required array and it would work.

But if you’re saying current is required if target is present and vice versa, you can use oneOf.

{
  "gqv": {
    "oneOf": [
      {
        "type": "object",
        "properties": {
          "current": {
            "type": "string"
          } ,
          "target": {
            "type": "string"
          } 
        },
        "required": [
          "current",
          "target"
        ]
      },
      {
        "type": "object"
      }
    ]
  }
}

No, Current and target are required fields so we need to use required array.

Basically based on the backend data, some time API give response with gqv as
“gqv”: {
“current”: “1392.49”,
“target”: “1200.00”
},
and some time response like empty
“gqv”: {},

So I want to make my schema reliable in both the conditions.
Above schema will fail when there is empty response for “gqv”, correct?

And I was deep diving more into anyOf/allOf/oneOf keywords through https://cswr.github.io/JsonSchema/spec/generic_keywords/

However find difficulty to understand with example.

Example of response API:
[
{
“id”: 120000755487,
“firstName”: “Bruce”,
“lastName”: “White”,
“generation”: 0,
“lastOrderedOn”: “2020-09-03 03:08:35”
},
{
“id”: 120001439895,
“firstName”: “Cherlyn”,
“lastName”: “Jeffries”,
“generation”: 0,
“lastOrderedOn”: null
}
]
If you see in array, lastOrderedOn object values are different. In this case which generic keyword(anyof/allof/oneof) will work with schema?
I tried with below schema but no luck.

const schema = {
“type”: “array”,
“items”: {
“anyOf”: [
{
“type”: “object”,
“required”: [“id”,“firstName”,“lastName”, “avatarUrl”,“generation”, “lastOrderedOn”],
“additionalProperties”: false,
“properties”: {
“id”: { “type”: “integer”},
“firstName”: {“type”: “string”},
“lastName”: {“type”: “string”},
“avatarUrl”: {“type”: “string”},
“generation”: {“type”: “integer”},
“lastOrderedOn”: {“type”: “string”},

                           }

                           }]}}

pm.test(“Body is correct”, function () {

    pm.response.to.have.jsonSchema(schema);
let jsonData = pm.response.json();
 pm.response.to.have.status(200);

pm.environment.set(“clientid”, jsonData[0].id);
});
Instead of anyOf, I tried oneOf/allOf but not working.

Solution was I just changed “lastOrderedOn”: {“type”: [ “string”, “null”] }, within schema that’s it within any keyword.

So want to know exact difference on such keyword?

Sorry now ticket contains 2 queries but want to learn basics.

What I showed you is if you have multiple allowable data structures for a particular schema.

In your use case where you have a single property that is nullable, that is not a change in the data structure because that property always exists.

If you want to know the difference between these multiple operators, see below:

  • oneOf - The object must match one of the defined schema options.
  • anyOf - The object may contain zero or more of the defined schema options. I usually use this one when composing a larger object.
  • allOf - The object must contain all of the defined schema options.

ome time API give response with gqv as
“gqv”: {
“current”: “1392.49”,
“target”: “1200.00”
},
and some time response like empty
“gqv”: {},

@allen.helton Repeating but again what gonna be best schema in such case when output is not consistent.
Based on the live database changes, response might be change and that’s a business changes but yes if response coming out then object and there datatypes should be proper.

A field cannot be both required and allowed missing at the same time. There’s no way to validate that schema since it’s always going to be true. Best thing I can say is to mark it as optional and do data type validation on it.

1 Like

Thanks, Will do the same.

in addition try to validate there are no additional properties