Contrib / Schematics API¶
rafter.contrib.schematics.app¶
-
class
RafterSchematics
(**kwargs)¶ Bases:
rafter.app.Rafter
-
default_filters
= [<function filter_validate_schemas>, <function filter_transform_response>, <function filter_validate_response>]¶ - Validate request data
- Pass Rafter’s default filters
- Validate output data
-
resource
(uri, methods=frozenset({'GET'}), **kwargs)¶ Decorates a function to be registered as a resource route.
Parameters: - uri – path of the URL
- methods – list or tuple of methods allowed
- host –
- strict_slashes –
- stream –
- version –
- name – user defined route name for url_for
- filters – List of callable that will filter request and response data
- validators – List of callable added to the filter list.
- request_schema – Schema for request data
- response_schema – Schema for response data
Returns: A decorated function
-
add_resource
(handler, uri, methods=frozenset({'GET'}), **kwargs)¶ Register a resource route.
Parameters: - handler – function or class instance
- uri – path of the URL
- methods – list or tuple of methods allowed
- host –
- strict_slashes –
- version –
- name – user defined route name for url_for
- filters – List of callable that will filter request and response data
- validators – List of callable added to the filter list.
- request_schema – Schema for request data
- response_schema – Schema for response data
Returns: function or class instance
-
rafter.contrib.schematics.exceptions¶
-
exception
ValidationErrors
(errors: dict, **kwargs)¶ Bases:
rafter.exceptions.ApiError
-
data
¶ Returns a dictionnary containing all the passed data and an item
error_list
which holds the result oferror_list
.
-
error_list
¶ Returns an error list based on the internal error dict values. Each item contains a dict with
messages
andpath
keys.Example:
>>> errors = { >>> 'body': { >>> 'age': ['invalid age'], >>> 'options': { >>> 'extra': { >>> 'ex1': ['invalid ex1'], >>> } >>> } >>> } >>> } >>> e = ValidationErrors(errors) >>> e.error_list [ {'messages': ['invalid age'], 'location': ['body', 'age']}, {'messages': ['invalid ex1'], 'location': ['body', 'options', 'extra', 'ex1']}, ]
-
rafter.contrib.schematics.filters¶
-
filter_validate_schemas
(get_response, params)¶ This filter validates input data against the resource’s
request_schema
and fill the request’svalidated
dict.Data from
request.params
andrequest.body
(when the request body is of a form type) will be converted using the schema in order to get proper lists or unique values.Important
The request validation is only effective when a
request_schema
has been provided by the resource definition.
-
filter_validate_response
(get_response, params)¶ This filter process the returned response. It does 2 things:
- If the response is a
sanic.response.HTTPResponse
and not arafter.http.Response
, return it immediately. - It processes, validates and serializes this response when a schema is provided.
That means that you can always return a normal Sanic’s HTTPResponse and thus, bypass the validation process when you need to do so.
Important
The response validation is only effective when:
- A
response_schema
has been provided by the resource definition - The resource returns a
rafter.http.Response
instance or arbitrary data.
- If the response is a
rafter.contrib.schematics.helpers¶
-
model_node
(**kwargs)¶ Decorates a
schematics.Model
class to add it as a field of typeschematic.types.ModelType
.Keyword arguments are passed to
schematic.types.ModelType
.Example:
from schematics import Model, types from rafter.contrib.schematics.helpers import model_node class MyModel(Model): name = types.StringType() @model_node() class options(Model): status = types.IntType() # With arguments and another name @model_node(serialized_name='extra', required=True) class _extra(Model): test = types.StringType()