使用 "floats" 在邮递员中验证模式时出现问题

Problem validating schema in postman using "floats"

我正在尝试与邮递员一起验证模式。

我使用网站创建了以下架构: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);
});

但问题是出现了一些“type: number”,它应该是“float”。如果我将此数字更改为浮点数,Postman 会显示以下错误消息:

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

有什么帮助吗?

和json一样,只有6种数据类型string,number,array,object,null,boolean,没有“float”这种数据类型。

修复:

"battery_charge": {"type": "float" } --> "battery_charge": {"type": "number" }