Skip to content

Commit

Permalink
repair date validation
Browse files Browse the repository at this point in the history
  • Loading branch information
willronchetti committed Aug 9, 2023
1 parent 9df20a9 commit 0a12830
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ snovault
Change Log
----------

10.0.1
======

* Extend ``FormatChecker`` to ensure date and date-time validation


10.0.0
======

Expand Down
128 changes: 124 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "dcicsnovault"
version = "10.0.0"
version = "10.0.1"
description = "Storage support for 4DN Data Portals."
authors = ["4DN-DCIC Team <[email protected]>"]
license = "MIT"
Expand Down Expand Up @@ -82,7 +82,7 @@ xlrd = "^1.0.0"
"zope.deprecation" = "^4.4.0"
"zope.interface" = ">=4.7.2,<6"
"zope.sqlalchemy" = "1.6"
jsonschema = "^4.18.4"
jsonschema = {extras = ["format-nongpl"], version = "^4.19.0"}

[tool.poetry.dev-dependencies]
botocore-stubs = ">=1.29.119" # no particular version required, but this speeds up search
Expand Down
7 changes: 2 additions & 5 deletions snovault/schema_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from datetime import datetime
from dcicutils.misc_utils import ignored
from snovault.schema_validation import SerializingSchemaValidator
from jsonschema import FormatChecker
from jsonschema import Draft202012Validator
from jsonschema import RefResolver
from jsonschema.exceptions import ValidationError
import os
Expand Down Expand Up @@ -355,9 +355,6 @@ class SchemaValidator(SerializingSchemaValidator):
SERVER_DEFAULTS = SERVER_DEFAULTS


format_checker = FormatChecker()


def load_schema(filename):
filename = favor_app_specific_schema(filename)
if isinstance(filename, dict):
Expand Down Expand Up @@ -403,7 +400,7 @@ def validate(schema, data, current=None, validate_current=False):
dict validated contents, list of errors
"""
resolver = NoRemoteResolver.from_schema(schema)
sv = SchemaValidator(schema, resolver=resolver, format_checker=format_checker)
sv = SchemaValidator(schema, resolver=resolver, format_checker=Draft202012Validator.FORMAT_CHECKER)
validated, errors = sv.serialize(data)
# validate against current contents if validate_current is set
if current and validate_current:
Expand Down
58 changes: 57 additions & 1 deletion snovault/tests/test_schema_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from snovault.schema_utils import (
_update_resolved_data, _handle_list_or_string_value, resolve_merge_refs
_update_resolved_data, _handle_list_or_string_value, resolve_merge_refs,
validate
)


Expand Down Expand Up @@ -364,3 +365,58 @@ def test_schema_utils_resolve_merge_ref_in_embedded_schema(testapp):
embedded = testapp.get(f'/{atid}?frame=embedded', status=200).json
assert embedded['link']['quality'] == 5
assert embedded['link']['linked_targets'][0]['test_description'] == 'test'


@pytest.mark.parametrize('invalid_date', [
'not a date',
'also-not-a-date',
'10-5-2022',
'10-05-almost',
'10-05-2023f'
])
def test_schema_utils_validates_dates(testapp, invalid_date):
""" Tests that our validator will validate dates """
schema = {
"type": "object",
"properties": {
"date_property": {
"type": "string",
"format": "date"
}
}
}
_, errors = validate(schema, {
'date_property': invalid_date
})
date_error = str(errors[0])
assert f"'{invalid_date}' is not a 'date'" in date_error


@pytest.mark.parametrize('invalid_date_time', [
'not a date',
'also-not-a-date',
'10-5-2022',
'10-05-almost',
'10-05-2023f',
'1424-45-93T15:32:12.9023368Z',
'20015-10-23T15:32:12.9023368Z',
'2001-130-23T15:32:12.9023368Z',
'2001-10-233T15:32:12.9023368Z',
'2001-10-23T153:32:12.9023368Z'
])
def test_schema_utils_validates_date_times(testapp, invalid_date_time):
""" Tests that our validator will validate date-time """
schema = {
"type": "object",
"properties": {
"date_time_property": {
"type": "string",
"format": "date-time"
}
}
}
_, errors = validate(schema, {
'date_time_property': invalid_date_time
})
date_error = str(errors[0])
assert f"'{invalid_date_time}' is not a 'date-time'" in date_error

0 comments on commit 0a12830

Please sign in to comment.