我正在嘗試使用郵遞員驗證模式。
我使用該網站創建了以下架構:https ://www.liquid-technologies.com/online-json-to-schema-converter
結果如下:
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);
});
但問題是出現了一些“型別:數字”,它應該是“浮點數”。如果我將此數字更改為浮動,郵遞員會顯示以下錯誤訊息:
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回應:
{
"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
}
}
請問有什么幫助嗎?
uj5u.com熱心網友回復:
使用json,只有6種資料型別string,number,array,object,null,boolean,沒有“float”這樣的資料型別。
使固定:
"battery_charge": {"type": "float" }-->"battery_charge": {"type": "number" }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/441325.html
