api/schemas/ and are used in the service layer to validate incoming data before processing.
Schema structure
Each schema is a marshmallowSchema subclass. The naming convention is Post<Name>Schema for POST request schemas:
Using a schema in a service
validation_error() is a helper in api/helper/functions/validation_error.py that converts a marshmallow ValidationError into an APIException(400) automatically.
Common field types
| Field | Description |
|---|---|
fields.String() | String value |
fields.Integer() | Integer value |
fields.Float() | Float value |
fields.Boolean() | Boolean value |
fields.Email() | Validates email format |
fields.URL() | Validates URL format |
fields.List(fields.String()) | List of strings |
fields.Dict() | Arbitrary dict |
fields.DateTime() | ISO 8601 datetime string |
Field options
| Option | Description |
|---|---|
required=True | Field must be present in input |
load_default=value | Default value when field is missing |
allow_none=True | Allow null values |
validate= | Attach a validator function |
Validation example
Nested schemas
Error format
When validation fails,validation_error() raises APIException(400). The response body will be:
error.messages in the ValidationError and pass them to APIException: