Home > front end >  Json schema array size reference
Json schema array size reference

Time:12-23

I have a json schema with two entities Foo and Bar. Foo contains field size and Bar is an array. I want Bar array size to be equal to Foo.size. Is there any way to achieve this?

My approach is to use reference to size definition in Foo, but it is an invalid schema , since schema validator expects an integer after "minItems" and "maxItems" (I use https://www.jsonschemavalidator.net/).

The approach:

{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "properties": {
    "foo": {"$ref": "#/definitions/Foo"},
    "bar": {"$ref": "#/definitions/Bar"}
  },
  "required": ["foo", "bar"],
  "definitions": {
    "Foo": {
        "type": "object",
        "properties": {
            "size": {"type": "number"}
        },
        "required": ["size"]
    },
    "Bar": {
        "type": "array",
        "minItems": "#/definitions/Foo/properties/size", // invalid
        "maxItems": "#/definitions/Foo/properties/size"  //invalid
    }
  }
}

CodePudding user response:

JSON Schema doesn't support data references this way, but it can be extended to do so.

I support this with JsonSchema.Net and my data vocabulary.

Documentation can be found here.

  • Related