是否可以将值设置为零以下和最大值,JsonSchema?

Is possible to set the values to below zero and to maximum value, JsonSchema?

我想用小于零的值验证温度和寒冷因素,但我不确定使用最小值是否正确:例如,-10、-20。

温度、寒冷因素和风速的实际最大值也是如此。

什么是正确使用?

非常感谢。

JsonSchema 如下所示:

 "type": "object",
    "properties": {
        "weather": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "windSpeed": {
                        "type": "number",
                        "minimum": 0.00
                    },
                    "cityName": {
                        "type": "string"
                    },
                    "currentConditions": {
                        "type": "string",
                        "enum": [ "Cloud", "Snow", "Sun", "Hail", "Rain", "Sleet", "Heavy Rain"]
                    },
                    "temperature": {
                        "type": "number",
                        "minimum": 0.00
                    },
                    "windDirection": {
                        "type": "string",
                        "enum": ["Northerly", "North easterly", "Easterly", "South easterly", "Southerly", "South westerly", "Westerly", "North westerly"]
                    },
                    "windChillFactor": {
                        "type": "number",
                        "minimum": 0.00
                    }

                },
                "required": ["cityId", "cityName", "currentConditions", "temperature", "windSpeed", "windDirection", "windChillFactor"]

            }
        }
    }
}

一般来说,当不确定这些事情时,我建议采用以下两种方法之一:

  1. 咨询json-schema.org/understanding-json-schema。在那里,只有 multipleOf 被描述为只允许正值,但是对于 minimum/maximum 任何数值(包括负数)都应该没问题(即使示例只使用正值来简化清酒)。
  2. 使用 jsonschemavalidator.net 等在线验证器根据示例数据测试您的模式,以查看它是否如您预期的那样验证失败或成功。

此外,您的架构看起来不错。不过,您可能想在 weather.items.properties 中包含 cityId(而不仅仅是在 weather.items.required 中)以指明其类型。