mercredi 17 septembre 2014

Python: jsonschema package to validate schema and custom error messages


Vote count:

0




Given this schema:



{
"type": "object",
"properties": {
"account": {
"type": "object",
"required": ["id"],
"properties": {
"id": {"type": "number"}
}
},
"name": {"type": "string"},
"trigger": {
"type": "object",
"required": ["type"],
"properties": {
"type": {"type": "string"}
}
},
"content": {
"type": "object",
"properties": {
"emails": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"required": ["fromEmail","subject"],
"properties": {
"fromEmail": {"type": "string", "format": "email"},
"subject": {"type": "string"}
}
}
}
}
}
}
}


I am trying to use jsonschema.validate to validate a POSTed JSON object to check for validity, but I am having some issues trying to come up with better human readable messages from the returned errors.


Here is how I am validating:



from jsonschema import Draft4Validator

v = Draft4Validator(self.schema)
errors = sorted(v.iter_errors(autoresponder_workflow), key=lambda e: e.path)

if errors:
print(', '.join(
'%s %s %s' % (error.path.popleft(), error.path.pop(), error.message) for error in errors
))


The error message looks like:


content emails [] is too short, trigger type None is not of type u'string'


Im trying to create an error message that looks a little more like Please add at least one email to your workflow," "Please ensure that all of your emails contain subject lines,", etc



asked 48 secs ago







Python: jsonschema package to validate schema and custom error messages

Aucun commentaire:

Enregistrer un commentaire