I want to generate a schema with two sets of objects. combined with anyOf
import json
from genson import SchemaBuilder
d11 = dict(a=1,b=2)
d12 = dict(a=3,b=4)
d21 = dict(c=2.0)
d22 = dict(c=3.0)
b1=SchemaBuilder()
b1.add_object(d11)
b1.add_object(d12)
s1 = b1.to_schema()
b2=SchemaBuilder()
b2.add_object(d21)
b2.add_object(d22)
s2 = b2.to_schema()
b=SchemaBuilder()
b.add_schema(s1)
b.add_schema(s2)
print(json.dumps(b.to_schema()))
gives
{
"$schema": "http://json-schema.org/schema#",
"type": "object",
"properties": {
"a": {
"type": "integer"
},
"b": {
"type": "integer"
},
"c": {
"type": "number"
}
}
}
whereas I want it to generate:
"$schema": "http://json-schema.org/schema#",
"anyOf": [
{
"type": "array",
"properties": {
"a": {
"type": "integer"
},
"b": {
"type": "integer"
},
"required": ["a", "b"]
}
},
{
"type": "object",
"properties": {
"c": {
"type": "number"
},
"required": ["c"]
}
}
]
}
How can I achieve this?
I want to generate a schema with two sets of objects. combined with anyOf
gives
whereas I want it to generate:
How can I achieve this?