Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jsonschema

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.

like image 915
tadasajon Avatar asked May 29 '13 22:05

tadasajon


People also ask

How do you define a boolean in a schema?

The boolean data type is used to specify a true or false value. Note: Legal values for boolean are true, false, 1 (which indicates true), and 0 (which indicates false).

Are booleans quoted in JSON?

Short answer, yes that is the proper way to send the JSON. You should not be placing anything other than a string inside of quotes. As for your bool value, if you want it to convert straight into a bool, than you do not need to include the quotes.

How do you pass a boolean value in the postman body?

So even if you send a parameter like “active=true”, it is still a string, this is how the HTTP protocol works. Hope this helps clarify the mystery. var bflag = Boolean(“true”); var bflag1 = Boolean(“false”);


2 Answers

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.

like image 162
fge Avatar answered Oct 08 '22 10:10

fge


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 }     } } 
like image 25
Relequestual Avatar answered Oct 08 '22 12:10

Relequestual