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): def __init__(self, path):
self.path = path self.path = path
def validate(self, data, schema_path=None): def validate(self, data):
if schema_path is not None: from os import path
from json_schema_validator.validator import Validator schema_path = path.normpath(path.join(path.dirname(__file__), 'manifest-schema.json'))
from json_schema_validator.schema import Schema self.schema_validate(data, schema_path)
from json_schema_validator.errors import ValidationError
schema = Schema(load_json(schema_path)) def schema_validate(self, data, schema_path):
try: import jsonschema
Validator.validate(schema, data) schema = load_json(schema_path)
except ValidationError as e: try:
from common.exceptions import ManifestError jsonschema.validate(data, schema)
raise ManifestError(e.message, self) except jsonschema.ValidationError as e:
from common.exceptions import ManifestError
raise ManifestError(e.message, self, e.path)
def parse(self, data): def parse(self, data):
self.provider = data['provider'] self.provider = data['provider']

View file

@ -2,11 +2,15 @@ __all__ = ['ManifestError']
class ManifestError(Exception): class ManifestError(Exception):
def __init__(self, message, manifest): def __init__(self, message, manifest, json_path=None):
self.message = message self.message = message
self.manifest = manifest self.manifest = manifest
self.json_path = json_path
def __str__(self): 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): class TaskListError(Exception):

View file

@ -6,7 +6,7 @@
"secret-key": null "secret-key": null
}, },
"bootstrapdir" : "/target", "bootstrapdir": "/target",
"image": { "image": {
"name" : "debian-{release}-{architecture}-{virtualization}-{year}{month}{day}", "name" : "debian-{release}-{architecture}-{virtualization}-{year}{month}{day}",
"description": "Debian {release} {architecture} AMI ({virtualization})" "description": "Debian {release} {architecture} AMI ({virtualization})"
@ -29,7 +29,7 @@
"enabled": true "enabled": true
}, },
"build_metadata": { "build_metadata": {
"enabled": true, "enabled": false,
"path" : "/root/build-metadata-{ami_name}" "path" : "/root/build-metadata-{ami_name}"
} }
} }

View file

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

View file

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