0

I would like the ability to write a jsonschema fragment like:

{
   "type":"array",
   "items":{
      "type":"number",
      "lag":2
   }
}

which succeeds on an array of numbers whose values are all 2 apart: eg

[ 1, 3, 5, 7 ]

I am able to describe the custom format as a function from list to bool.

Is this possible?

0

3 Answers 3

1

No there is not. You can find the available array validations here: https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.4

You can specify that a number must be a multiple of x: https://json-schema.org/understanding-json-schema/reference/numeric.html#multiples but of course this only does part of what you want to achieve.

1
  • Restricting the set to a multiple of x does not do "part of what" the OP wants to do, unless x = 1. Commented Jun 30, 2020 at 7:28
0

If you literally mean odd number, you can do:

{
   "type":"array",
   "items":{
      "allOf": [{"type":"number"}, {"not": {"multipleOf": 2}}],
   }
}
0

It is quite simple. I defined a custom validator https://python-jsonschema.readthedocs.io/en/stable/creating/. Unfortunately I have to do validation inside the validator that the instance is an array of numbers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.