I am triying to validate a schema with postman.
I created the following schema using the website: https://www.liquid-technologies.com/online-json-to-schema-converter
Here's the result:
const schema = {
"type": "object",
"properties": {
"system_id": {"type": "string"},
"start": {"type": "string"},
"end": {"type": "string"},
"period": {"type": "string" },
"unit": {"type": "string"},
"values": {"type": "array",
"items": [
{
"type": "object",
"properties": {
"timestamp": {"type": "string"},
"battery_charge": {"type": "float" },
"battery_discharge": {"type": "number"},
"grid_export": {"type": "number"},
"grid_import": {"type": "number"},
"home_consumption": {"type": "number"},
"solar": {"type": "number"},
"battery_charge_state": {"type": "number"}
},
"required": [
"timestamp",
"battery_charge",
"battery_discharge",
"grid_export",
"grid_import",
"home_consumption",
"solar",
"battery_charge_state"
]
}
]
},
"totals": {
"type": "object",
"properties": {
"battery_charge": {"type": "number"},
"battery_discharge": {"type": "number"},
"grid_export": {"type": "number"},
"grid_import": {"type": "number"},
"home_consumption": {"type": "number"},
"solar": {"type": "number"}
},
"required": [
"battery_charge",
"battery_discharge",
"grid_export",
"grid_import",
"home_consumption",
"solar"
]
}
},
"required": [
"system_id",
"start",
"end",
"period",
"unit",
"values",
"totals"
]
}
pm.test("Schema validation", () => {
pm.response.to.have.jsonSchema(schema);
});
But the problem is that appears some "type: number" and it should be "float". If I change this number to float, Postman displays the following error message:
Schema validation | Error: schema is invalid: data.properties['values'].items should be object,boolean, data.properties['values'].items[0].properties['battery_charge'].type should be equal to one of the allowed values, data.properties['values'].items[0].properties['battery_charge'].type should be array, data.properties['values'].items[0].properties['battery_charge'].type should match some schema in anyOf, data.properties['values'].items should match some schema in anyOf
Json Response:
{
"system_id": "C18208",
"start": "2022-02-06T00:00:00 00:00",
"end": "2022-02-06T23:59:00 00:00",
"period": "minute",
"unit": "kWh",
"values": [{
"timestamp": "2022-02-06T00:00:00 00:00",
"battery_charge": 0.0,
"battery_discharge": 0.0,
"grid_export": 0.0,
"grid_import": 0.0,
"home_consumption": 0.0,
"solar": 0.0,
"battery_charge_state": 100.0
}],
"totals": {
"battery_charge": 9.3,
"battery_discharge": 4.8,
"grid_export": 4.5,
"grid_import": 31.9,
"home_consumption": 32.1,
"solar": 33.3
}
}
Any help please?
CodePudding user response:
With json, there are only 6 data types string,number,array,object,null,boolean
, there is no such data type "float".
Fix:
"battery_charge": {"type": "float" }
--> "battery_charge": {"type": "number" }