Skip to content

Commit

Permalink
Fix: Don't treat 0 number as an empty attribute value (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
psrok1 committed Feb 26, 2024
1 parent 28a0362 commit 9e97428
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions mwdb/schema/attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class AttributeValueSchema(Schema):

@validates("value")
def validate_value(self, value):
# Currently only truthy values and False are allowed as values
if not value and value is not False:
# 0 and False are not empty values
if value in ["", {}, [], None]:
raise ValidationError("Value shouldn't be empty")


Expand Down
16 changes: 16 additions & 0 deletions tests/backend/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,19 @@ def test_json_attribute_uniqueness(admin_session, attr_session, random_attribute
and attr["value"] == first_value == second_value
]
assert len(stored_attributes) == 1


def test_attribute_falsy_values(admin_session, random_attribute):
sample_id, attr_name = random_attribute
admin_session.add_attribute(sample_id, attr_name, 0)
admin_session.add_attribute(sample_id, attr_name, False)
with ShouldRaise(400):
admin_session.add_attribute(sample_id, attr_name, [])
with ShouldRaise(400):
admin_session.add_attribute(sample_id, attr_name, {})
with ShouldRaise(400):
admin_session.add_attribute(sample_id, attr_name, None)
with ShouldRaise(400):
admin_session.add_attribute(sample_id, attr_name, "")
admin_session.add_attribute(sample_id, attr_name, ["nonempty"])
admin_session.add_attribute(sample_id, attr_name, {"nonempty": None})

0 comments on commit 9e97428

Please sign in to comment.