# Path: src/rest_toolkit/compat.py
# def add_metaclass(metaclass):
#     """Class decorator for creating a class with a metaclass."""
#     def wrapper(cls):
#         orig_vars = cls.__dict__.copy()
#         orig_vars.pop('__dict__', None)
#         orig_vars.pop('__weakref__', None)
#         slots = orig_vars.get('__slots__')
#         if slots is not None:
#             if isinstance(slots, str):
#                 slots = [slots]
#             for slots_var in slots:
#                 orig_vars.pop(slots_var)
#         return metaclass(cls.__name__, cls.__bases__, orig_vars)
#     return wrapper
# 
# Path: src/rest_toolkit/utils.py
# def add_missing(data, defaults):
#     for (key, value) in defaults.items():
#         if key not in data:
#             data[key] = value
#     return data


import abc
import jsonschema
from pyramid.httpexceptions import HTTPBadRequest
from ..compat import add_metaclass
from ..utils import add_missing
from __future__ import absolute_import


class JSONValidationError(HTTPBadRequest):
    """HTTP response for JSON validation errors.
    """

def validate(data, schema):
    """Validate data against a JSON schema.

    This is a helper function used by :py:class:`JsonSchemaValidationMixin`
    to validate data against a JSON schema. If validation fails this function
    will raise a :py:class:`pyramid.httpexceptions.HTTPBadRequest` exception
    describing the validation error.

    :raises pyramid.httpexceptions.HTTPBadRequest: if validation fails this
        exception is raised to abort any further processing.
    """
    try:
        jsonschema.validate(data, schema,
            format_checker=jsonschema.draft4_format_checker)
    except jsonschema.ValidationError as e:
        error = {
            '.'.join(str(p) for p in e.path): e.message
        }
        response = JSONValidationError(json=error)
        response.validation_error = e
        raise response



# Path: src/rest_toolkit/compat.py
# def add_metaclass(metaclass):
#     """Class decorator for creating a class with a metaclass."""
#     def wrapper(cls):
#         orig_vars = cls.__dict__.copy()
#         orig_vars.pop('__dict__', None)
#         orig_vars.pop('__weakref__', None)
#         slots = orig_vars.get('__slots__')
#         if slots is not None:
#             if isinstance(slots, str):
#                 slots = [slots]
#             for slots_var in slots:
#                 orig_vars.pop(slots_var)
#         return metaclass(cls.__name__, cls.__bases__, orig_vars)
#     return wrapper
# 
# Path: src/rest_toolkit/utils.py
# def add_missing(data, defaults):
#     for (key, value) in defaults.items():
#         if key not in data:
#             data[key] = value
#     return data


# Path: src/rest_toolkit/ext/jsonschema.py
import abc
import jsonschema
from pyramid.httpexceptions import HTTPBadRequest
from ..compat import add_metaclass
from ..utils import add_missing

from __future__ import absolute_import


class JSONValidationError(HTTPBadRequest):
    """HTTP response for JSON validation errors.
    """

def validate(data, schema):
    """Validate data against a JSON schema.

    This is a helper function used by :py:class:`JsonSchemaValidationMixin`
    to validate data against a JSON schema. If validation fails this function
    will raise a :py:class:`pyramid.httpexceptions.HTTPBadRequest` exception
    describing the validation error.

    :raises pyramid.httpexceptions.HTTPBadRequest: if validation fails this
        exception is raised to abort any further processing.
    """
    try:
        jsonschema.validate(data, schema,
            format_checker=jsonschema.draft4_format_checker)
    except jsonschema.ValidationError as e:
        error = {
            '.'.join(str(p) for p in e.path): e.message
        }
        response = JSONValidationError(json=error)
        response.validation_error = e
        raise response



@add_metaclass(abc.ABCMeta)