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:
-
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.
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.