Hello Максим,
On Mon, May 14, 2012 at 1:19 PM, Максим Коринец <[email protected]> wrote:
> I guess you can only flatten/unflatten a valid appstruct (that has been
> successfully deserialized). Invalid.asdict() returns a flat dictionary with
> error info, but I wouldn't rely on unflattening it with
> SchemaNode.unflatten().
>
> The reason I'm interested in this question is that I needed a similar
> functionality from Invalid.asdict(). I needed error messages translated in
> it, and since they were not, I had to step through the asdict() code and
> write my own version of it. Consider this:
>
> ...
> try:
> appstruct = some_schema.deserialize(cstruct)
> except Invalid, e:
> for path in e.paths():
> ...
>
> Now paths() has all the 'raw' error (fields and messages) data to manipulate
> and construct your own validation error handling mechanism.
I think you're right about unflatten and Invalid.asdict(). Based on
your suggestion,
I try to use the asdict to merge the error msg from asdict() to the
cstruct to show
the errors in-placed as the following:
# cstruct that missing 'name', and 'wrong' location
c ={ 'phones': [{'location': 'home', 'number': '1234'},
{'location': 'office', 'number': '33131'}]
}
try:
user.deserialize(c)
except colander.Invalid as invalid:
for k,msg in invalid.asdict().iteritems():
try:
value = user.get_value(c,k)
except KeyError:
value = None
user.set_value(c, k, "'%s' <-- ERROR %s" % (str(value), msg))
print c
# Result:
{{'phones': [{'location': 'home', 'number': '1234'}, {'location':
u'\'office\' <-- ERROR: "office" is not one of home, work', 'number':
'33131'}],
'name': u"'None' <-- ERROR: Required"}
Regards,
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en.