Oleg Sh
Oleg Sh

Reputation: 9013

Postman test - object's schema

I have a response body:

{
    "Id": 15,
    "Name": "Carrier1",
    "Fein": "Fein1",
    "McNumber": "McNumber1",
    "DotNumber": "DotNumber1",
    "Address": {
        "Street": "Street1",
        "City": "City1",
        "ZipPostalCode": null,
        "StateName": "AA (Armed Forces Americas)",
        "StateAbbr": "AA",
        "ContactName": null,
        "ContactPhone": null,
        "ContactFaxNumber": null,
        "ContactEmail": null
    }
}

I use Postman and want to describe schema for validation in tests:

const schema = {
  "required": ["Id"],
  "properties": {
    "Id": {
      "type": "integer",
    },
    "Name": {
      "type": "string",
    },
    "Fein": {
      "type": "string",
    },
    "McNumber": {
      "type": "string",
    },
    "DotNumber": {
      "type": "string",
    },
    "Address": {
        "type" : {
            "properties": {
                "Street": {
                "type": "string",
                },            
            },
        }
    }
  }
};

var carrier = JSON.parse(responseBody);
tests["Carrier is valid"] = tv4.validate(carrier, schema);

but it does not work. Validation that it just should be object:

"Address": {
    "type" : "object"
    }

works fine. How to describe it details?

Upvotes: 1

Views: 774

Answers (1)

Danny Dainton
Danny Dainton

Reputation: 25851

Would this work:

const schema = {
  "required": ["Id"],
  "properties": {
      "Id": {
          "type": "integer"
      },
      "Name": {
          "type": "string"
      },
      "Fein": {
          "type": "string"
      },
      "McNumber": {
          "type": "string"
      },
      "DotNumber": {
          "type": "string"
      },
      "Address": {
          "type" : "object",
          "properties": {
              "Street": {
                  "type": "string"
              }
          }
        }
    }
}

Added this test to check:

pm.test('Schema Valid', () => {
    var carrier = pm.response.json()
    pm.expect(tv4.validate(carrier, schema)).to.be.true
})

Test Passing

I'm using the native Postman application so if you're still using the Chrome extension, this will fail due to it not knowing about the pm.* API functions

Upvotes: 1

Related Questions