vantage6.backend.common.resource.input_schema.RoleInputSchema¶
- class RoleInputSchema(default_roles)¶
Bases:
_NameValidationSchema
Schema for validating input for creating a role.
- __init__(default_roles)¶
Methods
__init__
(default_roles)dump
(obj, *[, many])Serialize an object to native Python data types according to this Schema's fields.
dumps
(obj, *args[, many])Same as
dump()
, except return a JSON-encoded string.from_dict
(fields, *[, name])Generate a Schema class given a dictionary of fields.
get_attribute
(obj, attr, default)Defines how to pull values from an object to serialize.
handle_error
(error, data, *, many, **kwargs)Custom error handler function for the schema.
load
(data, *[, many, partial, unknown])Deserialize a data structure to an object defined by this Schema's fields.
loads
(json_data, *[, many, partial, unknown])Same as
load()
, except it takes a JSON string as input.on_bind_field
(field_name, field_obj)Hook to modify a field when it is bound to the Schema.
validate
(data, *[, many, partial])Validate data against the schema, returning a dictionary of validation errors.
validate_name
(name)Validate that role name is not one of the default roles.
Attributes
TYPE_MAPPING
dict_class
Overrides for default schema-level error messages
opts
set_class
Dictionary mapping field_names ->
Field
objects- class Meta¶
Bases:
object
Options object for a Schema.
Example usage:
class Meta: fields = ("id", "email", "date_created") exclude = ("password", "secret_attribute")
Available options:
fields
: Tuple or list of fields to include in the serialized result.additional
: Tuple or list of fields to include in addition to theexplicitly declared fields.
additional
andfields
are mutually-exclusive options.
include
: Dictionary of additional fields to include in the schema. It isusually better to define fields as class variables, but you may need to use this option, e.g., if your fields are Python keywords. May be an OrderedDict.
exclude
: Tuple or list of fields to exclude in the serialized result.Nested fields can be represented with dot delimiters.
dateformat
: Default format for Date <fields.Date> fields.datetimeformat
: Default format for DateTime <fields.DateTime> fields.timeformat
: Default format for Time <fields.Time> fields.render_module
: Module to use for loads <Schema.loads> and dumps <Schema.dumps>.Defaults to json from the standard library.
ordered
: If True, order serialization output according to theorder in which fields were declared. Output of Schema.dump will be a collections.OrderedDict.
index_errors
: If True, errors dictionaries will include the indexof invalid items in a collection.
load_only
: Tuple or list of fields to exclude from serialized results.dump_only
: Tuple or list of fields to exclude from deserializationunknown
: Whether to exclude, include, or raise an error for unknownfields in the data. Use EXCLUDE, INCLUDE or RAISE.
register
: Whether to register the Schema with marshmallow’s internalclass registry. Must be True if you intend to refer to this Schema by class name in Nested fields. Only set this to False when memory usage is critical. Defaults to True.
- OPTIONS_CLASS¶
alias of
SchemaOpts
- dump(obj, *, many=None)¶
Serialize an object to native Python data types according to this Schema’s fields.
- Parameters:
obj (
Any
) – The object to serialize.many (
Optional
[bool
]) – Whether to serialize obj as a collection. If None, the value for self.many is used.
- Returns:
Serialized data
Added in version 1.0.0.
Changed in version 3.0.0b7: This method returns the serialized data rather than a
(data, errors)
duple. AValidationError
is raised ifobj
is invalid.Changed in version 3.0.0rc9: Validation no longer occurs upon serialization.
- dumps(obj, *args, many=None, **kwargs)¶
Same as
dump()
, except return a JSON-encoded string.- Parameters:
obj (
Any
) – The object to serialize.many (
Optional
[bool
]) – Whether to serialize obj as a collection. If None, the value for self.many is used.
- Returns:
A
json
string
Added in version 1.0.0.
Changed in version 3.0.0b7: This method returns the serialized data rather than a
(data, errors)
duple. AValidationError
is raised ifobj
is invalid.
- error_messages: typing.Dict[str, str] = {}¶
Overrides for default schema-level error messages
- fields: typing.Dict[str, ma_fields.Field]¶
Dictionary mapping field_names ->
Field
objects
- classmethod from_dict(fields, *, name='GeneratedSchema')¶
Generate a Schema class given a dictionary of fields.
from marshmallow import Schema, fields PersonSchema = Schema.from_dict({"name": fields.Str()}) print(PersonSchema().load({"name": "David"})) # => {'name': 'David'}
Generated schemas are not added to the class registry and therefore cannot be referred to by name in Nested fields.
- Parameters:
fields (dict) – Dictionary mapping field names to field instances.
name (str) – Optional name for the class, which will appear in the
repr
for the class.
- Return type:
type
Added in version 3.0.0.
- get_attribute(obj, attr, default)¶
Defines how to pull values from an object to serialize.
Added in version 2.0.0.
Changed in version 3.0.0a1: Changed position of
obj
andattr
.
- handle_error(error, data, *, many, **kwargs)¶
Custom error handler function for the schema.
- Parameters:
error (
ValidationError
) – The ValidationError raised during (de)serialization.data (
Any
) – The original input data.many (
bool
) – Value ofmany
on dump or load.partial – Value of
partial
on load.
Added in version 2.0.0.
Changed in version 3.0.0rc9: Receives many and partial (on deserialization) as keyword arguments.
- load(data, *, many=None, partial=None, unknown=None)¶
Deserialize a data structure to an object defined by this Schema’s fields.
- Parameters:
data (
Union
[Mapping
[str
,Any
],Iterable
[Mapping
[str
,Any
]]]) – The data to deserialize.many (
Optional
[bool
]) – Whether to deserialize data as a collection. If None, the value for self.many is used.partial (
Union
[bool
,Sequence
[str
],Set
[str
],None
]) – Whether to ignore missing fields and not require any fields declared. Propagates down toNested
fields as well. If its value is an iterable, only missing fields listed in that iterable will be ignored. Use dot delimiters to specify nested fields.unknown (
Optional
[str
]) – Whether to exclude, include, or raise an error for unknown fields in the data. Use EXCLUDE, INCLUDE or RAISE. If None, the value for self.unknown is used.
- Returns:
Deserialized data
Added in version 1.0.0.
Changed in version 3.0.0b7: This method returns the deserialized data rather than a
(data, errors)
duple. AValidationError
is raised if invalid data are passed.
- loads(json_data, *, many=None, partial=None, unknown=None, **kwargs)¶
Same as
load()
, except it takes a JSON string as input.- Parameters:
json_data (
str
) – A JSON string of the data to deserialize.many (
Optional
[bool
]) – Whether to deserialize obj as a collection. If None, the value for self.many is used.partial (
Union
[bool
,Sequence
[str
],Set
[str
],None
]) – Whether to ignore missing fields and not require any fields declared. Propagates down toNested
fields as well. If its value is an iterable, only missing fields listed in that iterable will be ignored. Use dot delimiters to specify nested fields.unknown (
Optional
[str
]) – Whether to exclude, include, or raise an error for unknown fields in the data. Use EXCLUDE, INCLUDE or RAISE. If None, the value for self.unknown is used.
- Returns:
Deserialized data
Added in version 1.0.0.
Changed in version 3.0.0b7: This method returns the deserialized data rather than a
(data, errors)
duple. AValidationError
is raised if invalid data are passed.
- on_bind_field(field_name, field_obj)¶
Hook to modify a field when it is bound to the Schema.
No-op by default.
- Return type:
None
- validate(data, *, many=None, partial=None)¶
Validate data against the schema, returning a dictionary of validation errors.
- Parameters:
data (
Union
[Mapping
[str
,Any
],Iterable
[Mapping
[str
,Any
]]]) – The data to validate.many (
Optional
[bool
]) – Whether to validate data as a collection. If None, the value for self.many is used.partial (
Union
[bool
,Sequence
[str
],Set
[str
],None
]) – Whether to ignore missing fields and not require any fields declared. Propagates down toNested
fields as well. If its value is an iterable, only missing fields listed in that iterable will be ignored. Use dot delimiters to specify nested fields.
- Return type:
dict
[str
,list
[str
]]- Returns:
A dictionary of validation errors.
Added in version 1.1.0.
- validate_name(name)¶
Validate that role name is not one of the default roles.
- Parameters:
name (str) – Role name to validate.
- Raises:
ValidationError – If the role name is one of the default roles.