Skip to content

Commit 200dc4c

Browse files
authored
fix: updates record validation (#583)
1 parent b3446d3 commit 200dc4c

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/aind_data_schema/metadata.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import Dict, List, Optional
66
from uuid import UUID, uuid4
77

8-
from pydantic import Field, root_validator, validate_model
8+
from pydantic import Field, root_validator, validate_model, validator
99

1010
from aind_data_schema.base import AindCoreModel
1111
from aind_data_schema.data_description import DataDescription
@@ -37,7 +37,7 @@ class Metadata(AindCoreModel):
3737
"""The records in the Data Asset Collection needs to contain certain fields
3838
to easily query and index the data."""
3939

40-
schema_version: str = Field("0.0.4", description="schema version", title="Version", const=True)
40+
schema_version: str = Field("0.0.5", description="schema version", title="Version", const=True)
4141

4242
id: UUID = Field(
4343
default_factory=uuid4,
@@ -96,6 +96,25 @@ class Metadata(AindCoreModel):
9696
None, title="Instrument", description="Instrument, which is a collection of devices"
9797
)
9898

99+
@validator(
100+
"subject",
101+
"data_description",
102+
"procedures",
103+
"session",
104+
"rig",
105+
"processing",
106+
"acquisition",
107+
"instrument",
108+
pre=True,
109+
)
110+
def validate_core_fields(cls, value, values, field):
111+
"""Don't automatically raise errors if the core models are invalid"""
112+
if isinstance(value, dict):
113+
core_model = field.type_.construct(**value)
114+
else:
115+
core_model = value
116+
return core_model
117+
99118
@root_validator(pre=False)
100119
def validate_metadata(cls, values):
101120
"""Validator for metadata"""

tests/test_metadata.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ def test_invalid_core_models(self):
8787
)
8888
self.assertEqual(MetadataStatus.INVALID, d2.metadata_status)
8989

90+
# Tests constructed via dictionary
91+
d3 = Metadata(
92+
name="ecephys_655019_2023-04-03_18-17-09",
93+
location="bucket",
94+
subject=json.loads(Subject.construct().json()),
95+
procedures=json.loads(Procedures.construct().json()),
96+
)
97+
self.assertEqual(MetadataStatus.INVALID, d3.metadata_status)
98+
9099

91100
if __name__ == "__main__":
92101
unittest.main()

0 commit comments

Comments
 (0)