Skip to content

Commit

Permalink
PB-847: Refactor item properties validation
Browse files Browse the repository at this point in the history
Split validation function. Remove wrapper function
validate_item_properties_datetimes_dependencies.
  • Loading branch information
benschs committed Aug 15, 2024
1 parent 8a960c4 commit bc3a96d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 44 deletions.
63 changes: 23 additions & 40 deletions app/stac_api/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,36 +364,34 @@ def validate_geometry(geometry):
return geometry


def validate_item_properties_datetimes_dependencies(
def validate_datetime_format(date_string):
try:
if date_string is not None and not isinstance(date_string, datetime):
return fromisoformat(date_string)
return date_string
except ValueError as error:
logger.error("Invalid datetime string %s", error)
raise ValidationError(
_('Invalid datetime string %(error)s'), params={'error': error}, code='invalid'
) from error


def validate_item_properties_datetimes(
properties_datetime, properties_start_datetime, properties_end_datetime, properties_expires
):
'''
Validate the dependencies between the Item datetimes properties
Validate the dependencies between the item datetime properties.
This makes sure that either only the properties.datetime is set or
both properties.start_datetime and properties.end_datetime
both properties.start_datetime and properties.end_datetime are set.
If properties.expires is set, the value must be greater than
the properties.datetime or the properties.end_datetime.
Raises:
django.core.exceptions.ValidationError
'''
try:
if not isinstance(properties_datetime, datetime) and properties_datetime is not None:
properties_datetime = fromisoformat(properties_datetime)
if (
not isinstance(properties_start_datetime, datetime) and
properties_start_datetime is not None
):
properties_start_datetime = fromisoformat(properties_start_datetime)
if (
not isinstance(properties_end_datetime, datetime) and
properties_end_datetime is not None
):
properties_end_datetime = fromisoformat(properties_end_datetime)
if not isinstance(properties_expires, datetime) and properties_expires is not None:
properties_expires = fromisoformat(properties_expires)
except ValueError as error:
logger.error("Invalid datetime string %s", error)
raise ValidationError(
_('Invalid datetime string %(error)s'), params={'error': error}, code='invalid'
) from error
properties_datetime = validate_datetime_format(properties_datetime)
properties_start_datetime = validate_datetime_format(properties_start_datetime)
properties_end_datetime = validate_datetime_format(properties_end_datetime)
properties_expires = validate_datetime_format(properties_expires)

if properties_datetime is not None:
if (properties_start_datetime is not None or properties_end_datetime is not None):
Expand All @@ -405,7 +403,8 @@ def validate_item_properties_datetimes_dependencies(
if properties_expires < properties_datetime:
message = "Property expires can't refer to a date earlier than property datetime"
raise ValidationError(_(message), code='invalid')
else:

if properties_datetime is None:
if properties_end_datetime is None:
message = "Property end_datetime can't be null when no property datetime is given"
logger.error(message)
Expand All @@ -414,8 +413,6 @@ def validate_item_properties_datetimes_dependencies(
message = "Property start_datetime can't be null when no property datetime is given"
logger.error(message)
raise ValidationError(_(message), code='invalid')

if properties_datetime is None:
if properties_end_datetime < properties_start_datetime:
message = "Property end_datetime can't refer to a date earlier than property "\
"start_datetime"
Expand All @@ -427,20 +424,6 @@ def validate_item_properties_datetimes_dependencies(
raise ValidationError(_(message), code='invalid')


def validate_item_properties_datetimes(
properties_datetime, properties_start_datetime, properties_end_datetime, properties_expires
):
'''
Validate datetime values in the properties Item attributes
'''
validate_item_properties_datetimes_dependencies(
properties_datetime,
properties_start_datetime,
properties_end_datetime,
properties_expires,
)


def validate_checksum_multihash_sha256(value):
'''Validate the checksum multihash field
Expand Down
4 changes: 2 additions & 2 deletions app/tests/tests_09/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from stac_api.validators import get_media_type
from stac_api.validators import normalize_and_validate_media_type
from stac_api.validators import validate_content_encoding
from stac_api.validators import validate_item_properties_datetimes_dependencies
from stac_api.validators import validate_item_properties_datetimes


class TestValidators(TestCase):
Expand All @@ -16,7 +16,7 @@ def test_validate_function_invalid_datetime_string(self):
properties_start_datetime = "2001-22-66T08:00:00+00:00"
properties_end_datetime = "2001-11-11T08:00:00+00:00"
properties_expires = None
validate_item_properties_datetimes_dependencies(
validate_item_properties_datetimes(
properties_datetime,
properties_start_datetime,
properties_end_datetime,
Expand Down
1 change: 1 addition & 0 deletions app/tests/tests_10/test_item_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def test_item_create_model_invalid_datetime(self):
item.full_clean()
item.save()

def test_item_create_model_invalid_datetime_order(self):
with self.assertRaises(
ValidationError, msg="end_datetime must not be earlier than start_datetime"
):
Expand Down
4 changes: 2 additions & 2 deletions app/tests/tests_10/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from stac_api.validators import get_media_type
from stac_api.validators import normalize_and_validate_media_type
from stac_api.validators import validate_content_encoding
from stac_api.validators import validate_item_properties_datetimes_dependencies
from stac_api.validators import validate_item_properties_datetimes

from tests.tests_10.data_factory import Factory

Expand All @@ -22,7 +22,7 @@ def test_validate_function_invalid_datetime_string(self):
properties_start_datetime = "2001-22-66T08:00:00+00:00"
properties_end_datetime = "2001-11-11T08:00:00+00:00"
properties_expires = None
validate_item_properties_datetimes_dependencies(
validate_item_properties_datetimes(
properties_datetime,
properties_start_datetime,
properties_end_datetime,
Expand Down

0 comments on commit bc3a96d

Please sign in to comment.