Schema#

class oblate.Schema(data, /, *, state=None, ignore_extra=...)#

The base class for all schemas.

All user defined schemas must inherit from this class. When initializing the raw data is passed in form of dictionary or other mapping as a positional argument.

Parameters:
  • data (Mapping[str, Any]) – The raw data to initialize the schema with.

  • state – The initial value for SchemaContext.state attribute. This can be used to store and propagate custom stateful information which may be useful in deserialization of data.

  • ignore_extra (bool) –

    Whether to ignore extra (invalid) fields in the data.

    This parameter overrides the SchemaConfig.ignore_extra configuration.

__config__#

alias of SchemaConfig

preprocess_data(data, /)#

Preprocesses the input data.

This method is called before the raw data is serialized. The processed data must be returned. By default, this method returns the data as-is.

Warning

The data parameter to schema constructor is passed directly to this method without any validation so the data may be invalid.

Parameters:

data (Mapping[str, Any]) – The input data.

Returns:

The processed data.

Return type:

Mapping[str, Any]

__schema_post_init__()#

The post initialization hook.

This method is called when the schema is done initializing. This method is meant to be overriden by subclasses and does nothing by default.

New in version 1.1.

property context#

The context for this schema.

Schema context holds the information about schema and its state.

Type:

SchemaContext

copy()#

Copies the current schema.

Returns:

The copied schema instance.

Return type:

Schema

get_value_for(field_name, default=..., /)#

Returns the value for a field.

If field has no value set, a ValueError is raised unless a default is provided.

Parameters:
  • field_name (str) – The name of field to get value for. This can either be field (attribute) name or the value of fields.Field.load_key.

  • default – The default value to return if field has no value.

Return type:

The field value.

Raises:
  • RuntimeError – Invalid field name.

  • FieldNotSet – Field value is not set.

update(data, /, *, ignore_extra=...)#

Updates the schema with the given data.

If the update fails i.e one or more fields fail to validate, the schema’s state (field values) is rolled back to previous state.

Parameters:
  • data (Mapping[str, Any]) – The data to update with.

  • ignore_extra (bool) –

    Whether to ignore extra (invalid) fields in the data.

    This parameter overrides the SchemaConfig.ignore_extra configuration.

Raises:
  • FrozenError – The schema is read only or one of the fields attempted to be updated is read only and cannot be updated.

  • ValidationError – The validation failed.

dump(*, include=..., exclude=...)#

Serializes the schema to raw form.

The returned value is serialized data in dictionary form. The include and exclude parameters are mutually exclusive.

Parameters:
  • include (Sequence[str]) – The fields to include in the returned data.

  • exclude (Sequence[str]) – The fields to exclude from the returned data.

Returns:

The serialized data.

Return type:

Dict[str, Any]

Raises:
  • TypeError – Both include and exclude provided.

  • ValidationError – Validation failed while serializing one or more fields.