bind_schema

bind_schema(schema, **kwargs)[source]

Class decorator for adding schema validation to its instances.

The decorator acts on the model class by adding:

  • a class attribute schema with the schema used for validation

  • a class attribute shallow_schema used for validation during instantiation.

The same schema cannot be bound more than once. If you need to reuse a schema for a different class, create a new schema subclassing the one you want to reuse and leave the new empty:

class MySchema(BaseSchema):
    title = String()

class AnotherSchema(MySchema):
    pass

@bind_schema(MySchema):
class MyModel(BaseModel):
    pass

@bind_schema(AnotherSchema):
class AnotherModel(BaseModel):
    pass

Note

By default, models decorated with this decorator are validated during instantiation. If validate=False is passed to the constructor, this validation will not be performed.

Parameters
  • schema (class) – the schema class used for validation.

  • **kwargs – Additional attributes for the marshmallow.Schema initializer.

Raises

ValueError – when trying to bind the same schema more than once.

Returns

the same class with validation capabilities.

Return type

type