Postman test - object's schema - json

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?

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
})
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

Related

Mapping request body fields in logic apps

We have configured common alert schema for alerts and we are using a ticketing software and whenever we receive alert it should create a ticket.
In the logic app I have to make Post API call for creation of ticket with following json object, some fields are hard coded values:
{
"subject": "",
"Id": "123456789", // Hard code value
"priority": "",
"email": "test#test.com", // Hard code value
"status": "Open" // Hard code value
}
Parse Json sample payload schema for Alert:
{
"data": {
"alertContext": {
},
},
"customProperties": null,
"essentials": {
"alertContextVersion": "123",
"alertId": "123",
"alertRule": "Test Alerts",
"description": "test",
"severity": "Sev4"
}
},
"schemaId": "test"
}
I have to map "subject and "priority" fields with alert json object "description" and "severity":
subject-->description
priority --> severity // sev0 =high ,sev1=medium, sev2 =low
How can I achieve this using logic app?
After Parse JSON You can directly map its objects in the compose connector with the required fields. Below is my logic app flow.
You can use the below Code view to reproduce the same in your Logic app
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Compose": {
"inputs": {
"customProperties": null,
"data": {
"alertContext": {}
},
"essentials": {
"alertContextVersion": "123",
"alertId": "123",
"alertRule": "Test Alerts",
"description": "test",
"severity": "Sev4"
},
"schemaId": "test"
},
"runAfter": {},
"type": "Compose"
},
"Compose_2": {
"inputs": {
"Id": "123456789",
"email": "test#test.com",
"priority": "#{body('Parse_JSON')?['essentials']?['severity']}",
"status": "Open",
"subject": "#{body('Parse_JSON')?['essentials']?['description']}"
},
"runAfter": {
"Parse_JSON": [
"Succeeded"
]
},
"type": "Compose"
},
"Parse_JSON": {
"inputs": {
"content": "#outputs('Compose')",
"schema": {
"properties": {
"customProperties": {},
"data": {
"properties": {
"alertContext": {
"properties": {},
"type": "object"
}
},
"type": "object"
},
"essentials": {
"properties": {
"alertContextVersion": {
"type": "string"
},
"alertId": {
"type": "string"
},
"alertRule": {
"type": "string"
},
"description": {
"type": "string"
},
"severity": {
"type": "string"
}
},
"type": "object"
},
"schemaId": {
"type": "string"
}
},
"type": "object"
}
},
"runAfter": {
"Compose": [
"Succeeded"
]
},
"type": "ParseJson"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {},
"triggers": {
"manual": {
"inputs": {},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}

add field conditionally after traversing all nodes in JSON Circe Scala

Assume I have a complex JSON:
{
"type": "object",
"properties": {
"first_name": { "type": "string" },
"last_name": { "type": "string" },
"birthday": { "type": "string", "format": "date" },
"address": {
"type": "object",
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" },
"country": { "type" : "string" }
}
}
}
}
I want to add the field additionalProperties: false to all the json objects having type:object
In the above example, the output json would look like:
{
"type": "object",
"additionalProperties": false,
"properties": {
"first_name": { "type": "string" },
"last_name": { "type": "string" },
"birthday": { "type": "string", "format": "date" },
"address": {
"type": "object",
"additionalProperties": false,
"properties": {
"street_address": { "type": "string" },
"city": { "type": "string" },
"state": { "type": "string" },
"country": { "type" : "string" }
}
}
}
}
This is what I have so far:
val circeJson: io.circe.Json = parser.parse(json).getOrElse(io.circe.Json.Null)
val updatedJson = transform(circeJson)
def transform(js: io.circe.Json): io.circe.Json =
js
.mapArray(_.map(transform))
.mapObject(obj => obj.+:("additionalProperties", io.circe.Json.fromBoolean(false)))
From some reason, it's working and adding the field only for the root level object.
The following is working:
def transformJson(js: io.circe.Json, transformation: JsonObject => JsonObject): io.circe.Json =
js
.mapArray(_.map(transformJson(_, transformation)))
.mapObject(obj => transformation(obj).mapValues(obj => transformJson(obj, transformation)))
val circeJson: io.circe.Json = parser.parse(json).getOrElse(io.circe.Json.Null)
val updatedJson =
transformJson(circeJson, obj => obj.+:("additionalProperties", io.circe.Json.fromBoolean(false)))

I want to validate the following JSON schema in datapower using schema file

{
"email": "005foobar#gmail.com",
"phone": "9867534210",
"country_code": "91",
"firstname":"Rakesh",
"surname" :"A R"
}
I want either firstname or surname to be in my request(like one of: "firstname" or "surname")
For example:
{
"email": "005foobar#gmail.com",
"phone": "9867534210",
"country_code": "91",
"firstname":"Rakesh"
}
or
{
"email": "005foobar#gmail.com",
"phone": "9867534210",
"country_code": "91",
"surname":"A R"
}
Can you please help me with the json schema for my requirement?
For example something like this. It is a little bit complex, but if you want exactly EITHER surname OR fisrtName but NOT together and NOT absence of them it is what you need.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"email": {
"type": "string"
},
"phone": {
"type": "string"
},
"country_code": {
"type": "string"
}
},
"type": "object",
"oneOf": [
{
"properties": {
"email": {
"$ref": "#/definitions/email"
},
"phone": {
"$ref": "#/definitions/phone"
},
"county_code": {
"$ref": "#/definitions/country_code"
},
"firstname": {
"type": "string"
}
},
"required": [
"email",
"phone",
"country_code",
"firstname"
]
},
{
"properties": {
"email": {
"$ref": "#/definitions/email"
},
"phone": {
"$ref": "#/definitions/phone"
},
"county_code": {
"$ref": "#/definitions/country_code"
},
"surname": {
"type": "string"
}
},
"required": [
"email",
"phone",
"country_code",
"surname"
]
}
]
}

POSTMAN returns fail for schema validation test

I have a sample response:
{
"tags": [
{
"id": 1,
"name": "[String]",
"user_id": 1,
"created_at": "2016-12-20T15:50:37.000Z",
"updated_at": "2016-12-20T15:50:37.000Z",
"deleted_at": null
}
]
}
I've written a test for the response:
var schema = {
"type": "object",
"properties": {
"tags": {
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"user_id": { "type": "number" },
"created_at": { "type": "string" },
"updated_at": { "type": "string" },
"deleted_at": { "type": ["string", "null"] }
}
}
}
};
var data = JSON.parse(responseBody);
tests["Valid schema"] = tv4.validate(data, schema);
This test returns [FAIL]. What wrongs in the test?
Thank you for a respond!
There is a problem on the definition of tags, since it's an array instead of an object. You should nest its properties into its items properties.
This code is passing the test:
test_data = {
"tags": [
{
"id": 1,
"name": "[String]",
"user_id": 1,
"created_at": "2016-12-20T15:50:37.000Z",
"updated_at": "2016-12-20T15:50:37.000Z",
"deleted_at": null
}
]
}
test_schema = {
"type": "object",
"properties": {
"tags": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "integer" },
"name": { "type": "string" },
"user_id": { "type": "number" },
"created_at": { "type": "string" },
"updated_at": { "type": "string" },
"deleted_at": { "type": ["string", "null"] }
}
}
}
}
};
tests["Testing schema"] = tv4.validate(test_data, test_schema);
console.log("Validation errors: ", tv4.error);

Why doesn't this JSON validate against this schema?

user_definition.json
{
"definitions": {
"user": {
"type": "object",
"properties": {
"first_name": { "type": "string" },
"last_name": { "type": "string" },
"email": { "type": "email" }
},
"required": ["first_name", "last_name", "email"]
}
}
}
user_schema.json
{
"allOf": [
{ "$ref": "../definitions/user_definition.json#/definitions/user" },
{
"properties": {
"id": { "type": "string" },
"created_at": { "type": "date-time" },
"updated_at": { "type": "date-time" }
},
"required": ["id", "created_at", "updated_at"]
}
]
}
JSON
{
"id":"f10b9c6e-695c-11e5-bf98-c7cb6d8af9eb",
"first_name":"Johanna",
"last_name":"Littel",
"email":"ora_turcotte#ratke.net",
"created_at":"2015-10-02T23:26:00.663Z",
"updated_at":"2015-10-02T23:26:00.663Z"
}
I get the following error when I validate the document against the schema:
expected
{"id":"f10b9c6e-695c-11e5-bf98-c7cb6d8af9eb","first_name":"Johanna","last_name":"Littel","email":"ora_turcotte#ratke.net","created_at":"2015-10-02T23:26:00.663Z","updated_at":"2015-10-02T23:26:00.663Z"}
to match schema "user_response":
{
"allOf": [
{ "$ref": "../definitions/user_definition.json#/definitions/user" },
{
"properties": {
"id": { "type": "string" },
"created_at": { "type": "date-time" },
"updated_at": { "type": "date-time" }
},
"required": ["id", "created_at", "updated_at"]
}
]
}
I know it's pulling in the definition because if I make the ref invalid it will throw an error.
The problem is probably that your schema is invalid. date-time and email are not valid values for the type keyword. You will need to do something like the following instead.
{ "type": "string", "format": "date-time" }
and
{ "type": "string", "format": "email" }