Re: Non-empty list validator in schema

Ian Bicking <[email protected]>
Newsgroups gmane.comp.python.formencode
Message-ID <[email protected]>
Graham Stratton wrote:
> Hi,
> 
> I want to validate a non-empty list of integers as part of a
> schema.  The validation works provided that a key exists in the data for
> the list, but if it doesn't exist the validator return an empty list:
> 
> 
>>>> class S(Schema):
>>>>
> ...   a = formencode.ForEach(formencode.validators.Int(),  
> not_empty=True)
> ...
> 
> 
>>>> S.to_python({'a':None})
>>>>
> formencode.api.Invalid: a: Please enter a value
> 
> 
>>>> S.to_python({})
>>>>
> {'a': []}
> 
> I've tried setting other parameters, but I can't work out what to do  
> about
> this.  Any help would be greatly appreciated.

It's the not_empty=True that's raising the error.  But when there's no 
value at all, if_missing is what comes into play.  Usually this is set 
to formencode.api.NoDefault, which causes the Schema to raise an error. 
  But for ForEach it is set to [].


-- 
Ian Bicking | [email protected] | http://blog.ianbicking.org
             | Write code, do good | http://topp.openplans.org/careers

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.