Commit 5eaaafed authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

erp5_json_form: Empty data doesn't mean data is valid

* Case of required properties
parent 5515bb22
...@@ -73,8 +73,6 @@ class JSONForm(JSONType, TextDocument): ...@@ -73,8 +73,6 @@ class JSONForm(JSONType, TextDocument):
""" """
Validate contained JSON with the Schema defined in the Portal Type. Validate contained JSON with the Schema defined in the Portal Type.
""" """
if not json_data:
return True
defined_schema = json.loads(self.getTextContent() or "") defined_schema = json.loads(self.getTextContent() or "")
try: try:
jsonschema.validate(json_data, defined_schema, format_checker=jsonschema.FormatChecker()) jsonschema.validate(json_data, defined_schema, format_checker=jsonschema.FormatChecker())
......
...@@ -214,3 +214,32 @@ return json.dumps({ ...@@ -214,3 +214,32 @@ return json.dumps({
except ValueError as e: except ValueError as e:
self.assertEqual(error, json.loads(str(e))) self.assertEqual(error, json.loads(str(e)))
def test_empty_data_with_required_property(self):
"""
"""
schema = """{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "my-schema.json",
"properties":{
"title": {
"type": "string"
}
},
"required": ["title"]
}"""
data = {}
method = "test_ERP5Site_processSimpleStriingAsJSON"
after_method = self.createBasicScriptreturnJSONWithTimestamp()
self.fixJSONForm(method, schema, after_method)
self.tic()
self.assertRaises(ValueError, getattr(self.portal, method), data, list_error=True)
error = {
"my-schema.json": [[
'Validation Error', u"u'title' is a required property"
]]
}
try:
getattr(self.portal, method)(data, list_error=True)
raise ValueError("No error raised during processing")
except ValueError, e:
self.assertEqual(error, json.loads(str(e)))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment