Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ from ju.pydantic_util import is_valid_wrt_model, valid_models
is_valid = is_valid_wrt_model({"name": "Bob", "age": 25}, UserModel)

# Find all models that validate given data
AdminModel = data_to_pydantic_model({"name": "Root", "age": 40, "is_admin": True}, "Admin")
models = [UserModel, ProductModel, AdminModel]
matching = list(valid_models(data, models))
```
Expand All @@ -192,6 +193,7 @@ def fix_field_names(schema):
schema['properties'][new_name] = schema['properties'].pop(old_name)
return schema

source_schema = schema # the JSON schema defined above
code = pydantic_model_to_code(
source_schema,
ingress_transform=fix_field_names,
Expand All @@ -218,6 +220,7 @@ paths = field_paths_and_annotations(UserModel)
descriptions = model_field_descriptions(UserModel)

# Use models as data extraction templates
json_data = data # the dict defined above
extractor = ModelExtractor([UserModel, AdminModel])
data_reader = extractor(json_data) # Returns mapping with model-based paths
values = data_reader['address.city'] # Extract nested values easily
Expand Down
Loading