fix of schema

This commit is contained in:
Cayo Puigdefabregas 2024-01-12 21:32:26 +01:00
parent bf68d7e741
commit 336a2148f2

View file

@ -1,5 +1,6 @@
import csv import csv
import json import json
import copy
import pandas as pd import pandas as pd
from django import forms from django import forms
@ -62,7 +63,7 @@ class ImportForm(forms.Form):
self._schema = schema.first() self._schema = schema.first()
try: try:
self.json_schema = json.loads(self._schema.data) self.json_schema = json.loads(self._schema.data)
props = [x for x in self.json_schema["allOf"] if 'properties' in x] props = [x for x in self.json_schema["allOf"] if 'properties' in x.keys()]
prop = props[0]['properties'] prop = props[0]['properties']
self.properties = prop['credentialSubject']['properties'] self.properties = prop['credentialSubject']['properties']
except Exception: except Exception:
@ -71,7 +72,10 @@ class ImportForm(forms.Form):
if not self.properties: if not self.properties:
raise ValidationError("Schema is not valid!") raise ValidationError("Schema is not valid!")
# TODO we need filter "$ref" of schema for can validate a csv
self.json_schema_filtered = copy.copy(self.json_schema)
allOf = [x for x in self.json_schema["allOf"] if '$ref' not in x.keys()]
self.json_schema_filtered["allOf"] = allOf
return data return data
def clean_file_import(self): def clean_file_import(self):
@ -112,9 +116,9 @@ class ImportForm(forms.Form):
return return
def validate_jsonld(self, line, row): def validate_jsonld(self, line, row):
import pdb; pdb.set_trace()
try: try:
check = credtools.validate_json(row, self.json_schema) check = credtools.validate_json(row, self.json_schema_filtered)
if check is not True:
raise ValidationError("Not valid row") raise ValidationError("Not valid row")
except Exception as e: except Exception as e:
msg = "line {}: {}".format(line+1, e) msg = "line {}: {}".format(line+1, e)