This project has moved and is read-only. For the latest updates, please go here.

Schema Error Messages

Feb 15, 2012 at 10:39 PM

Thank you for all your efforts as JSON.Net is an excellent piece of coding which is really useful.

The error messages provided by the Schema are good for log files and programmers however it is not so good to return back to the user without further investigation of the schema.

Looking through the code you do not implement the description property for the JsonSchemaModel.  I was thinking it could be a good solution to include this property on the JsonSchemaModel and when an error is discovered for a property that instead of a List of schema errors being returned that the list contains both the error message (as currently implement) with the addition of the description.  This way the description could be returned to the user without having rather than the error message.

For example a schema such as:

      {  "id": "System.Version",  "type": "object",  "additionalProperties": false,  "properties": {    "Name": {   "description" : "The name is required",   "required": true,      "type": [        "string",        "null"      ]    },    "Email": {    "description" : "The email is required in the correct format",  "required": true,       "pattern" : "[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}",      "type": [        "string",        "null"      ]    }  } 

In the current version running the code:

string jsonToTest = @"{  ""Name"": ""pete"",  ""Email"": ""pete""}";       
JObject obj = JObject.Parse(jsonToTest);       
IList<string> errors = new List<string>();       
bool isValid = obj.IsValid(schema, out errors);

String 'pete' does not match regex pattern '[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}'. Line 1, position 35.

Whilst this could be changed to:

IList<string, string> errors = new List<string, string>();        
bool isValid = obj.IsValid(schema, out errors);

String 'pete' does not match regex pattern '[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}'. Line 1, position 35.
The email is required in the correct format

Other alternatives would be to:

a) return the failing schema object so any properties could be interogated

b) not using the description property but looking for an error message property/array and if found returning that as well/instead

I am not keen on forking the code to implement the above as I would like to keep pace with other advancements on the code base.  If there are no other options then I may look into applying extension methods to extract the error messages from the schema at the point of error.

If anyone implements any other solutions I would love to hear them.