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}"
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With