Proper way to describe variant-like structure #422
-
Dear community, I've read quite a few tips and tricks on how to structure JSON schema for non-trivial examples. Sadly, I've got a simple task which seems not so easy to describe in schema. Imagine {
"type": "foo",
"info": "generic comment",
"foo_1": 42,
"foo_2": 666
} {
"type": "bar",
"info": "generic comment v2",
"bar1": [ 6, 6 ],
"bar4": 1.4
} Due to restrictions of Visual Studio Code I am unable to use I was wondering if I could go with (cursed) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The good new is, I don't think {
"allOf": [{ "$ref": "./common" }],
"properties": {
"foo_1": { "type": "number" },
"foo_2": { "type": "number" }
},
"unevaluatedProperties": false
} There are two alternatives you can use that will work in VS Code, but both require some duplication. The first is that you can use {
"allOf": [{ "$ref": "./common" }],
"properties": {
"type": true,
"info": true,
"foo_1": { "type": "number" },
"foo_2": { "type": "number" }
},
"additionalProperties": false
} The second is that you can use {
"allOf": [{ "$ref": "./common" }],
"properties": {
"foo_1": { "type": "number" },
"foo_2": { "type": "number" }
},
"propertyNames": { "enum": ["type", "info", "foo_1", "foo_2"] }
}
If your target is VS Code, this might not be a problem for you. VS Code will do a lot of guessing what was intended and from what I've seen, it usually makes good guesses and provides reasonable messages. I'd say it's worth trying this method and seeing what results you get. But, if you expect the schema to be used with a standard validator, your users would probably thank you to make your schemas more explicit. |
Beta Was this translation helpful? Give feedback.
The good new is, I don't think
$dynamicRef
/$dynamicAnchor
is the solution you're looking for anyway. What you need isunevaluatedProperties
. The bad news is, VS Code doesn't support that either 😢.There are two alternatives you can use that will work in VS Code, but both require some duplication.
The first is that you can use
additionalProperties
, but you have to put some placeholders for the properties that are declared in the common schema.