Combining unevaluatedProperties and ref: # #375
-
| 
         Hi all, Thanks for your work on JSON Schema, work really seems to have picked up a lot since you have people working on it full time. My question is about whether a particular construct is possible or whether I'm just doing it wrong. I would like to have a property which allows as its contents anything from the root schema, plus one extra property. For example, say I have a simple schema defining one property that is required,  and another property  I have tried to combine recursion and unevaluated properties to make it work. This is what I came up with: But it fails: Am I doing something wrong, or is this not possible with the 2020-12 draft?  | 
  
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
| 
         The problem is  {
  "$ref": "#/$defs/base",
  "unevaluatedProperties": false,
  "$defs": {
    "base": { ... }
  }
}Now, the top level can be "closed" while still having an "open" schema available at  {
  "$ref": "#/$defs/base",
  "properties": {
    "newProp": { "type": "string" }
  },
  "unevaluatedProperties": false
} | 
  
Beta Was this translation helpful? Give feedback.
The problem is
"additionalProperties": falseat the top level. Once you do this, you can no longer add properties using schema composition. We sometimes call this a "closed" schema. The way around this is to define your base schema as a definition ($defs) allowing additional/unevaluated properties and then using schema composition at the top level to "close" the schema withunevaluatedProperties.{ "$ref": "#/$defs/base", "unevaluatedProperties": false, "$defs": { "base": { ... } } }Now, the top level can be "closed" while still having an "open" schema available at
#/$defs/basethat can be extended.{ "$ref": "#/$defs/base", "properties": { "newProp": { "type": "string"