datamodel-code-generator
datamodel-code-generator copied to clipboard
Consolidate Data Models for "oneOf"["required.. JSON Schema models
Is your feature request related to a problem? Please describe.
One of the reasons why I want to use pydantic instead of json-schema (where the source of truth for data models I'm using lie) is because if much easier error codes and information when data doesn't validate. However, when translating a json schema that specifies that one of or any of a set of requirements must be met, it creates a bunch of separate data models and puts them together in a single data model that is a union. This results in error codes that are....not very helpful. It creates a laundry list of all the possible sub models that aren't being met.
...
"oneOf": [
{"required":["option_1"]},
{"required":["option_2"]},
{"required":["option_3"]},
...
class ParentModel1(BaseModel):
other_info:....
option_1: Option1
option_2: Optional[Option2] = None
option_3: Optional[Option3] = None
class ParentModel2(BaseModel):
other_info:....
option_1: Optional[Option1] = None
option_2: Option2
option_3: Optional[Option3] = None
class ParentModel3(BaseModel):
other_info:....
option_1: Optional[Option1] = None
option_2: Optional[Option2] = None
option_3: Option3
class ParentModel(
RootModel[
Union[
ParentModel1,
ParentModel2,
ParentModel3, #note that in my case I have a few dozen of these...
]
]
):
root: Annotated[
Union[
ParentModel1,
ParentModel2,
ParentModel3,
],
Field(title='Parent Schema'),
]
Describe the solution you'd like Translation of a "oneOf" statement (and similar) to a pydantic @rootmodel validator.
Describe alternatives you've considered After spending a few hours trying to accurately post-process the resulting data models, I currently edit the resulting data models by hand, which isn't traceable or reproducible in a data pipeline.
I also did a search to see if other issues like this had been opened and couldn't immediately find any - which was surprising to me so maybe I missed something?
Additional context Add any other context or screenshots about the feature request here.