romis2012 / pydantic-collections

Collections of pydantic models
Apache License 2.0
42 stars 4 forks source link

Support for collections other than list, for example dict #6

Open bramp opened 3 weeks ago

bramp commented 3 weeks ago

I would like to encode a dict[str, SomeBaseModel], but currently it fails with:

class MyCollection(BaseCollectionModel[MyModel]):
    pass
pydantic_core._pydantic_core.ValidationError: 1 validation error for MyCollection
  Input should be a valid list [type=list_type, input_value={...}, input_type=dict]
romis2012 commented 3 weeks ago

This feature may be added in the next major version. Right now you can use pydantic RootModel:

class MyCollection(RootModel[dict[str, SomeBaseModel]]):
    pass
bramp commented 3 weeks ago

Ah thanks!