Skip to content

Commit

Permalink
17343 update jsonschema (bcgov#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
vysakh-menon-aot authored Aug 14, 2023
1 parent 7ea3441 commit 46fe420
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
flask
jsonschema[format]==4.17.3
jsonschema[format]
referencing
requests
strict-rfc3339
3 changes: 2 additions & 1 deletion requirements/prod.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
flask
jsonschema
referencing
requests
strict-rfc3339
strict-rfc3339
4 changes: 2 additions & 2 deletions src/registry_schemas/schemas/change_of_directors.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@
"$ref": "https://bcrs.gov.bc.ca/.well_known/schemas/directors#/definitions/director"
},
{
"$ref": "#definitions/actions"
"$ref": "#/definitions/actions"
}
]
}
}
}
}
}
}
}
41 changes: 23 additions & 18 deletions src/registry_schemas/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
from os import listdir, path
from typing import Tuple

from jsonschema import Draft7Validator, RefResolver, SchemaError
from jsonschema import Draft7Validator, SchemaError
from referencing import Registry, Resource
from referencing.jsonschema import DRAFT7


BASE_URI = 'https://bcrs.gov.bc.ca/.well_known/schemas'
Expand Down Expand Up @@ -83,26 +85,29 @@ def validate(json_data: json,
if not schema_store:
schema_store = get_schema_store(validate_schema, schema_search_path)

schema = schema_store.get(f'{BASE_URI}/{schema_id}')
schema_uri = f'{BASE_URI}/{schema_id}'
schema = schema_store.get(schema_uri)
if validate_schema:
Draft7Validator.check_schema(schema)

schema_file_path = path.join(schema_search_path, schema_id)
resolver = RefResolver(f'file://{schema_file_path}.json', schema, schema_store)

if Draft7Validator(schema,
format_checker=Draft7Validator.FORMAT_CHECKER,
resolver=resolver
) \
.is_valid(json_data):
return True, None

errors = Draft7Validator(schema,
format_checker=Draft7Validator.FORMAT_CHECKER,
resolver=resolver
) \
.iter_errors(json_data)
return False, errors
def retrieve_resource(uri):
contents = schema_store.get(uri)
return Resource.from_contents(contents)

registry = Registry(retrieve=retrieve_resource).with_resource(
schema_uri,
DRAFT7.create_resource(schema)
)

validator = Draft7Validator(
{'$ref': schema_uri},
registry=registry,
format_checker=Draft7Validator.FORMAT_CHECKER
)
if not validator.is_valid(json_data):
return False, validator.iter_errors(json_data)

return True, None

except SchemaError as error:
# handle schema error
Expand Down

0 comments on commit 46fe420

Please sign in to comment.