zenwalker
1/6/2017 - 8:20 PM

jsonschemavalidator.py

from django.utils.deconstruct import deconstructible
from django.core.exceptions import ValidationError

from jsonschema.exceptions import ValidationError as SchemaValidationError
from jsonschema import validate as validate_schema


@deconstructible
class JSONSchemaValidator:
    def __init__(self, schema):
        self.schema = schema

    def __call__(self, value):
        try:
            validate_schema(value, self.schema)
        except SchemaValidationError as e:
            raise ValidationError(e.message)