Skip to content

Commit

Permalink
feat: validate_schema accepts a validator argument, instead of using …
Browse files Browse the repository at this point in the history
…JSON Schema Draft 4
  • Loading branch information
jpmckinney committed Jun 15, 2023
1 parent dfcd505 commit b1ad2a7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
25 changes: 25 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
Changelog
=========

0.2.0 (2023-06-14)
------------------

Changed
~~~~~~~

- :meth:`~jscc.testing.checks.validate_schema` accepts a ``validator`` argument, instead of using JSON Schema Draft 4. To preserve the same behavior as before, change:

.. code-block:: python
validate_schema(path, data, schema)
to:

.. code-block:: python
from jsonschema import FormatChecker
from jsonschema.validators import Draft4Validator
# Declare this at the module level, to minimize initializations.
validator = Draft4Validator(Draft4Validator.META_SCHEMA, format_checker=FormatChecker())
# Meanwhile, elsewhere...
validate_schema(path, data, schema, validator)
0.1.1 (2023-04-19)
------------------

Expand Down
33 changes: 23 additions & 10 deletions jscc/testing/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,29 @@ def test_schema_valid(path, name, data):
.. code-block:: python
import jsonref
from jscc.testing.checks import (validate_array_items, validate_codelist_enum, validate_deep_properties,
validate_items_type, validate_letter_case, validate_merge_properties,
validate_metadata_presence, validate_null_type, validate_object_id, validate_ref,
validate_schema)
from jscc.testing.checks import (
validate_array_items,
validate_codelist_enum,
validate_deep_properties,
validate_items_type,
validate_letter_case,
validate_merge_properties,
validate_metadata_presence,
validate_null_type,
validate_object_id,
validate_ref,
validate_schema,
)
from jsonschema import FormatChecker
from jsonschema.validators import Draft4Validator
validator = Draft4Validator(Draft4Validator.META_SCHEMA, format_checker=FormatChecker())
def validate_json_schema(path, name, data, schema):
errors = 0
errors += validate_schema(path, data, schema)
errors += validate_schema(path, data, schema, validator)
errors += validate_array_items(path, data)
errors += validate_items_type(path, data)
errors += validate_codelist_enum(path, data)
Expand All @@ -47,7 +61,7 @@ def validate_json_schema(path, name, data, schema):
# Here, we don't add to `errors`, in order to not count these warnings as errors.
validate_deep_properties(path, data)
assert not errors, 'One or more JSON Schema files are invalid. See warnings below.'
assert not errors, "One or more JSON Schema files are invalid. See warnings below."
You can monkeypatch ``warnings.formatwarning`` to customize and abbreviate the warning messages:
Expand Down Expand Up @@ -80,8 +94,6 @@ def formatwarning(message, category, filename, lineno, line=None):
from warnings import warn

import jsonref
from jsonschema import FormatChecker
from jsonschema.validators import Draft4Validator as validator

from jscc.exceptions import (CodelistEnumWarning, DeepPropertiesWarning, DuplicateKeyError, ItemsTypeWarning,
LetterCaseWarning, MergePropertiesWarning, MetadataPresenceWarning, NullTypeWarning,
Expand Down Expand Up @@ -222,19 +234,20 @@ def test_invalid_csv():
yield path, e


def validate_schema(path, data, schema):
def validate_schema(path, data, schema, validator):
"""
Warns and returns the number of errors relating to JSON Schema validation.
Uses the `jsonschema <https://python-jsonschema.readthedocs.io/>`__ module.
:param object schema: the metaschema against which to validate
:param validator: The validator to use
:returns: the number of errors
:rtype: int
"""
errors = 0

for error in validator(schema, format_checker=FormatChecker()).iter_errors(data):
for error in validator.iter_errors(data):
errors += 1
warn(f"{json.dumps(error.instance, indent=2)}\n{error.message} ({'/'.join(error.absolute_schema_path)})\n",
SchemaWarning)
Expand Down
5 changes: 4 additions & 1 deletion tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import jsonref
import pytest
from jsonschema import FormatChecker
from jsonschema.validators import Draft4Validator

import jscc.testing.checks
from jscc.exceptions import DuplicateKeyError
Expand Down Expand Up @@ -258,8 +260,9 @@ def test_validate_ref_fail():


def test_validate_schema():
validator = Draft4Validator(Draft4Validator.META_SCHEMA, format_checker=FormatChecker())
with pytest.warns(UserWarning) as records:
errors = validate('schema', parse('meta-schema.json'))
errors = validate('schema', parse('meta-schema.json'), validator)

assert [str(record.message) for record in records] == [
"[]\n[] is not of type 'object' (properties/properties/type)\n",
Expand Down

0 comments on commit b1ad2a7

Please sign in to comment.