Better schema validation and errors

This commit is contained in:
Anders Ingemann 2013-06-23 23:37:21 +02:00
parent b701bd028b
commit 79a699a360
6 changed files with 49 additions and 16 deletions

25
base/manifest-schema.json Normal file
View file

@ -0,0 +1,25 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Generic manifest",
"type": "object",
"properties": {
"provider": {
"type": "string"
},
"plugins": {
"type": "object",
"patternProperties": {
"^\\w+$": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean"
}
},
"required": ["enabled"]
}
},
"additionalProperties": false
}
}
}

View file

@ -23,17 +23,19 @@ class Manifest(object):
def __init__(self, path):
self.path = path
def validate(self, data, schema_path=None):
if schema_path is not None:
from json_schema_validator.validator import Validator
from json_schema_validator.schema import Schema
from json_schema_validator.errors import ValidationError
schema = Schema(load_json(schema_path))
def validate(self, data):
from os import path
schema_path = path.normpath(path.join(path.dirname(__file__), 'manifest-schema.json'))
self.schema_validate(data, schema_path)
def schema_validate(self, data, schema_path):
import jsonschema
schema = load_json(schema_path)
try:
Validator.validate(schema, data)
except ValidationError as e:
jsonschema.validate(data, schema)
except jsonschema.ValidationError as e:
from common.exceptions import ManifestError
raise ManifestError(e.message, self)
raise ManifestError(e.message, self, e.path)
def parse(self, data):
self.provider = data['provider']

View file

@ -2,11 +2,15 @@ __all__ = ['ManifestError']
class ManifestError(Exception):
def __init__(self, message, manifest):
def __init__(self, message, manifest, json_path=None):
self.message = message
self.manifest = manifest
self.json_path = json_path
def __str__(self):
return "Error in manifest {0}: {1}".format(self.manifest.path, self.message)
if self.json_path is not None:
path = '.'.join(self.json_path)
return "{2}\n\tFile: {0}\n\tJSON path: {1}".format(self.manifest.path, path, self.message)
return "{0}: {1}".format(self.manifest.path, self.message)
class TaskListError(Exception):

View file

@ -29,7 +29,7 @@
"enabled": true
},
"build_metadata": {
"enabled": true,
"enabled": false,
"path" : "/root/build-metadata-{ami_name}"
}
}

View file

@ -1,4 +1,5 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "EC2 manifest",
"type": "object",
"properties": {

View file

@ -5,7 +5,8 @@ class Manifest(base.Manifest):
def validate(self, data):
from os import path
schema_path = path.normpath(path.join(path.dirname(__file__), 'manifest-schema.json'))
super(Manifest, self).validate(data, schema_path)
self.schema_validate(data, schema_path)
super(Manifest, self).validate(data)
def parse(self, data):
super(Manifest, self).parse(data)