API Reference

Schema

The schema module.

class schemable.schema.Schema(spec, strict=False, extra=None)

The primary schema class that defines the validation and loading specification of a schema.

This class is used to create a top-level schema object that can be called on input data to validation and load it according to the specification.

Parameters:
  • spec (object) – Schema specification.
  • strict (bool, optional) – Whether to evaluate schema in strict mode that will raise an exception on failed validation. Defaults to False.
  • extra (bool|None, optional) – Sets the extra keys policy when validating Dict schemas. Defaults to IGNORE_EXTRA.
class schemable.base.SchemaResult

The result returned from schema evaluation.

data

object – Parsed data that passed schema validation.

errors

object – Schema errors as None, dict, or str depending on whethere there were any errors and what kind of schema was defined.

exception schemable.base.SchemaError(message, errors, data, original_data)

Exception raised when schema validation fails during strict schema mode.

message

str – Generic error message.

errors

str|dict – Schema validation error string or dictionary.

data

list|dict|None – Partially parsed data or None.

original_data

object – Original data being validated.

Validators

The validators module.

class schemable.validators.All(*specs)

Schema helper that validates against a list of schemas where all schems must validate.

Parameters:*specs (object) – Schema specifications to validate against.
class schemable.validators.Any(*specs)

Schema helper that validates against a list of schemas where at least one schema must validate.

Parameters:*specs (object) – Schema specifications to validate against.
class schemable.validators.Dict(spec, extra=None)

Schema helper that validates against dict or dict-like objects.

Parameters:
  • spec (dict) – Dictionary containing schema specification to validate against.
  • extra (bool|None, optional) – Sets the extra keys policy. Defaults to IGNORE_EXTRA.
class schemable.validators.List(spec)

Schema helper that validates against list objects.

Parameters:spec (list) – List containing schema specification to validate each list item against.
class schemable.validators.Optional(spec, default=<NotSet>)

Schema helper used to mark a Dict key as optional.

Parameters:
  • spec (object) – Dict key schema specification.
  • default (object, optional) – Default value or callable that returns a default to be used when a key isn’t given.
class schemable.validators.Type(spec)

Schema helper that validates against types.

Parameters:spec (type|tuple[type]) – A type or tuple or tuple of types to validate against.
class schemable.validators.Validate(spec)

Schema helper that validates against a callable.

Validation passes if the callable returns None or a truthy value. Validation fails if the callable raises and exception or returns a non-None falsey value.

Parameters:spec (callable) – Callable to validate against.
class schemable.validators.Value(spec)

Schema helper that validates against value equality.

Parameters:spec (object) – Value to compare to.

Transforms

The transforms module.

class schemable.transforms.As(spec)

Schema helper that modifies a parsed schema value using a callable.

Unlike Validate the return value from the callable will replace the parsed value. However, if an exception occurs, validation will fail.

Parameters:spec (callable) – Callable that transforms a value.
class schemable.transforms.Select(spec, iteratee=<NotSet>)

Schema helper that selects and optionally modifies source data.

There are three ways to use:

  1. Select('<field>'): Returns data['<field>'].
  2. Select(<callable>): Passes source data to <callable> and returned value is the schema result.
  3. Select('<field>', <callable>): Passes data['<field>'] to <callable> and returned value is the schema result.
Parameters:
  • spec (str|callable) – The string field name to select from the source or a callable that accepts the source as its only argument.
  • iteratee (callable, optional) – A callable that modifies a source object field value. Is used when spec is a string that selects a field from the source and iteratee then modifies the field value.
class schemable.transforms.Use(spec)

Schema helper that returns a constant value or the return from a callable while ignoring the source data.

Parameters:spec (object) – Any object or callable.