dimanche 1 décembre 2019

How to integrate both a null datatype and a number datatype into my schema?

I have written a schema and found that the EventId response comes back as either a number or null but upon first glance I thought the responses were just numbers yet i was wrong theirs both as in the error below: AssertionError: Invalid type: null (expected number): expected false to equal true

How do I accommodate these two data types in my schema?

Schema below:

tests['Status code is 200'] = pm.response.to.have.status(200);
const schema =
{
  "definitions": {},
  "$schema": "http://json-schema.org/draft-06/schema#",
  "$id": "http://example.com/root.json",
  "type": "array",
  "items": {
    "$id": "#/items",
    "type": "object",
    "required": [
      "RequestId",
      "FleetGroupId",
      "RecorderId",
      "Status",
      "DateRaised",
      "DateRaisedString",
      "DateRaisedISO",
      "RequestedTime",
      "RequestedTimeString",
      "RequestedTimeISO",
      "EventId",
      "AlertLevel",
      "Media",
      "MediaList"
    ],
    "properties": {
      "RequestId": {
        "$id": "#/items/properties/RequestId",
        "type": "number"
      },
      "FleetGroupId": {
        "$id": "#/items/properties/FleetGroupId",
        "type": "number"
      },
      "RecorderId": {
        "$id": "#/items/properties/RecorderId",
        "type": "string",
        "pattern": "^(.*)$"
      },
      "Status": {
        "$id": "#/items/properties/Status",
        "type": "string",
        "pattern": "^(.*)$"
      },
      "DateRaised": {
        "$id": "#/items/properties/DateRaised",
        "type": "string",
        "pattern": "^(.*)$"
      },
      "DateRaisedString": {
        "$id": "#/items/properties/DateRaisedString",
        "type": "string",
        "pattern": "^(.*)$"
      },
      "DateRaisedISO": {
        "$id": "#/items/properties/DateRaisedISO",
        "type": "string",
        "pattern": "^(.*)$"
      },
      "RequestedTime": {
        "$id": "#/items/properties/RequestedTime",
        "type": "string",
        "pattern": "^(.*)$"
      },
      "RequestedTimeString": {
        "$id": "#/items/properties/RequestedTimeString",
        "type": "string",
        "pattern": "^(.*)$"
      },
      "RequestedTimeISO": {
        "$id": "#/items/properties/RequestedTimeISO",
        "type": "string",
        "pattern": "^(.*)$"
      },
      "EventId": {
        "$id": "#/items/properties/EventId",
        "type": "number"
      },
      "AlertLevel": {
        "$id": "#/items/properties/AlertLevel",
        "type": "string",
        "pattern": "^(.*)$"
      },
      "Media": {
        "$id": "#/items/properties/Media",
        "type": "object",
        "required": [
          "Time",
          "TimeString",
          "TimeISOString",
          "ContentType",
          "MediaUrl",
          "JsonUrl",
          "ChannelId"
        ],
        "properties": {
          "Time": {
            "$id": "#/items/properties/Media/properties/Time",
            "type": "string",
            "pattern": "^(.*)$"
          },
          "TimeString": {
            "$id": "#/items/properties/Media/properties/TimeString",
            "type": "string",
            "pattern": "^(.*)$"
          },
          "TimeISOString": {
            "$id": "#/items/properties/Media/properties/TimeISOString",
            "type": "string",
            "pattern": "^(.*)$"
          },
          "ContentType": {
            "$id": "#/items/properties/Media/properties/ContentType",
            "type": "string",
            "pattern": "^(.*)$"
          },
          "MediaUrl": {
            "$id": "#/items/properties/Media/properties/MediaUrl",
            "type": "string",
            "pattern": "^(.*)$"
          },
          "JsonUrl": {
            "$id": "#/items/properties/Media/properties/JsonUrl",
            "type": "string",
            "pattern": "^(.*)$"
          },
          "ChannelId": {
            "$id": "#/items/properties/Media/properties/ChannelId",
            "type": "number"
          }
        }
      },
      "MediaList": {
        "$id": "#/items/properties/MediaList",
        "type": "array",
        "items": {
          "$id": "#/items/properties/MediaList/items",
          "type": "object",
          "required": [
            "Time",
            "TimeString",
            "TimeISOString",
            "ContentType",
            "MediaUrl",
            "JsonUrl",
            "ChannelId"
          ],
          "properties": {
            "Time": {
              "$id": "#/items/properties/MediaList/items/properties/Time",
              "type": "string",
              "pattern": "^(.*)$"
            },
            "TimeString": {
              "$id": "#/items/properties/MediaList/items/properties/TimeString",
              "type": "string",
              "pattern": "^(.*)$"
            },
            "TimeISOString": {
              "$id": "#/items/properties/MediaList/items/properties/TimeISOString",
              "type": "string",
              "pattern": "^(.*)$"
            },
            "ContentType": {
              "$id": "#/items/properties/MediaList/items/properties/ContentType",
              "type": "string",
              "pattern": "^(.*)$"
            },
            "MediaUrl": {
              "$id": "#/items/properties/MediaList/items/properties/MediaUrl",
              "type": "string",
              "pattern": "^(.*)$"
            },
            "JsonUrl": {
              "$id": "#/items/properties/MediaList/items/properties/JsonUrl",
              "type": "string",
              "pattern": "^(.*)$"
            },
            "ChannelId": {
              "$id": "#/items/properties/MediaList/items/properties/ChannelId",
              "type": "number"
            }
          }
        }
      }
    }
  }
};

 pm.test('Schema is valid', function() {
 var response = pm.response.json();
   var result = tv4.validateResult(response, schema, false, true);
 pm.expect(result.valid).to.equal(true, result.valid ? null : result.error.message);
});

I have come across a similar question in stackoverflow but I don't know how to incorporate the code into my schema, can you please help? This is exactly what I need..well some of it:

var jsonData = JSON.parse(responseBody);

pm.test('All EventIdcontains number', () => {

    for (i = 0; i < jsonData.length; i++) {            
        pm.expect(typeof jsonData[i].EventId).to.be.oneOf(['number', 'object']);
    }
});

Aucun commentaire:

Enregistrer un commentaire