Hi all,
I'm trying to validate JSON data with Colander, and have a little
trouble finding the right way to support arbitrary key names.
Suppose I have phone numbers like in the example schema:
http://docs.pylonsproject.org/projects/colander/en/latest/basics.html#defining-a-colander-schema
{
'phones':[{'location':'home', 'number':'555-1212'},
{'location':'work', 'number':'555-8989'},],
}
How would I best go about validating custom fields, like for example:
{
'phones':[{'location':'home', 'number':'555-1212', 'type':'landline'},
{'provider':'telekom', 'number':'555-8989'},],
}
I want the user to be able to add arbitrary fields to the phone numbers.
At the moment, I solved it like this (using a sequence of "fieldname",
"value" pairs):
}
'phones':[{'number':'555-1212', fields=[{'fieldname':'provider',
'value':'telekom'}, {'fieldname':'type', 'value':'landline'],}],
}
Which works, but I think it would be much nicer to be able to just use
{"provider":"telekom"}.
Is this possible, and how would you validate data like this with
Colander? I could just choose to not validate the fields, but I would
prefer if I could still make sure that the value for a dynamic field
is a string etc.
Thanks in advance,
Jesaja Everling