I have a set of Colander SchemaNodes used with Pyramid/Cornice in an API. For some querystring args, a range is passed (ie time=X-Y
means a time range from X to Y where X and Y are integers representing epochs). I currently validate this with a RegEx()
validator to ensure an epoch or epoch range is passed in:
class TimeOrRange(SchemaNode):
schema_type = String
location = "querystring"
description = 'Time (or range) in epochs: ssssssssss(-ssssssssss)'
validator = Regex("^[0-9]{10}\-{0,1}[0-9]{0,10}$")
I then use this in a MappingSchema which is then tied to my Cornice view with @view(schema=TimedThingGet)
:
class TimedThingGet(MappingSchema):
time = TimeOrRange(missing=drop)
What I would like to do is update the return value in my TimeOrRange
SchemaNode code so time
in TimedThingGet
is a tuple of time ranges. In other words, if time=X-Y
is passed in to a TimedThingGet
instance, then time=(x, y)
is returned in the validated data. Similarly, if only X
is passed in, then I want Y
to be set to the epoch of now()
.
It looks like set_value()
is the way to go, and here's where the problem get's some extra credit:
set_value
get called before or after validation?set_value
have access to the validator such that a RegEx
validator which creates regex groups could then be used to set my tuple: time=(validated.match.group[1], validated.match.group[2])
?I think you should actually look at:
Preparer callable that you can pass to SchemaNode:
It will allow you to manipulate the data before it is passed to validators
And optionally at:
http://docs.pylonsproject.org/projects/colander/en/latest/binding.html#what-is-schema-binding
Which allows you set additional properies after schema is instantiated.