Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema: Annotations to oneOf and add oneOfEnumSelectorField #376

Merged
2 commits merged into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/schema/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Added
- ``Country.name`` is now a required field (previously it was defined as "MUST" in the description).
- ``Jurisdiction.name`` is now a required field (previously it was defined as "MUST" in the description).
- ``SecuritiesListing.stockExchangeJurisdiction`` has minimum and maximum lengths to match the two lists that values could be from.
- Annotations have ``oneOfEnumSelectorField`` added to provide hints to validation code which will produce better error messages.

Changed
-------
Expand All @@ -31,6 +32,7 @@ Changed
- Clarified ``Country.code`` is from the ISO 3166-1 list (previously it was unclear which ISO list was meant and used "digit" when it meant "letter").
- Clarified ``Jurisdiction.code`` is from the ISO 3166-1 or ISO 3166-2 list (previously it was unclear which ISO list was meant and used "digit" when it meant "letter").
- Clarified ``SecuritiesListing.stockExchangeJurisdiction`` is from the ISO 3166-1 or ISO 3166-2 list (previously it was unclear which ISO list was meant and used "digit" when it meant "letter").
- Annotations changes from a ``anyOf`` to a ``oneOf``. This is technically correct and also is needed to improve validation messages.


[0.2] - 2019-06-30
Expand Down
3 changes: 2 additions & 1 deletion schema/components.json
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@
"statementPointerTarget",
"motivation"
],
"anyOf": [
"oneOfEnumSelectorField": "motivation",
"oneOf": [
{
"properties": {
"motivation": {
Expand Down
11 changes: 11 additions & 0 deletions tests/bods_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ def build_schemas_for_testing():
)
with open(os.path.join(absolute_path_to_schema_dir, source_file), "w") as fp:
created_json = json.loads(ctjs.get_as_string())
# The items in the annotations objects must not have additionalProperties set
# They factor out common elements of the oneOf bits as allowed in
# https://json-schema.org/understanding-json-schema/reference/combining.html#factoring-schemas
# But additionalProperties is applied to the oneOf schema only, not the full item and that causes issues
if source_file.endswith('entity-statement.json') or source_file.endswith('ownership-or-control-statement.json') or source_file.endswith('person-statement.json'):
for j in range(0, 2):
created_json['properties']['annotations']['items']['oneOf'][j]['additionalProperties'] = True
if source_file.endswith('bods-package.json'):
for i in range(0, 3):
for j in range(0, 2):
created_json['items']['oneOf'][i]['properties']['annotations']['items']['oneOf'][j]['additionalProperties'] = True
# write with correct indentation
fp.write(json.dumps(created_json, ensure_ascii=False, indent=2, separators=(',', ': ')) + '\n')

Expand Down
270 changes: 270 additions & 0 deletions tests/schema/meta-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
{
"id": "http://json-schema.org/draft-04/schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Core schema meta-schema",
"definitions": {
"schemaArray": {
"type": "array",
"minItems": 1,
"items": {
"$ref": "#"
}
},
"positiveInteger": {
"type": "integer",
"minimum": 0
},
"positiveIntegerDefault0": {
"allOf": [
{
"$ref": "#/definitions/positiveInteger"
},
{
"default": 0
}
]
},
"simpleTypes": {
"enum": [
"array",
"boolean",
"integer",
"null",
"number",
"object",
"string"
]
},
"stringArray": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"uniqueItems": true
}
},
"type": "object",
"properties": {
"id": {
"type": "string"
},
"$schema": {
"type": "string"
},
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"default": {},
"multipleOf": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
},
"maximum": {
"type": "number"
},
"exclusiveMaximum": {
"type": "boolean",
"default": false
},
"minimum": {
"type": "number"
},
"exclusiveMinimum": {
"type": "boolean",
"default": false
},
"maxLength": {
"$ref": "#/definitions/positiveInteger"
},
"minLength": {
"$ref": "#/definitions/positiveIntegerDefault0"
},
"pattern": {
"type": "string",
"format": "regex"
},
"additionalItems": {
"anyOf": [
{
"type": "boolean"
},
{
"$ref": "#"
}
],
"default": {}
},
"items": {
"anyOf": [
{
"$ref": "#"
},
{
"$ref": "#/definitions/schemaArray"
}
],
"default": {}
},
"maxItems": {
"$ref": "#/definitions/positiveInteger"
},
"minItems": {
"$ref": "#/definitions/positiveIntegerDefault0"
},
"uniqueItems": {
"type": "boolean",
"default": false
},
"maxProperties": {
"$ref": "#/definitions/positiveInteger"
},
"minProperties": {
"$ref": "#/definitions/positiveIntegerDefault0"
},
"required": {
"$ref": "#/definitions/stringArray"
},
"additionalProperties": {
"anyOf": [
{
"type": "boolean"
},
{
"$ref": "#"
}
],
"default": {}
},
"definitions": {
"type": "object",
"additionalProperties": {
"$ref": "#"
},
"default": {}
},
"properties": {
"type": "object",
"additionalProperties": {
"$ref": "#"
},
"default": {}
},
"patternProperties": {
"type": "object",
"additionalProperties": {
"$ref": "#"
},
"default": {}
},
"dependencies": {
"type": "object",
"additionalProperties": {
"anyOf": [
{
"$ref": "#"
},
{
"$ref": "#/definitions/stringArray"
}
]
}
},
"enum": {
"type": "array",
"minItems": 1,
"uniqueItems": true
},
"type": {
"anyOf": [
{
"$ref": "#/definitions/simpleTypes"
},
{
"type": "array",
"items": {
"$ref": "#/definitions/simpleTypes"
},
"minItems": 1,
"uniqueItems": true
}
]
},
"format": {
"type": "string"
},
"allOf": {
"$ref": "#/definitions/schemaArray"
},
"anyOf": {
"$ref": "#/definitions/schemaArray"
},
"oneOf": {
"$ref": "#/definitions/schemaArray"
},
"not": {
"$ref": "#"
},
"$ref": {
"type": "string"
},
"omitWhenMerged": {
"type": "boolean",
"default": false
},
"deprecated": {
"type": "object",
"additionalProperties": false,
"required": [
"deprecatedVersion",
"description"
],
"properties": {
"deprecatedVersion": {
"type": "string"
},
"description": {
"type": "string"
}
}
},
"codelist": {
"type": "string"
},
"openCodelist": {
"type": "boolean",
"default": false
},
"wholeListMerge": {
"type": "boolean",
"default": false
},
"versionId": {
"type": "boolean",
"default": false
},
"version": {
"type": "string"
},
"propertyOrder": {
"type": "integer"
},
"oneOfEnumSelectorField": {
"type": "string"
}
},
"dependencies": {
"exclusiveMaximum": [
"maximum"
],
"exclusiveMinimum": [
"minimum"
]
},
"default": {},
"additionalProperties": false
}
1 change: 1 addition & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ def test_all_examples_and_data_files_are_used():
files_used.extend(test_valid_statement_json_parametrize_data)
files_used.extend(test_valid_package_json_parametrize_data)
files_used.extend([a[0] for a in test_invalid_statement_json_parametrize_data])
files_used.append('schema/meta-schema.json')
for data in test_invalid_package_json_parametrize_data:
if data[0]:
files_used.append(data[0])
Expand Down
17 changes: 6 additions & 11 deletions tests/test_json.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from bods_validate import absolute_path_to_source_schema_dir
from bods_validate import absolute_path_to_source_schema_dir, this_dir

from warnings import warn
from collections import Counter
Expand All @@ -7,9 +7,10 @@
from jscc.testing.filesystem import walk_json_data, walk_csv_data
from jscc.schema import is_json_schema
from jscc.testing.checks import validate_items_type, validate_letter_case, validate_schema
from jscc.testing.util import http_get

import pytest
import os
import json


def test_empty():
Expand All @@ -32,15 +33,9 @@ def test_invalid_json():
)


schemas = [(path, name, data) for path, name, _, data in walk_json_data() if is_json_schema(data)]
metaschema = http_get("https://standard.open-contracting.org/schema/1__1__4/meta-schema.json").json()

metaschema["properties"]["version"] = {
"type": "string",
}
metaschema["properties"]["propertyOrder"] = {
"type": "integer",
}
schemas = [(path, name, data) for path, name, _, data in walk_json_data() if is_json_schema(data) and not path.endswith('tests/schema/meta-schema.json')]
with open(os.path.join(this_dir, 'schema', 'meta-schema.json')) as fp:
metaschema = json.load(fp)


@pytest.mark.parametrize("path,name,data", schemas)
Expand Down