weaver.wps_restapi.colander_extras

Module Contents

class weaver.wps_restapi.colander_extras.DropableNoneSchema[source]

Bases: colander.SchemaNode

Drops the underlying schema node if missing=drop was specified and that the value representing it is None.

Original behaviour of schema classes that can have children nodes such as colander.MappingSchema and colander.SequenceSchema are to drop the sub-node only if its value is resolved as colander.null or colander.drop. This results in “missing” definitions replaced by None in many implementations to raise colander.Invalid during deserialization. Inheriting this class in a schema definition will handle this situation automatically.

Required schemas (without missing=drop, i.e.: colander.required) will still raise for undefined nodes.

The following snippet shows the result that can be achieved using this schema class:

class SchemaA(DropableNoneSchema, MappingSchema):
    field = SchemaNode(String())

class SchemaB(MappingSchema):
    s1 = SchemaA(missing=drop)   # optional
    s2 = SchemaA()               # required

SchemaB().deserialize({"s1": None, "s2": {"field": "ok"}})
# >> {'s2': {'field': 'ok'}}
static schema_type()[source]
deserialize(self, cstruct)[source]
class weaver.wps_restapi.colander_extras.VariableMappingSchema[source]

Bases: colander.Mapping

Mapping schema that will allow any unknown field to remain present in the resulting deserialization.

This definition is useful for defining a dictionary where some field names are not known in advance. Other fields that are explicitly specified with sub-schema nodes will be validated as per usual behaviour.

class weaver.wps_restapi.colander_extras.SchemaNodeDefault[source]

Bases: colander.SchemaNode

If default keyword is provided during colander.SchemaNode creation, overrides the returned value by this default if missing from the structure during deserialize() call.

Original behaviour was to drop the missing value instead of replacing by the default. Executes all other colander.SchemaNode operations normally.

static schema_type()[source]
deserialize(self, cstruct)[source]
class weaver.wps_restapi.colander_extras.OneOfMappingSchema(*args, **kwargs)[source]

Bases: colander.MappingSchema

Allows specifying multiple supported mapping schemas variants for an underlying schema definition. Corresponds to the oneOf specifier of OpenAPI specification.

Example:

class Variant1(MappingSchema):
    [...fields of Variant1...]

class Variant2(MappingSchema):
    [...fields of Variant2...]

class RequiredByBoth(MappingSchema):
    [...fields required by both Variant1 and Variant2...]

class LiteralDataDomainType(OneOfMappingSchema, RequiredByBoth):
    _one_of = (Variant1, Variant2)
    [...alternatively, field required by all variants here...]

In the above example, the validation (ie: deserialize) process will succeed if any of the _one_of variants’ validator completely succeed, and will fail if every variant fails validation execution.

Warning

Because the validation process requires only at least one of the variants to succeed, it is important to insert more permissive validators later in the _one_of iterator. For example, having a variant with all fields defined as optional (ie: with missing=drop) inserted as first item in _one_of will make it always succeed regardless of following variants. This would have as side effect to never validate the other variants explicitly for specific field types and formats since the first option would always consist as a valid input fulfilling the specified definition (ie: an empty {} schema with all fields missing).

static _one_of()[source]
deserialize_one_of(self, cstruct)[source]
deserialize(self, cstruct)[source]
class weaver.wps_restapi.colander_extras.CustomTypeConversionDispatcher(custom_converters=None, default_converter=None)[source]

Bases: object

weaver.wps_restapi.colander_extras._dict_nested_equals(parent, child)[source]

Tests that a dict is ‘contained’ within a parent dict

>>> parent = {"other": 2, "test": [{"inside": 1, "other_nested": 2}]}
>>> child = {"test": [{"inside": 1}]}
>>> _dict_nested_equals(parent, child)
True
Parameters:
  • parent (dict) – The dict that could contain the child
  • child (dict) – The dict that could be nested inside the parent