def test_email_repr(): assert repr(validate.Email(error=None)) == "<Email(error={!r})>".format( "Not a valid email address." ) assert repr(validate.Email(error="foo")) == "<Email(error={!r})>".format( "foo" )
class UserSchema(Schema): id = fields.Int(dump_only=True) email = fields.Str( required=True, validate=validate.Email(error="Not a valid email address"), ) password = fields.Str( required=True, validate=[validate.Length(min=6, max=36)], load_only=True, ) joined_on = fields.DateTime(dump_only=True) # Clean up data @pre_load def process_input(self, data): data["email"] = data["email"].lower().strip() return data # We add a post_dump hook to add an envelope to responses @post_dump(pass_many=True) def wrap(self, data, many): key = "users" if many else "user" return {key: data}
def test_email_invalid(invalid_email): validator = validate.Email() with pytest.raises(ValidationError): validator(invalid_email)
def test_email_custom_message(): validator = validate.Email(error="{input} is not an email addy.") with pytest.raises(ValidationError) as excinfo: validator("invalid") assert "invalid is not an email addy." in str(excinfo)
def test_email_valid(valid_email): validator = validate.Email() assert validator(valid_email) == valid_email