0

I have already looked at:


I need my JSON schema to enforce that an object has all properties from an enum and I cannot puzzle out how.

stuff.list.json

Note: Imagine this, but much longer.

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://example.com/product.schema.json",
    "title": "ListOfStuff",
    "description": "List of stuff",
    "enum": [
        "Thing1",
        "Thing2",
        "Thing3"
    ],
    "uniqueItems": true
}

stuff.schema.json

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://example.com/product.schema.json",
    "title": "Stuff Defined",
    "description": "Stuff in detail",
    "type": "object",
    "properties": {
        /* a property for each enum value of stuff.list.json  */: { <----- ???
            "type": "array",
            "items": { /* Schema for each item */ }
        }
    },
    "required": [ /* $ref: ./stuff.list.json ?? */ ]
 }

The output I'm aiming for it to enforce is:

 {
     "Thing1": [],
     "Thing2": [],
     "Thing3": []
 }
Douglas Gaskell
  • 9,017
  • 9
  • 71
  • 128

1 Answers1

0

Are you looking for the required keyword? You can put it in a definition and reference it later:

"$defs": {
  "required_properties": {
    "required": [
      "Thing1",
      "Thing2",
      "Thing3"
    ]
  }
},
...

and then in your main schema:

{
  ...,
  "type": "object",
  "$ref": "stuff.list.json#/$defs/required_properties",
  ...,
}
Ether
  • 53,118
  • 13
  • 86
  • 159