Skip to content

Commit 0152e11

Browse files
committed
fix: add conversion to dict type for attributes and relations in Resource check_all_values validator
1 parent b00799d commit 0152e11

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

jsonapi_pydantic/constants.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass
5+
class JsonApiVersion:
6+
v1_0 = "1.0"
7+
v1_1 = "1.1"
8+
9+
10+
JSON_API_VERSION = JsonApiVersion
11+
JSON_API_MEDIA_TYPE = "application/vnd.api+json"
12+
13+
__all__ = ["JSON_API_MEDIA_TYPE"]

jsonapi_pydantic/v1_0/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from jsonapi_pydantic.v1_0.error import *
22
from jsonapi_pydantic.v1_0.jsonapi import *
33
from jsonapi_pydantic.v1_0.links import *
4+
from jsonapi_pydantic.v1_0.meta import *
45
from jsonapi_pydantic.v1_0.resource import *
56
from jsonapi_pydantic.v1_0.resource_identifier import *
67
from jsonapi_pydantic.v1_0.toplevel import *

jsonapi_pydantic/v1_0/resource/resource.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,24 @@ class Resource(BaseModel):
2525
def check_all_values(cls, values: dict) -> dict:
2626
# More about these restrictions: https://jsonapi.org/format/#document-resource-object-fields
2727
attributes, relationships = values.get("attributes"), values.get("relationships")
28+
try:
29+
attributes, relationships = dict(attributes), dict(relationships)
30+
except ValueError:
31+
raise ValueError("Attributes and relationships must be json objects.")
32+
2833
if attributes and (attributes.get("id") or attributes.get("type")):
2934
raise ValueError("Attributes can not have keys named id or type.")
35+
3036
if relationships and (relationships.get("id") or relationships.get("type")):
3137
raise ValueError("Relationships can not have keys named id or type.")
38+
3239
if attributes and relationships:
3340
for key in attributes:
3441
if relationships.get(key):
3542
raise ValueError(
3643
f"A resource can not have an attribute and relationship with the same name. Name: {key}."
3744
)
45+
3846
return values
3947

4048

0 commit comments

Comments
 (0)