Skip to content

Commit

Permalink
fix: fix an issue with name parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
dbirman committed Oct 22, 2024
1 parent 82f30e3 commit 352315d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/aind_data_schema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def unit_validator(cls, values):
for (unit_name, unit_value) in values:
if "_unit" in unit_name and not unit_value:
var_name = unit_name.rsplit('_unit', 1)[0]
root_name = unit_name.split('_')[0]

# Go through all the values again, if any value matches the variable name
# and is set, then the unit needs to be set as well
Expand All @@ -77,7 +78,7 @@ def unit_validator(cls, values):
for (variable_name, variable_value) in values:
# skip the unit itself
if variable_name is not unit_name:
if var_name in variable_name and variable_value:
if root_name in variable_name and variable_value:
raise ValueError(
f"Unit {unit_name} is required when {variable_name} is set."
)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ class TestModel(AindModel):

# Multi-unit condition
class MultiModel(AindModel):
value_one: Optional[str] = Field(default=None)
value_two: Optional[str] = Field(default=None)
value_one_with_depth: Optional[str] = Field(default=None)
value_two_with_depth: Optional[str] = Field(default=None)
value_unit: Optional[str] = Field(default=None)

self.assertRaises(ValidationError, lambda: MultiModel(
value_one="value"
value_one_with_depth="value"
))

test2 = MultiModel(
value_one="value1",
value_one_with_depth="value1",
value_unit="unit"
)
self.assertIsNotNone(test2)
Expand Down

0 comments on commit 352315d

Please sign in to comment.