Skip to content

Commit

Permalink
Validate metadata model JSON; closes #24
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-saeon committed Jun 27, 2018
1 parent 487e5f0 commit 538df7c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ckanext/metadata/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def metadata_model_create_schema():
'metadata_schema_id': [v.not_empty, unicode, v.metadata_schema_exists],
'organization_id': [v.not_missing, unicode, v.group_exists('organization')],
'infrastructure_id': [v.not_missing, unicode, v.group_exists('infrastructure')],
'model_json': [v.not_missing, unicode, v.json_dict_validator],
'model_json': [v.not_missing, unicode, v.json_dict_validator, v.json_schema_validator],
'state': [ignore_not_sysadmin, ignore_missing],

# post-validation
Expand Down
19 changes: 19 additions & 0 deletions ckanext/metadata/logic/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import uuid
import re
import urlparse
import jsonschema

import ckan.plugins.toolkit as tk
from ckan.common import _
Expand Down Expand Up @@ -133,6 +134,24 @@ def json_dict_validator(value):
return value


def json_schema_validator(value):
"""
Checks that the value represents a valid JSON schema.
"""
if value:
try:
schema = json.loads(value)
jsonschema.validate({}, schema)
except ValueError, e:
raise tk.Invalid(_("JSON decode error: %s") % e.message)
except AttributeError, e:
raise tk.Invalid(_("Invalid JSON object type: %s") % e.message)
except jsonschema.SchemaError, e:
raise tk.Invalid(_("Invalid JSON schema: %s") % e.message)

return value


def xsd_validator(value):
"""
TODO
Expand Down
14 changes: 14 additions & 0 deletions ckanext/metadata/tests/test_metadata_model_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,12 @@ def test_create_invalid_not_json_dict(self):
model_json='[1,2,3]')
assert_error(result, 'model_json', 'Expecting a JSON dictionary')

def test_create_invalid_not_json_schema(self):
result, obj = self._test_action('create', 'metadata_model',
exception_class=tk.ValidationError,
model_json='{"type": "foo"}')
assert_error(result, 'model_json', 'Invalid JSON schema')

def test_create_invalid_missing_params(self):
result, obj = self._test_action('create', 'metadata_model',
exception_class=tk.ValidationError)
Expand Down Expand Up @@ -710,6 +716,14 @@ def test_update_invalid_not_json_dict(self):
model_json='[1,2,3]')
assert_error(result, 'model_json', 'Expecting a JSON dictionary')

def test_update_invalid_not_json_schema(self):
metadata_model = ckanext_factories.MetadataModel()
result, obj = self._test_action('update', 'metadata_model',
exception_class=tk.ValidationError,
id=metadata_model['id'],
model_json='{"type": "foo"}')
assert_error(result, 'model_json', 'Invalid JSON schema')

def test_update_invalid_bad_references(self):
metadata_model = ckanext_factories.MetadataModel()
result, obj = self._test_action('update', 'metadata_model',
Expand Down

0 comments on commit 538df7c

Please sign in to comment.