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

469 validate example #478

Merged
merged 7 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
20 changes: 11 additions & 9 deletions docs/examples/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"updated": "2018-12-10T15:53:00Z",
"title": "M75 Junctions 4 to 5 upgrade smart motorway",
"description": "Upgrading the 5km stretch of the M75 near Birmingham Airport, between junction 4 near Patcham and junction 5 at Windlesham, to an all-lane running smart motorway.",
"status": "completed",
"status": "maintenance",
"period": {
"startDate": "2016-01-01T00:00:00Z",
"endDate": "2018-12-10T00:00:00Z",
Expand Down Expand Up @@ -198,7 +198,9 @@
"finance": [
{
"id": "1",
"assetClass": "debt",
"assetClass": [
"debt"
],
"type": "loan",
"concessional": true,
"value": {
Expand Down Expand Up @@ -544,8 +546,7 @@
"period": {
"startDate": "2018-01-07T00:00:00Z",
"endDate": "2018-01-07T00:00:00Z"
},
"value": {}
}
},
{
"id": "2",
Expand Down Expand Up @@ -592,8 +593,7 @@
"period": {
"startDate": "2018-01-07T00:00:00Z",
"endDate": "2018-01-07T00:00:00Z"
},
"value": {}
}
},
{
"id": "2",
Expand Down Expand Up @@ -794,7 +794,7 @@
],
"social": {
"laborBudget": {
"amount": "150000",
"amount": 150000,
"currency": "USD"
},
"laborObligations": {
Expand Down Expand Up @@ -1138,7 +1138,7 @@
"countryName": "United Kingdom"
},
"date": "2024-01-15T00:00:00Z",
"numberOfParticipants": "25",
"numberOfParticipants": 25,
"publicOffice": {
"person": {
"name": "Brett Gliddon"
Expand Down Expand Up @@ -1199,7 +1199,9 @@
"inProtectedArea": true,
"climateMeasures": [
{
"type": "regenerativeDesign",
"type": [
"regenerativeDesign"
],
"description": "Regenerative design measures for the project include biomimicry to imitate nature."
}
]
Expand Down
6 changes: 6 additions & 0 deletions docs/reference/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [0.9.5] - YYYY-MM-DD

### Documentation

* [#478](https://github.com/open-contracting/infrastructure/pull/478) - Fix validation errors in `example.json`.

## [0.9.4] - 2023-12-12

### Documentation
Expand Down
25 changes: 13 additions & 12 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ def write_yaml_file(filename, data):
yaml.dump(data, f, Dumper=Dumper, indent=4, width=1000, sort_keys=False)


# From standard-maintenance-scripts/tests/test_readme.py
def set_additional_properties(data, additional_properties):
if isinstance(data, list):
for item in data:
set_additional_properties(item, additional_properties)
elif isinstance(data, dict):
if "properties" in data:
data["additionalProperties"] = additional_properties
for value in data.values():
set_additional_properties(value, additional_properties)


def traverse(schema_action=None, object_action=None):
"""
Common logic for walking through the schema.
Expand Down Expand Up @@ -810,17 +822,6 @@ def _compare(actual, infra_list, ocds_list, prefix, suffix):
@click.option("-l", "--link-fields", is_flag=True, help="Link field names to jsonschema directives")
def lint(filename, additional_properties, link_fields):

# From standard-maintenance-scripts/tests/test_readme.py
def _set_additional_properties(data, additional_properties):
if isinstance(data, list):
for item in data:
_set_additional_properties(item, additional_properties)
elif isinstance(data, dict):
if "properties" in data:
data["additionalProperties"] = additional_properties
for value in data.values():
_set_additional_properties(value, additional_properties)

def _get_fields(schema, parents=()):
"""
Generate field names (as tuples) in the JSON Schema.
Expand Down Expand Up @@ -887,7 +888,7 @@ def _link(name, fields, text):
with (basedir / 'schema' / 'project-level' / 'project-schema.json').open() as f:
schema = json.load(f)

_set_additional_properties(schema, additional_properties)
set_additional_properties(schema, additional_properties)

fields = set(_get_fields(schema))
for name, definition in schema['definitions'].items():
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ sphinxcontrib-opendataservices==0.5.0
sphinx-design

# Build
jscc
jsonref
jsonschema
mdformat
Expand Down
45 changes: 45 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import json
import os
import warnings
from glob import glob
from pathlib import Path

import pytest
from jsonschema import FormatChecker
from jsonschema.validators import Draft4Validator
from jscc.testing.checks import validate_schema

from manage import set_additional_properties

cwd = os.getcwd()
basedir = Path(__file__).resolve().parents[1]


def formatwarning(message, category, filename, lineno, line=None):
return str(message).replace(cwd + os.sep, '')


warnings.formatwarning = formatwarning
pytestmark = pytest.mark.filterwarnings('always')


# See test_example_valid in standard-maintenance-scripts/tests/test_readme.py
def test_examples_valid():
with (basedir / 'schema' / 'project-level' / 'project-package-schema.json').open() as f:
schema = json.load(f)

set_additional_properties(schema, False)

validator = Draft4Validator(schema, format_checker=FormatChecker())

errors = 0
for path in (basedir / 'docs' / 'examples').glob('**/*.json'):
if path.name == 'blank.json':
continue

with open(path) as f:
data = json.load(f)

errors += validate_schema(path, data, validator)

assert not errors, 'One or more JSON files are invalid. See warnings below.'
Loading