Discussion:
[FE-discuss] Allowing validation to pass for partial post values?
Iain Duncan
2012-07-27 18:39:40 UTC
Permalink
Hi, I'm using form encode for validation, and am building a ReST back end
for use with Dojo data stores. the Dojo system wants to be able to use PUT
requests with partial lists of params, and I'm trying to figure out best to
do that. So if I have a schema, and some keys are missing, I want it to
'pass', with the values dictionary as is, rather than having the values
dict have values for the missing keys that come from using if_missing='foo'.

Is there way to do that without resorting to hackery like inspecting the
errors and discarding all the ones that are missing value errors?

thanks
Iain
Chris Lambacher
2012-07-27 20:04:17 UTC
Permalink
Hi Iain,

If what you want is to do is conditionally require some values then what
you are looking for is RequireIfMissing/RequireIfPresent which you put in
the chained_validators for the Schema:
http://www.formencode.org/en/latest/modules/validators.html#formencode.validators.RequireIfMissing


Note that it won't block other validation errors, so if you have an Int
validator and get a string, that validator will still give you an error
even if it will ultimately be ignored.

If that is a concern you can do a pre_validator that modifies / replaces
the value dict such that it includes only the values that should be kept.

-Chris
Post by Iain Duncan
Hi, I'm using form encode for validation, and am building a ReST back end
for use with Dojo data stores. the Dojo system wants to be able to use PUT
requests with partial lists of params, and I'm trying to figure out best to
do that. So if I have a schema, and some keys are missing, I want it to
'pass', with the values dictionary as is, rather than having the values
dict have values for the missing keys that come from using if_missing='foo'.
Is there way to do that without resorting to hackery like inspecting the
errors and discarding all the ones that are missing value errors?
thanks
Iain
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FormEncode-discuss mailing list
https://lists.sourceforge.net/lists/listinfo/formencode-discuss
--
Christopher Lambacher
***@kateandchris.net
Iain Duncan
2012-07-27 20:15:05 UTC
Permalink
Hi Chris, I'm not sure if what you're suggesting is what I want. Here's a
clarification, maybe you can tell me.

The schema for my tests looks like:

class PersonValidationSchema(ValidationSchema):
"""validation schema for resource Person"""
name_first = v.UnicodeString(not_empty=True)
name_last = v.UnicodeString(not_empty=True)
birthdate = IsoDate()

The names can't be empty, which is correct, because I want errors if we're
sent post values with empty strings for them. But what I need to be able to
do for Dojo is receive and *pass* a post dict that is missing a name_last
key, and get a validated dict also missing a name_last key. When that
happens, the write to the db will update only the fields that have keys in
my post vals, and ignore the others ( not setting them to none or any
default value).

Does that makes sense? Is that something I can do with what you are
suggesting?

thanks!
Iain
Post by Chris Lambacher
Hi Iain,
If what you want is to do is conditionally require some values then what
you are looking for is RequireIfMissing/RequireIfPresent which you put in
http://www.formencode.org/en/latest/modules/validators.html#formencode.validators.RequireIfMissing
Note that it won't block other validation errors, so if you have an Int
validator and get a string, that validator will still give you an error
even if it will ultimately be ignored.
If that is a concern you can do a pre_validator that modifies / replaces
the value dict such that it includes only the values that should be kept.
-Chris
Post by Iain Duncan
Hi, I'm using form encode for validation, and am building a ReST back end
for use with Dojo data stores. the Dojo system wants to be able to use PUT
requests with partial lists of params, and I'm trying to figure out best to
do that. So if I have a schema, and some keys are missing, I want it to
'pass', with the values dictionary as is, rather than having the values
dict have values for the missing keys that come from using if_missing='foo'.
Is there way to do that without resorting to hackery like inspecting the
errors and discarding all the ones that are missing value errors?
thanks
Iain
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and
threat landscape has changed and how IT managers can respond. Discussions
will include endpoint security, mobile security and the latest in malware
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
FormEncode-discuss mailing list
https://lists.sourceforge.net/lists/listinfo/formencode-discuss
--
Christopher Lambacher
Iain Duncan
2012-07-27 20:17:04 UTC
Permalink
Post by Iain Duncan
Hi Chris, I'm not sure if what you're suggesting is what I want. Here's a
clarification, maybe you can tell me.
"""validation schema for resource Person"""
name_first = v.UnicodeString(not_empty=True)
name_last = v.UnicodeString(not_empty=True)
birthdate = IsoDate()
The names can't be empty, which is correct, because I want errors if we're
sent post values with empty strings for them. But what I need to be able to
do for Dojo is receive and *pass* a post dict that is missing a name_last
key, and get a validated dict also missing a name_last key. When that
happens, the write to the db will update only the fields that have keys in
my post vals, and ignore the others ( not setting them to none or any
default value).
This should pass:
{ 'name_last':'Duncan', 'birthdate':'1911-01-01'}

This should fail:
{'name_last':'Duncan', 'name_first': '', 'birthdate': '1911-01-01' }

thanks!
Chris Lambacher
2012-07-27 20:32:34 UTC
Permalink
Post by Iain Duncan
{ 'name_last':'Duncan', 'birthdate':'1911-01-01'}
{'name_last':'Duncan', 'name_first': '', 'birthdate': '1911-01-01' }
thanks!
I don't think I am totally clear on the problem yet, but I am guessing that
you don't like the Value Missing error in the first case and the second
case the Empty value error is correct. If that is correct then what you
want I think is:

class PersonValidationSchema(ValidationSchema):
"""validation schema for resource Person"""
# will take if_missing value unvalidated
name_first = v.UnicodeString(not_empty=True, if_missing=None)
name_last = v.UnicodeString(not_empty=True, if_missing=None)
birthdate = IsoDate()

or

class PersonValidationSchema(ValidationSchema):
"""validation schema for resource Person"""
# will totally ignore a missing value (unless if_missing provided)
ignore_key_missing = True
name_first = v.UnicodeString(not_empty=True)
name_last = v.UnicodeString(not_empty=True)
birthdate = IsoDate()

or

class PersonValidationSchema(ValidationSchema):
"""validation schema for resource Person"""
# will run the validator with the value given (i.e. None)
if_key_missing = None
name_first = v.UnicodeString(not_empty=True)
name_last = v.UnicodeString(not_empty=True)
birthdate = IsoDate()


-Chris
--
Christopher Lambacher
***@kateandchris.net
Iain Duncan
2012-07-27 20:45:36 UTC
Permalink
Post by Iain Duncan
"""validation schema for resource Person"""
# will totally ignore a missing value (unless if_missing provided)
ignore_key_missing = True
name_first = v.UnicodeString(not_empty=True)
name_last = v.UnicodeString(not_empty=True)
birthdate = IsoDate()
The above was exactly what I needed. Is there a reason this is not included
in the narrative docs? It would be really helpful to have it mentioned on
this page somewhere,

http://www.formencode.org/en/latest/Validator.html

if it's in the docs, I had a hard time finding anything. Thanks so much!

Iain
Chris Lambacher
2012-07-27 20:50:58 UTC
Permalink
Post by Iain Duncan
The above was exactly what I needed. Is there a reason this is not
included in the narrative docs? It would be really helpful to have it
mentioned on this page somewhere,
The docs suck and need major work. There have been some efforts in the past
and they are better than they were.

Most things have comments in the code that document them, but these have
not been surface to user consumable docs:
https://github.com/formencode/formencode/blob/master/formencode/schema.py#L56


There are a bunch of properties of Schema that change the operation that
need to be documented somewhere. I generally need to open the code to know
the names of them.

-Chris
--
Christopher Lambacher
***@kateandchris.net
Iain Duncan
2012-07-27 21:09:51 UTC
Permalink
Post by Chris Lambacher
Post by Iain Duncan
The above was exactly what I needed. Is there a reason this is not
included in the narrative docs? It would be really helpful to have it
mentioned on this page somewhere,
The docs suck and need major work. There have been some efforts in the
past and they are better than they were.
Most things have comments in the code that document them, but these have
https://github.com/formencode/formencode/blob/master/formencode/schema.py#L56
There are a bunch of properties of Schema that change the operation that
need to be documented somewhere. I generally need to open the code to know
the names of them.
Hmm, that's a pity. Is it possible for us to volunteer time on them? Is
editing them open? I use formencode enough that I figure we can assign at
least an hour or two to fixing up some docs.

thanks
Iain
Post by Chris Lambacher
-Chris
--
Christopher Lambacher
Chris Lambacher
2012-07-27 21:20:48 UTC
Permalink
Post by Iain Duncan
Post by Chris Lambacher
Post by Iain Duncan
The above was exactly what I needed. Is there a reason this is not
included in the narrative docs? It would be really helpful to have it
mentioned on this page somewhere,
The docs suck and need major work. There have been some efforts in the
past and they are better than they were.
Most things have comments in the code that document them, but these have
https://github.com/formencode/formencode/blob/master/formencode/schema.py#L56
There are a bunch of properties of Schema that change the operation that
need to be documented somewhere. I generally need to open the code to know
the names of them.
Hmm, that's a pity. Is it possible for us to volunteer time on them? Is
editing them open? I use formencode enough that I figure we can assign at
least an hour or two to fixing up some docs.
Yes please. If you want you can use github's web editing feature to edit
the docs right there (behind the scenes it does a fork and a pull request).
I try to accept or reject pull requests pretty quickly:
https://github.com/formencode/formencode

I don't have the keys to the read the docs admin so someone needs to update
it to accept the new repo location for the push hooks.


thanks
Post by Iain Duncan
Iain
Your welcome, and thank you.
--
Christopher Lambacher
***@kateandchris.net
Loading...