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 isNone
.Original behaviour of schema classes that can have children nodes such as
colander.MappingSchema
andcolander.SequenceSchema
are to drop the sub-node only if its value is resolved ascolander.null
orcolander.drop
. This results in “missing” definitions replaced byNone
in many implementations to raisecolander.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'}}
-
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 duringcolander.SchemaNode
creation, overrides the returned value by this default if missing from the structure duringdeserialize()
call.Original behaviour was to drop the missing value instead of replacing by the default. Executes all other
colander.SchemaNode
operations normally.
-
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: withmissing=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).
-
class
weaver.wps_restapi.colander_extras.
CustomTypeConversionDispatcher
(custom_converters=None, default_converter=None)[source]¶ Bases:
object