marshmallow-validators

Release v3.0.0 (Changelog)

Use 3rd-party validators (e.g. from WTForms and colander) with marshmallow.

from marshmallow import Schema, fields
from marshmallow_validators.wtforms import from_wtforms
from wtforms.validators import Email, Length

# Leverage WTForms il8n
locales = ['de_DE', 'de']

class UserSchema(Schema):
    email = fields.Str(
        validate=from_wtforms([Email()], locales=locales)
    )
    password = fields.Str(
        validate=from_wtforms([Length(min=8, max=300)], locales=locales)
    )

UserSchema().validate({'email': 'invalid', 'password': 'abc'})
# {'email': ['Ungültige Email-Adresse.'],
# 'password': ['Feld muss zwischen 8 und 300 Zeichen beinhalten.']}

Get it now

pip install -U marshmallow-validators

WTForms support

class marshmallow_validators.wtforms.from_wtforms(validators, locales=None)[source]

Convert a WTForms validator from wtforms.validators to a marshmallow validator.

Example:

from marshmallow import fields
from marshmallow_validators.wtforms import from_wtforms
from wtforms.validators import Length

password = fields.Str(
    validate=from_wtforms([Length(min=8, max=100)])
)
Parameters:
  • validators (list) – WTForms validators.
  • locales (list) – List of locales for error messages, in order. If None, will try to use locale information from the environment.
make_validator(wtf_validator)[source]

Receives a 3rd-party validator and converts it to a marshmallow validator function/callable.

Parameters:validator – A 3rd-party validator object
Returns:A callable marshmallow validator
marshmallow_validators.wtforms.make_converter(locales=None)[source]

Return a WTForms validator converter that uses the given locales.

Example:

from marshmallow import fields
from marshmallow_validators.wtforms import make_converter
from wtforms.validators import Length

from_wtf = make_converter(['de_DE', 'de'])

field = fields.Str(from_wtf([Length(max=3)]))
Parameters:locales (list) – List of locales for error messages.

colander support

class marshmallow_validators.colander.from_colander(validators)[source]

Convert a colander validator to a marshmallow validator.

Example:

from marshmallow import fields
from marshmallow_validators.colander import from_colander
from colander import Length

password = fields.Str(
    validate=from_colander([Length(min=8, max=100)])
)
Parameters:validators (list) – Colander validators.
make_validator(colander_validator)[source]

Receives a 3rd-party validator and converts it to a marshmallow validator function/callable.

Parameters:validator – A 3rd-party validator object
Returns:A callable marshmallow validator

Base converter class

class marshmallow_validators.core.BaseConverter(validators)[source]

Base converter validator that converts a third-party validators into marshmallow validators. Concrete classes must implement make_validator.

Parameters:validators (list) – List of 3rd-party validator objects to convert.
make_validator(validator)[source]

Receives a 3rd-party validator and converts it to a marshmallow validator function/callable.

Parameters:validator – A 3rd-party validator object
Returns:A callable marshmallow validator

Project Info