Skip to content

Commit 3f98d26

Browse files
authored
Merge pull request #14 from RSS-Engineering/allow-multiple-field-inputs
Add the ability to injest a field through multiple names
2 parents a30990f + bb94ac7 commit 3f98d26

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

pydanticrud/backends/dynamodb.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ def __init__(self, schema):
117117
self.definitions = schema.get("definitions")
118118

119119
def _get_type_possibilities(self, field_name) -> Set[tuple]:
120-
field_properties = self.properties[field_name]
120+
field_properties = self.properties.get(field_name)
121+
122+
if not field_properties:
123+
return set()
121124

122125
possible_types = []
123126
if "anyOf" in field_properties:
@@ -154,6 +157,8 @@ def _serialize_field(self, field_name, value):
154157
except (ValueError, TypeError, KeyError):
155158
pass
156159

160+
# If we got a value that is not part of the schema, pass it
161+
# through and let pydantic sort it out.
157162
return value
158163

159164
def serialize_record(self, data_dict) -> dict:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pydanticrud"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
description = "Supercharge your Pydantic models with CRUD methods and a pluggable backend"
55
authors = ["Timothy Farrell <[email protected]>"]
66
license = "MIT"

tests/test_dynamodb.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import random
66

77
import docker
8-
from pydantic import BaseModel as PydanticBaseModel, Field
8+
from pydantic import BaseModel as PydanticBaseModel, Field, root_validator, ValidationError
99
from pydanticrud import BaseModel, DynamoDbBackend, ConditionCheckFailed
1010
import pytest
1111
from pydanticrud.exceptions import DoesNotExist
@@ -46,6 +46,12 @@ class AliasKeyModel(BaseModel):
4646
name: str
4747
type_: str = Field(alias="type")
4848

49+
@root_validator(pre=True)
50+
def type_from_typ(cls, values):
51+
if 'typ' in values:
52+
values['type'] = values.pop('typ')
53+
return values
54+
4955
class Config:
5056
title = "AliasTitle123"
5157
hash_key = "name"
@@ -504,3 +510,13 @@ def test_get_alias_model_data(dynamo, alias_query_data):
504510
data = alias_model_data_generator()
505511
res = AliasKeyModel.get(alias_query_data[0]['name'])
506512
assert res.dict(by_alias=True) == alias_query_data[0]
513+
514+
515+
def test_alias_model_validator_ingest(dynamo):
516+
data = alias_model_data_generator()
517+
AliasKeyModel(**data)
518+
data["typ"] = data.pop("type")
519+
AliasKeyModel(**data)
520+
data.pop("typ")
521+
with pytest.raises(ValidationError):
522+
AliasKeyModel(**data)

0 commit comments

Comments
 (0)