lundi 30 mars 2020

How do I subdivide a json schema into multiple schemas and not allow any properties to be part of the schema outside these other schemas?

Json Schema Validation Problem

I ran into a problem that I expect will be pretty common when building complex schemas. Suppose in this example that we want a Sample Schema to be an object with properties fooA1, fooA2, fooB1 and fooB2, but no other properties. We also want the advantage of being able to separate the subschemas into files fooA.json and fooB.json. How can we meet both of these requirements?

Main.json

{
  "title": "Sample Schema",
  "description": "Trying to combine two schemas",
  "allOf": [
    {"$ref": "classpath:JsonSchema/Common/fooA.json"}.
    {"$ref": "classpath:JsonSchema/Common/fooB.json"}
  ]
}

fooA.json

{
  "type": "object",
  "properties": {
    "fooA1": {
      "type": "integer"
    },
    "fooA2": {
      "type": "integer"
    }
  },
  "required": ["fooA1", "fooA2"]
}

fooB.json

{
  "type": "object",
  "properties": {
    "fooB1": {
      "type": "integer"
    },
    "fooB2": {
      "type": "integer"
    }
  },
  "required": ["fooB1", "fooB2"]
}

Aucun commentaire:

Enregistrer un commentaire