JSON Schema - how do I specify that a boolean value must be false?

微笑、不失礼 提交于 2019-12-10 00:45:22

问题


Let's say I have a type that will be boolean, but I don't just want to specify that it will be boolean, I want to specify that it will have the value false. To just specify that it will be boolean I do the following:

{
    "properties": {
        "some_flag": {
            "type": "boolean"
        }
    }
}

I have tried substituting "boolean" above for "false" and false (without quotes), but neither works.


回答1:


Use the enum keyword:

{
    "properties": {
        "some_flag": { "enum": [ false ] }
    }
}

This keyword is designed for such cases. The list of JSON values in an enum is the list of possible values for the currently validated value. Here, there is only one possible value: JSON boolean false.




回答2:


As of draft-6, you can use the const keyword. It's similar to enum, but only takes one value.

{
    "properties": {
        "some_flag": { "const": false }
    }
}


来源:https://stackoverflow.com/questions/16825108/json-schema-how-do-i-specify-that-a-boolean-value-must-be-false

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!