/marshmallow-extensions

Helpers to ease validations using Marshmallow

Primary LanguagePythonMIT LicenseMIT

Marshmallow Schema Fields Extension

Extension fields to use along with Marshmallow schemas.

Custom Fields


EnumField

Field to validate if the input string is a enum value.

from enum import Enum
from marshmallow import Schema

class FrequencyEnum(Enum):
    Daily = 'daily'
    Monthly = 'monthly'
    Weekly = 'weekly'
    Annually = 'annually'


class CustomSchema(Schema):
    frequency = EnumField(enum=FrequencyEnum, required=True)

''

Validates if the integer is positive, you can use the param allow_zero to set if zero is a valid value.

On this exemple, any number lesser than or equal to zero will throw an error during validation.

from marshmallow import Schema

class CustomSchema(Schema):
    amount = PositiveInteger(required=False)    

To allow zero, just pass the param allow_zero=True during field initialization.

from marshmallow import Schema

class CustomSchema(Schema):
    amount = PositiveInteger(allow_zero=True, required=False)    

Hex UUID

Validates if it is a UUID valid and return the hex value, instead of pythons native UUID instance when it's valid.

from marshmallow import Schema

class CustomSchema(Schema):
    id = UUIDHex(required=False)