Skip to content

Commit

Permalink
Added validation of iterable and multi-unit observations
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerkin committed Apr 24, 2019
1 parent 74fa34c commit 41b2e38
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion sciunit/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ def __init__(self, *args, **kwargs):
"a `test` keyword argument"))
super(ObservationValidator, self).__init__(*args, **kwargs)
register_type(pq.quantity.Quantity, 'quantity')

def _validate_iterable(self, is_iterable, key, value):
"""Validate fields with `iterable` key in schema set to True"""
if is_iterable:
try:
iter(value)
except TypeError:
self._error(key, "Must be iterable (e.g. a list or array)")

def _validate_units(self, has_units, key, value):
"""Validate fields with `units` key in schema set to True.
Expand All @@ -41,7 +49,12 @@ def _validate_units(self, has_units, key, value):
{'type': 'boolean'}
"""
if has_units:
required_units = self.test.units
if isinstance(self.test.units, dict):
required_units = self.test.units[key]
else:
required_units = self.test.units
if not isinstance(value, pq.quantity.Quantity):
self._error(key, "Must be a python quantity")
if not isinstance(value, pq.quantity.Quantity):
self._error(key, "Must be a python quantity")
provided_units = value.simplified.units
Expand Down

0 comments on commit 41b2e38

Please sign in to comment.