Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Colander schemas for query string parsing
class ColanderSearchParamsSchema(colander.Schema):
# Fields that affect search result number and ordering.
sort = colander.SchemaNode(colander.String(),
validator=colander.OneOf(["updated", "created", "id", "group", "user"]),
missing="updated")
order = colander.SchemaNode(colander.String(),
validator=colander.OneOf(["asc", "desc"]),
missing="desc")
limit = colander.SchemaNode(colander.Integer(),
validator=colander.Range(0, 200),
missing=20)
offset = colander.SchemaNode(colander.Integer(),
validator=colander.Range(0, 2000),
missing=0)
# Undocumented options
_separate_replies = colander.SchemaNode(colander.Boolean(), missing=False)
# Specific fields that can be searched by.
any = colander.SchemaNode(colander.String(), missing=colander.drop)
group = colander.SchemaNode(colander.String(),
missing=colander.drop)
quote = colander.SchemaNode(colander.String(), missing=colander.drop)
references = colander.SchemaNode(colander.String(), missing=colander.drop)
tag = colander.SchemaNode(colander.String(), missing=colander.drop)
tags = colander.SchemaNode(colander.String(), missing=colander.drop)
uri = colander.SchemaNode(colander.String(), missing=colander.drop)
url = colander.SchemaNode(colander.String(), missing=colander.drop)
user = colander.SchemaNode(colander.String(), missing=colander.drop)
from schemas import ColanderSearchParamsSchema
from webob.multidict import MultiDict
params = MultiDict({"order": "asc", "limit": "5", "unknown": "foobar", "_separate_replies": "true"})
print("Input params: ", params)
schema = ColanderSearchParamsSchema()
parsed_params = schema.deserialize(params)
print("Parsed params: ", parsed_params)
# TODO - Figure out how to handle repeat-able fields ("text", "tag", "quote" etc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment