write a json schema that is essentially MyObject = { [any key (string)]: MyObject, ...}…
- User
write a json schema that is essentially
MyObject = { [any key (string)]: MyObject, ...} | null
- Assistant
Certainly! You want a JSON Schema where the value is either:
null, or- an object with keys as strings, and values recursively matching the same schema.
Here is a JSON Schema for that:
{ "$schema": "http://json-schema.org/draft-07/schema#", "title": "MyObject", "type": ["object", "null"], "additionalProperties": { "$ref": "#" } }Explanation:
- The root schema accepts either
"null"or"object". - If it's an object, it can have arbitrary string keys (
additionalPropertiesallows any key). - Each property's value must also be validated against the same schema (
"$ref": "#") — i.e., recursively the same constraints. - This creates a recursive definition where the object structure may nest arbitrarily or can be
null.
If you want draft 2019-09 or later, you can replace the
$schemaURL accordingly. But the above works with draft-07, which is widely supported. - User
Add the constraint that if it's an object it must have at least one key
- Assistant
Got it! You want the schema to allow:
null, or- an object with at least one key, where each value is recursively the same schema.
Here's the updated JSON Schema with the
minProperties: 1constraint added:{ "$schema": "http://json-schema.org/draft-07/schema#", "title": "MyObject", "type": ["object", "null"], "minProperties": 1, "additionalProperties": { "$ref": "#" } }How it works:
"type": ["object", "null"]allows the root to be eithernullor an object."minProperties": 1requires that if it's an object, it must have at least one property."additionalProperties": { "$ref": "#" }means the value of each property must also be validated by the same schema recursively.
This schema ensures no empty objects, only
nullor non-empty objects with recursively the same pattern. - User
Okay now I want a json schema that is like this:
Total = {"list": list[str], "obj": MyObject}
- Assistant
Sure! You want a JSON Schema for an object with two properties:
"list": an array of strings"obj": an instance of the recursiveMyObjectschema you defined earlier (null or non-empty object with recursive values)
Let's combine that.
First, recall the
MyObjectschema:{ "type": ["object", "null"], "minProperties": 1, "additionalProperties": { "$ref": "#" } }To avoid duplication and use recursive references, let's define both schemas in one document using
$defs(draft-07 usesdefinitions, but is preferred in newer drafts — I'll use for compatibility with draft-07). - User
The top item in obj should have exactly one key
- Assistant
Got it! You want the
"obj"to be either:null, or- an object with exactly one key (instead of at least one key),
and recursively the same for all nested objects inside.
Here's the modified schema for
Totalwhere"obj":- is
"null"or an object - if an object, must have exactly one key (
minProperties: 1,maxProperties: 1) - recursively applies the same rules for all nested objects
{ "$schema": "http://json-schema.org/draft-07/schema#", "title": "Total", "type": "object", "properties": { "list": { "type": "array", "items": { "type": "string" } }, "obj": { "$ref": "#/definitions/MyObject" } }, "required": ["list", "obj"], "additionalProperties": false, "definitions": { "MyObject": { "type": ["object", "null"], "minProperties": 1, "maxProperties": 1, "additionalProperties": { "$ref": "#/definitions/MyObject" } } } } - User
NO MyObject in general can have more keys
- Assistant
Thanks for the clarification!
So, for your
MyObject:- In general, it can have at least one or more keys (so
minProperties: 1, no limit on max) - Recursively applies.
But for the top-level
objproperty inTotal, it must be either:null, or
- In general, it can have at least one or more keys (so
- User
non null
- Assistant
Understood! You want:
- At the top level of
"obj": it must be an object (notnull) with exactly one key. - Nested objects (
MyObject) can benullor an object with at least one key, recursively.
In other words, at the top level:
"obj": { "type": "object", "minProperties": 1, "maxProperties": 1, ... }(not allowing
null), but inside, you keep the previous recursiveMyObjectthat allowsnullor object.
Here is the updated full schema:
{ "$schema": "http://json-schema.org/draft-07/schema#", "title": "Total", "type": "object", "properties": { "list": { "type": "array", "items": { "type": "string" } }, "obj": { "type": "object", "minProperties": 1, "maxProperties": 1, "additionalProperties": { "$ref": "#/definitions/MyObject" } } }, "required": ["list", "obj"], "additionalProperties": false, "definitions": { "MyObject": { "type": ["object", "null"], "minProperties": 1, "additionalProperties": { "$ref": "#/definitions/MyObject" } } } } - At the top level of
- User
maxProperties is not supported by my custom parser. Can you do an alternative?
- Assistant