Skip to content

Commit

Permalink
Migrating From RefResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
vysakh-menon-aot committed Aug 11, 2023
1 parent c3f6838 commit 042eaaa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
flask
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 042eaaa

Please sign in to comment.