Repository: hydroshare/hydroshare

Issue:
Improving the error message when model instance metadata schema is invalid
When an uploaded JSON schema is invalid, the error message shown can be improved and be more specific. I present two examples here:

1- If a comma is missing after one of the schema fields the following error is shown:

"Error! metadata failed to update [{"schema_name":Not a valid JSON string"}]"

Is it possible to tell the user which line is problematic? If not, can HydroShare use a more informative error message (and add a hyperlink to Model Instance Metadata Schema help page, once it is created)?:

"Error! metadata failed to update [{"mi_json_schema_file":Not a valid JSON string"}]. Please follow the instructions provided by **Model Instance Metadata Schema help page** when creating a JSON schema” 

2- I intentionally put a few errors in a schema and got a very long error message which can potentially be confusing (see the screenshot below). In addition to a link to Model Instance Metadata Schema help page (as also stated above), can HydroShare find a way to make this error easier to understand by the user? 

![image](https://user-images.githubusercontent.com/33031286/121948591-695b3080-cd25-11eb-887b-e051f52e2fd5.png)



Discussion:
adding @horsburgh as this pertains to the Resource Model Category.

Resolving patch:
diff --git a/hs_file_types/forms.py b/hs_file_types/forms.py
--- a/hs_file_types/forms.py
+++ b/hs_file_types/forms.py
@@ -172,7 +172,7 @@ def _validate_json_schema(self, schema_string, field_name):
         try:
             json_schema = json.loads(schema_string)
         except ValueError:
-            self.add_error(field_name, "Not a valid JSON string")
+            self.add_error(field_name, "Schema is not valid JSON")
             return json_schema
 
         if json_schema:
@@ -192,12 +192,19 @@ def _validate_json_schema(self, schema_string, field_name):
                                   "draft-04"
                     self.add_error(field_name, err_message)
 
+            if 'properties' not in json_schema:
+                is_schema_valid = False
+                self.add_error(field_name,
+                               "Not a valid metadata schema. Attribute 'properties' "
+                               "is missing")
+
             if is_schema_valid:
                 try:
                     jsonschema.Draft4Validator.check_schema(json_schema)
                 except jsonschema.SchemaError as ex:
                     is_schema_valid = False
-                    self.add_error(field_name, "Not a valid JSON schema. Error:{}".format(str(ex)))
+                    schema_err_msg = "{}. Schema invalid field path:{}".format(ex.message, str(list(ex.path)))
+                    self.add_error(field_name, "Not a valid JSON schema. Error:{}".format(schema_err_msg))
 
         if is_schema_valid:
             # custom validation - hydroshare requirements
diff --git a/hs_file_types/views.py b/hs_file_types/views.py
--- a/hs_file_types/views.py
+++ b/hs_file_types/views.py
@@ -1080,7 +1080,8 @@ def update_model_instance_metadata_json(request, file_type_id, **kwargs):
         metadata_json_schema = logical_file.metadata_schema_json
         jsonschema.Draft4Validator(metadata_json_schema).validate(metadata_json)
     except jsonschema.ValidationError as ex:
-        msg = "JSON metadata is not valid as per the associated metadata schema {}".format(str(ex))
+        schema_err_msg = "{}. Schema invalid field path:{}".format(ex.message, str(list(ex.path)))
+        msg = "JSON metadata is not valid as per the associated metadata schema. Error: {}".format(schema_err_msg)
         error_response = {"status": "error", "message": msg}
         return JsonResponse(error_response, status=status.HTTP_400_BAD_REQUEST)
     # save json data


Test patch: