Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Schema - specify string length based on input property

Tags:

jsonschema

Is it possible with JSON Schema to specify a string length that depends on the value of a property in the item that is being validated?

For example, I have a document with a "foo" property with value 3. I would like to ensure that the "bar" property is a string of at least size 3

Sample JSON

{
"foo": 3,
"bar": "111"
}

JSON Schema

{
   "properties" : {
      "foo": {
         "type": "integer",
         "minimum": 1
       }
      "bar": {
         "type": "string",
         "minLength": "{$foo}"
       }
   }
}
like image 724
cordialgerm Avatar asked Oct 19 '22 02:10

cordialgerm


1 Answers

There is a v5 proposal for a $data keyword that would "allow schemas to use values from the data, specified using JSON Pointers or Relative JSON Pointers".

Using your example:

{
   "properties" : {
      "foo": {
         "type": "integer",
         "minimum": 1
       }
      "bar": {
         "type": "string",
         "minLength": { "$data": "1/foo" }
       }
   }
}

Support for the $data keyword will obviously depend upon the validator you are using. Some validators do support the v5 proposals.

like image 156
cartant Avatar answered Nov 15 '22 09:11

cartant