Validation of JSON data can be get very complex if we need to do anything more than trimming and very basic assertions as to the type of the information coming in.
The reason behind the complexity in validation is that JSON can be also a very complex data type. I know this very well because I wrote a JSON (or Python Dictionary) validation engine in Python (see: http://notario.cafepais.com/docs/index.html)
And ultimately, that is the type of validation that should be implemented: some engine that can accept complex data structures with a very flexible engine that has great error reporting.
Great error reporting is a must when dealing with these type of data structures. Imagine some deeply nested value that just isn't expected, how can you report correctly where is that value in the tree?
The library I wrote kept track of everything and reported nicely about it.
Take this example data:
{'foo':
{
'a':{'a':10},
'b':{'a':20},
'c':{'a':20}
}
}
If the validation was expecting a "90" as value for any key in 'foo', this is how that engine would report the actual error:
Invalid: -> foo -> a -> a -> 10 did not match 90
The is the reason why error reporting is important, and also demonstrates how this can easily be very tedious to attempt to validate without a proper engine.