Filtered Parser¶
API-Reference: wsgi_tools.filtered_parser()
This includes the WSGI-app wsgi_tools.filtered_parser.FilteredJSONParser()
. It is structured like the wsgi_tools.parser.JSONParser()
described here, but does not accept every json-string.
What json-strings to accept is configured in filters.
A filter is a callable, which has the value to control as an arg and returns a tuple of the boolean, which tells you if this value is allowed and the reason as a string. If the value is allowed, the reason is not relevant and can be an empty string or None
.
There are the premade filters wsgi_tools.filtered_parser.Int()
, wsgi_tools.filtered_parser.Float()
, wsgi_tools.filtered_parser.Boolean()
, wsgi_tools.filtered_parser.Object()
, wsgi_tools.filtered_parser.String()
, wsgi_tools.filtered_parser.Options()
and wsgi_tools.filtered_parser.List()
.
wsgi_tools.filtered_parser.Boolean()
andwsgi_tools.filtered_parser.String()
are unlike the other premade filters directly filters. They don’t have to be construced.wsgi_tools.filtered_parser.Int()
andwsgi_tools.filtered_parser.Float()
have the two optional argsmin
andmax
.wsgi_tools.filtered_parser.Options()
has to be construced with other filters. If one of them allows the value, thewsgi_tools.filtered_parser.Options()
allows the value.wsgi_tools.filtered_parser.List()
has to be constructed with another filter. If this filter allows every value in the list, thewsgi_tools.filtered_parser.List()
allows the list.wsgi_tools.filtered_parser.Object()
has to be constructed with a dict of this shape:{ "key1": filter1, "key2": (filter2, True) }
In this example, the object has to have a key
"key1"
and it’s value has to be allowed byfilter1
. The key"key2"
is optinal, because it is in a tuple withTrue
as the second value. If"key2"
exists, it’s value has to be allowed byfilter2
. Otherwieswsgi_tools.filtered_parser.Object()
won’t allow this json-object.