Skip to content
Snippets Groups Projects

Resolve "Validate data against json schema"

Merged Frank Sauerburger requested to merge 17-validate-data-against-json-schema into master
5 files
+ 303
5
Compare changes
  • Side-by-side
  • Inline
Files
5
import json
import os
from django.contrib.auth.models import User
from django.db.models import Q
from django.urls import reverse
from rest_framework import serializers
import jsonschema
from uhepp_vault.models import PUBLIC_LEVEL, INTERNAL_LEVEL, PUBLIC_LEVEL, \
VISIBILITY_LEVELS, Plot, Collection
from .masks import filter_collections
@@ -70,6 +73,24 @@ class PlotSerializer(serializers.HyperlinkedModelSerializer):
'collection-detail',
queryset=Collection.objects
)
def validate_uhepp(self, value):
"""Check if the JSON data conforms to the uhepp schema"""
try:
schema_path = os.path.join(
os.path.dirname(__file__),
"uhepp.schema.json"
)
with open(schema_path) as schema_file:
schema = json.load(schema_file)
jsonschema.validate(value, schema=schema)
except Exception as e:
raise serializers.ValidationError("JSON schema validation error: %s" % e)
return value
class Meta:
model = Plot
fields = ["uuid", "collection", "url", "uhepp"]
Loading