Skip to content

Commit

Permalink
test handling of $schema
Browse files Browse the repository at this point in the history
  • Loading branch information
sir-sigurd committed Nov 4, 2020
1 parent f3feaef commit 4153868
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
20 changes: 14 additions & 6 deletions api/python/quilt3/workflows/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import functools
import json
import urllib.parse

import botocore.exceptions
import jsonschema
Expand All @@ -18,9 +19,9 @@ def _get_conf_validator():
return jsonschema.Draft7Validator(schema).validate


_SUPPORTED_SCHEMA_VERSIONS = (
'http://json-schema.org/draft-07/schema#',
)
SUPPORTED_META_SCHEMAS = {
'http://json-schema.org/draft-07/schema#': jsonschema.Draft7Validator,
}


def _schema_load_object_hook(o):
Expand Down Expand Up @@ -93,10 +94,17 @@ def validate(*, registry: PackageRegistry, workflow, meta, message):

schema_data, schema_pk_to_store = get_bytes_and_effective_pk(schema_pk)
schema = _load_schema_json(schema_data.decode())
if isinstance(schema, dict) and schema.get('$schema') not in (None, *_SUPPORTED_SCHEMA_VERSIONS):
raise util.QuiltException(f"Unsupported schema version: {schema.get('$schema')}.")

validator_cls = jsonschema.Draft7Validator
if isinstance(schema, dict) and '$schema' in schema:
meta_schema = schema['$schema']
if not isinstance(meta_schema, str):
raise util.QuiltException('$schema must be a string.')
validator_cls = SUPPORTED_META_SCHEMAS.get(meta_schema)
if validator_cls is None:
raise util.QuiltException(f"Unsupported meta-schema: {meta_schema}.")
try:
jsonschema.validate(meta, schema, cls=jsonschema.Draft7Validator)
jsonschema.validate(meta, schema, cls=validator_cls)
except jsonschema.ValidationError as e:
raise util.QuiltException(f"Metadata failed validation: {e.message}.")
result['schemas'] = {metadata_schema_id: str(schema_pk_to_store)}
Expand Down
31 changes: 31 additions & 0 deletions api/python/tests/integration/test_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,34 @@ def test_invalid_url(self):
'''))
with pytest.raises(QuiltException, match=fr"Couldn't parse URL '{url}'."):
self._validate(workflow='w1')

def test_unsupported_meta_schema(self):
for meta_schema in (
'http://json-schema.org/draft-07/schema',
'http://json-schema.org/draft-06/schema#',
):
with self.subTest(meta_schema=meta_schema):
set_local_conf_data(get_v1_conf_data('''
workflows:
w1:
name: Name
metadata_schema: schema-id
schemas:
schema-id:
url: %s
''' % create_local_tmp_schema(f'{{"$schema": "{meta_schema}"}}')))
with pytest.raises(QuiltException, match=fr"Unsupported meta-schema: {meta_schema}."):
self._validate(workflow='w1')

def test_invalid_meta_schema(self):
set_local_conf_data(get_v1_conf_data('''
workflows:
w1:
name: Name
metadata_schema: schema-id
schemas:
schema-id:
url: %s
''' % create_local_tmp_schema('{"$schema": 42}')))
with pytest.raises(QuiltException, match=r'\$schema must be a string.'):
self._validate(workflow='w1')

0 comments on commit 4153868

Please sign in to comment.