Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate Data Models for "oneOf"["required.. JSON Schema models #1875

Open
e-lo opened this issue Mar 6, 2024 · 0 comments
Open

Consolidate Data Models for "oneOf"["required.. JSON Schema models #1875

e-lo opened this issue Mar 6, 2024 · 0 comments

Comments

@e-lo
Copy link

e-lo commented Mar 6, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant