Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Colander: unflatten invalid.asdict() error

Received: by 10.14.95.199 with SMTP id p47mr5912199eef.3.1336539277968;
        Tue, 08 May 2012 21:54:37 -0700 (PDT)
X-BeenThere: pylons-discuss@googlegroups.com
Received: by 10.14.95.134 with SMTP id p6ls46126eef.1.gmail; Tue, 08 May 2012
 21:54:34 -0700 (PDT)
Received: by 10.213.150.67 with SMTP id x3mr350866ebv.2.1336539274390;
        Tue, 08 May 2012 21:54:34 -0700 (PDT)
Received: by 10.213.150.67 with SMTP id x3mr350864ebv.2.1336539274314;
        Tue, 08 May 2012 21:54:34 -0700 (PDT)
Return-Path: <xrotw...@googlemail.com>
Received: from mail-ey0-f180.google.com (mail-ey0-f180.google.com [209.85.215.180])
        by gmr-mx.google.com with ESMTPS id t52si1441576eeb.0.2012.05.08.21.54.34
        (version=TLSv1/SSLv3 cipher=OTHER);
        Tue, 08 May 2012 21:54:34 -0700 (PDT)
Received-SPF: pass (google.com: domain of xrotw...@googlemail.com designates 209.85.215.180 as permitted sender) client-ip=209.85.215.180;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of xrotw...@googlemail.com designates 209.85.215.180 as permitted sender) smtp.mail=xrotw...@googlemail.com; dkim=pass header...@googlemail.com
Received: by eaal12 with SMTP id l12so1764860eaa.25
        for <pylons-discuss@googlegroups.com>; Tue, 08 May 2012 21:54:34 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=googlemail.com; s=20120113;
        h=mime-version:in-reply-to:references:date:message-id:subject:from:to
         :content-type:content-transfer-encoding;
        bh=gVwn6Qc0dZyuXF4RvrBqgUGBII+ECzIlvGt6xwpPc2Y=;
        b=e/FIkjSEsXeQyguneCJVP89dk70DMF4Lw9NdFUZb5Rbq+xnZcxJk+RvYDaNWrCj5q0
         EpDki5LSRf/El9J+mPDRXbJ8SvsFck9oIE9bOGMMlLFKRknwgaY4G+HNg9tMva9YWWXC
         HnLjlqXhFJRSusAlCS2HhaCflswxqY0qnE1J9Jn+DFvbSqOu7gt+M7e0TDTkIBtQ8CFG
         YID5lT2EkJxdtEK0gFa5e4J+VH9t9VcMdcohib0iwFwJSnk+5WxV9m53u2fTE2oMbuKY
         Rfdj8PRSh1hoIOdu+zg5f5ex3xvAE9xRXlT45GSMv8T1OUqhSRuAVqAJsFXyy0hsCfr8
         eNlw==
MIME-Version: 1.0
Received: by 10.213.20.201 with SMTP id g9mr276326ebb.72.1336539274154; Tue,
 08 May 2012 21:54:34 -0700 (PDT)
Received: by 10.14.48.11 with HTTP; Tue, 8 May 2012 21:54:34 -0700 (PDT)
In-Reply-To: <CAHNaMx84VU-MsB8EsqvoHRfkDsOFTXDEDewEaB6jL2p5jkg...@mail.gmail.com>
References: <CAHNaMx84VU-MsB8EsqvoHRfkDsOFTXDEDewEaB6jL2p5jkg...@mail.gmail.com>
Date: Wed, 9 May 2012 06:54:34 +0200
Message-ID: <CAJhx5RfkStjr88_Sg73Fw7jCBUt1EChLbiv4swyz7qF4wOx...@mail.gmail.com>
Subject: Re: Colander: unflatten invalid.asdict() error
From: Robert Forkel <xrotw...@googlemail.com>
To: pylons-discuss@googlegroups.com
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

you may want to have a look at cornice [1]. Among other things it
helps with validating json input via colander and handling resulting
errors.
regards,
robert

[1] http://cornice.readthedocs.org/en/latest/index.html

On Wed, May 9, 2012 at 3:52 AM, anh le <anh...@gmail.com> wrote:
> Hi all,
>
> I'm using colander to validate json input for my api. To report the
> invalid input to user
> I try to =A0unflatten the invalid.asdict()
>
> However if the invalid input located in the place other than the first
> place in a
> sequence the unflatten fails.
>
> Here is the snipets: (as =A0https://gist.github.com/2641011 )
>
> import colander
>
> class Phone(colander.MappingSchema):
> =A0 =A0location =3D colander.SchemaNode(colander.String(),
> =A0 =A0 =A0 =A0 =A0 =A0validator=3Dcolander.OneOf(['home', 'work']))
> =A0 =A0number =3D colander.SchemaNode(colander.String())
>
> class Phones(colander.SequenceSchema):
> =A0 =A0phone =3D Phone()
>
> class User(colander.MappingSchema):
> =A0 =A0name =3D colander.SchemaNode(colander.Str())
> =A0 =A0phones =3D Phones()
>
> user =3D User()
>
> d1 =3D {'name': 'jim',
> =A0 =A0 =A0'phones': [{'location': 'office', 'number': '12343'},
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {'location': 'home', 'number': '33131'}]
> }
>
> try:
> =A0 =A0user.deserialize(d1)
> except colander.Invalid as invalid:
> =A0 =A0print user.unflatten(invalid.asdict())
>
> # result:
> # {'phones': [{'location': u'"office" is not one of home, work'}]}
>
> # place invalid location as second position
> d2 =3D {'name': 'bob',
> =A0 =A0 =A0'phones': [{'location': 'home', 'number': '1234'},
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 {'location': 'office', 'number': '33131'}=
]
> }
>
> try:
> =A0 =A0user.deserialize(d2)
> except colander.Invalid as invalid:
> =A0 =A0print user.unflatten(invalid.asdict())
>
> # result:
> # ...
> # /opt/env/lib/python2.7/site-packages/colander/__init__.py", line
> 816, in unflatten
> # =A0 =A0 return [mapstruct[str(index)] for index in xrange(len(mapstruct=
))]
> # KeyError: '0'
> #
>
> How to handle this error or is there a better way to show the error in
> a api (json) validation?
>
> thanks,
>
> --
> You received this message because you are subscribed to the Google Groups=
 "pylons-discuss" group.
> To post to this group, send email to pylons-discuss@googlegroups.com.
> To unsubscribe from this group, send email to pylons-discuss+unsubscribe@=
googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pylo=
ns-discuss?hl=3Den.
>