Skip to content

Commit

Permalink
Merge pull request #11 from duncandewhurst/10-defs
Browse files Browse the repository at this point in the history
Add support for $defs keyword
  • Loading branch information
jpmckinney committed Jul 6, 2023
2 parents 8e5589d + 5571468 commit 4444f60
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
15 changes: 15 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
Changelog
=========

0.2.3 (2023-07-06)
------------------

Changed
~~~~~~~

- Add support for `$defs` keyword in:

- :meth:`jscc.testing.checks.validate_letter_case`
- :meth:`jscc.testing.checks.validate_metadata_presence`
- :meth:`jscc.testing.checks.validate_null_types`
- :meth:`jscc.testing.checks.validate_deep_properties`
- :meth:`jscc.schema.is_json_schema`
- :meth:`jscc.schema.is_json_merge_patch`

0.2.2 (2023-06-14)
------------------

Expand Down
4 changes: 2 additions & 2 deletions jscc/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def is_json_schema(data):
:returns: whether the JSON data is a JSON Schema
:rtype: bool
"""
return '$schema' in data or 'definitions' in data or 'properties' in data
return '$schema' in data or 'definitions' in data or '$defs' in data or 'properties' in data


def is_json_merge_patch(data):
Expand All @@ -35,7 +35,7 @@ def is_json_merge_patch(data):
:returns: whether the JSON data is a JSON Merge Patch
:rtype: bool
"""
return '$schema' not in data and ('definitions' in data or 'properties' in data)
return '$schema' not in data and ('definitions' in data or '$defs' in data or 'properties' in data)


def is_array_of_objects(field):
Expand Down
15 changes: 10 additions & 5 deletions jscc/testing/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def block(path, data, pointer):
if not re.search(r'^[a-z][A-Za-z]+$', key) and key not in property_exceptions:
errors += 1
warn(f"{path}: {pointer}/{key} field isn't lowerCamelCase ASCII letters", LetterCaseWarning)
elif parent == 'definitions':
elif parent in ('definitions', '$defs'):
for key in data.keys():
if not re.search(r'^[A-Z][A-Za-z]+$', key) and key not in definition_exceptions:
errors += 1
Expand All @@ -276,7 +276,7 @@ def validate_metadata_presence(*args, allow_missing=_false):
:returns: the number of errors
:rtype: int
""" # noqa: E501
schema_fields = {'definitions', 'deprecated', 'items', 'patternProperties', 'properties'}
schema_fields = {'definitions', '$defs', 'deprecated', 'items', 'patternProperties', 'properties'}
schema_sections = {'patternProperties'}
required_properties = {'title', 'description'}

Expand Down Expand Up @@ -363,7 +363,7 @@ def validate_null_type(path, data, pointer='', no_null=False, expect_null=True,
required = data.get('required', [])

for key, value in data.items():
if key in ('properties', 'definitions'):
if key in ('properties', 'definitions', '$defs'):
for k, v in data[key].items():
expect_null = key == 'properties' and k not in required
errors += validate_null_type(path, v, pointer=f'{pointer}/{key}/{k}', **kwargs, no_null=no_null,
Expand Down Expand Up @@ -528,7 +528,7 @@ def validate_deep_properties(*args, allow_deep=()):
"""
Warns and returns the number of errors relating to deep objects.
The schema must use "definitions" instead of nesting "properties".
The schema must use "definitions" or "$defs" instead of nesting "properties".
:param allow_deep: JSON Pointers of fields to ignore
:type allow_deep: list, tuple or set
Expand All @@ -544,7 +544,12 @@ def block(path, data, pointer):
else:
grandparent = None

if pointer and grandparent != 'definitions' and 'properties' in data and pointer not in allow_deep:
if (
pointer
and grandparent not in ('definitions', '$defs')
and 'properties' in data
and pointer not in allow_deep
):
errors += 1
warn(f'{path} has "properties" within "properties" at {pointer}', DeepPropertiesWarning)

Expand Down

0 comments on commit 4444f60

Please sign in to comment.