On Thu, 2011-03-24 at 12:41 -0700, oO wrote:
> Is it possible to declare a Mapping Schema with a built-in validator
> attached to it?
> 
> I'm creating a form has uses Geo coordinates ( lat, lon ) and I'm
> looking for the best way to implement these.
> Right now, I have the following:
> 
> class GeoCoordSchema(colander.MappingSchema):    
>     lat = colander.SchemaNode(
>         colander.Float(),
>         missing = colander.null,
>         validator = validators.latitude # custom validator, allows
> null or -90 <> 90
>     )
>     lon = colander.SchemaNode(
>         colander.Float(),
>         missing = colander.null,
>         validator = validators.longitude # custom validator, allows
> null or -180 <> 180
>     )
> 
> I also implemented a higher level validator, that makes sure that the
> coordinates is either null,null
> or a valid latitude,longitude pairs.
> 
> this works, but I need to remember to add the validator when I declare
> my CoordSchema as part of another schema, and I'm sure I'll forget at
> one point.

Just assign the result of creating the schema to a variable and import
it from somewhere:

coord_schema = GeoCoordSchema(validator=...)

from foo import coord_schema

> 
> Questions
>      1. is it possible to make a sub-schema optional (missing = null )
>         but still have fields within the schema be required? I would
>         interpret that as "Coordinates are optional, but if you fill
>         them, both latitude and longitude fields are required". This
>         is essentially what my top level validator is doing, so I
>         would love to see this be done at the library level. ( I would
>         have a similar pattern for street addresses, for example)
>      2. Is there a way to declare the validator as part of the schema
>         class definition itself? I tried overriding the __init__,
>         adding a property called validator, etc.
>      3. The last thing I'm thinking about would be to declare a custom
>         type called GeoCoord which would handle the lat,lon as a unit,
>         either as a dictionary or a tuple. Is that possible? I only
>         saw examples of custom types for simple values (int, string,
>         date, ...)

Don't have the appropriate energy level to answer any of these at the
moment, sorry.  I'll try later.

- C


> 
> 
> 
> -- 
> 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 pylons-discuss
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.


-- 
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.

Reply via email to