Skip to content

Commit e93c553

Browse files
committed
Switch back to model validator for meta check
1 parent 2595b4f commit e93c553

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

optimade/models/entries.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import datetime
22
from typing import Annotated, Any, ClassVar, Literal
33

4-
from pydantic import BaseModel, ValidationInfo, field_validator
4+
from pydantic import BaseModel, field_validator, model_validator
55

66
from optimade.models.jsonapi import Attributes, Meta, Relationships, Resource
77
from optimade.models.optimade_json import (
@@ -217,27 +217,29 @@ class EntryResource(Resource):
217217
),
218218
] = None
219219

220-
@field_validator("meta", mode="before")
221-
def check_meta(cls, meta: Any, info: ValidationInfo) -> dict | None:
220+
@model_validator(mode="before")
221+
def check_meta(cls, data: Any) -> dict | None:
222222
"""Validator to check whether the per-entry `meta` field is valid,
223223
including stripping out any per-property metadata for properties that
224224
do not otherwise appear in the model.
225225
226226
"""
227+
228+
meta = data.get("meta")
227229
if not meta:
228-
return meta
230+
return data
229231

230232
if property_metadata := meta.pop("property_metadata", None):
231233
# check that all the fields under property metadata are in attributes
232-
attributes = info.data.get("attributes", {})
234+
attributes = data.get("attributes", {})
233235
property_error_fields: list[str] = []
234236
for subfield in property_metadata:
235237
if subfield not in attributes:
236238
property_error_fields.append(subfield)
237239

238240
if property_error_fields:
239241
raise ValueError(
240-
f"The keys under the field `property_metadata` need to match with the field names in attributes. The field(s) {property_error_fields} are however not present in attributes."
242+
f"The keys under the field `property_metadata` need to match with the field names in attributes. The field(s) {property_error_fields} are however not present in attributes {attributes}"
241243
)
242244

243245
meta["property_metadata"] = property_metadata
@@ -253,7 +255,8 @@ def check_meta(cls, meta: Any, info: ValidationInfo) -> dict | None:
253255
f"The keys under the field `meta` need to be prefixed if not otherwise defined. The field(s) {meta_error_fields} are not defined for per-entry `meta`."
254256
)
255257

256-
return meta
258+
data["meta"] = meta
259+
return data
257260

258261

259262
class EntryInfoProperty(BaseModel):

0 commit comments

Comments
 (0)