【问题标题】:Is possible to set the values to below zero and to maximum value, JsonSchema?是否可以将值设置为低于零和最大值,JsonSchema?
【发布时间】:2020-08-08 19:30:47
【问题描述】:

我想验证温度和寒冷系数,其值低于零,但我不确定是否使用最小值:-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"]

            }
        }
    }
}

【问题讨论】:

    标签: json jsonschema weather weather-api json-schema-validator


    【解决方案1】:

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

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

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

    【讨论】:

    • 我也会包含cityid,类型是数字。谢谢你帮助我理解。
    猜你喜欢
    • 2021-09-11
    • 2014-03-26
    • 2013-02-22
    • 1970-01-01
    • 2018-12-29
    • 2022-11-22
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多