Ejemplo n.º 1
0
class RequestSchema(Schema):
    """Request schema"""
    request_id = fields.Str(dump_only=True)
    title = fields.Str(
        required=True,
        validate=from_wtforms(
            [
                Length(
                    min=20,
                    max=100,
                    message="Request title should be between 20 to 50 characters"
                ),
            ]
        )
    )
    description = fields.Str(
        required=True,
        validate=from_wtforms(
            [
                Length(min=50, max=500,
                       message="Description should be between 50 to 500 characters"),
            ]
        )
    )
    email = fields.Email()

    @post_load
    def make_user(self, data):  # pylint: disable=R0201
        """Receives dictionary of deserialized data"""
        return Request(**data)
Ejemplo n.º 2
0
class LoginSchema(Schema):
    """Login schema"""
    email = fields.Email(
        required=True,
        validate=from_wtforms([
            Length(min=8,
                   max=50,
                   message="Email should be between 8 and 50 characters")
        ]))
    password = fields.Str(
        required=True,
        validate=from_wtforms([
            Length(min=8,
                   max=20,
                   message="Password should be between 8 and 20 characters"),
        ]))
 def test_from_wtforms_with_translation(self):
     field = fields.Field(
         validate=from_wtforms([Length(max=1)], locales=["de_DE", "de"]))
     with pytest.raises(MarshmallowValidationError) as excinfo:
         field.deserialize("toolong")
     # lol
     validation_msg = excinfo.value.args[0]
     assert "Feld kann nicht l\xe4nger als 1 Zeichen sein." in validation_msg
Ejemplo n.º 4
0
class UserSchema(Schema):
    """User schema"""
    user_id = fields.Int(dump_only=True)
    user_level = fields.Int(dump_only=True)
    first_name = fields.Str(
        required=True,
        validate=from_wtforms([
            Length(min=3,
                   max=15,
                   message="First name should be between 3 and 15 characters"),
            Regexp('^[^±!@£$%^&*_+§¡€#¢§¶•ªº«\\/<>?:;|=.,]{1,20}$',
                   message="Invalid first name"),
        ]))
    last_name = fields.Str(
        required=True,
        validate=from_wtforms([
            Length(min=3,
                   max=15,
                   message="Last name should be between 3 and 15"),
            Regexp('^[^±!@£$%^&*_+§¡€#¢§¶•ªº«\\/<>?:;|=.,]{1,20}$',
                   message="Invalid first name"),
        ]))
    email = fields.Email(required=True,
                         validate=from_wtforms([
                             Length(min=8,
                                    max=50,
                                    message="Email should be between 8 and 50")
                         ]))
    password = fields.Str(
        required=True,
        validate=from_wtforms([
            Length(min=8,
                   max=20,
                   message="Password should be between 8 and 20"),
            Regexp(PASS_REG, message="Weak password"),
        ]))
    registered_on = fields.DateTime(dump_only=True)

    @post_load
    def make_user(self, data):  # pylint: disable=R0201
        """Receives dictionary of deserialized data"""
        return User(**data)
Ejemplo n.º 5
0
class ModifyRequestSchema(Schema):
    """Request schema"""
    title = fields.Str(
        required=True,
        validate=from_wtforms(
            [
                Length(
                    min=20,
                    max=100,
                    message="Request title should be between 20 to 50 characters"
                ),
            ]
        )
    )
    description = fields.Str(
        required=True,
        validate=from_wtforms(
            [
                Length(min=50, max=500,
                       message="Description should be between 50 to 500 characters"),
            ]
        )
    )
    def test_from_wtforms_multi(self):
        field = fields.Field(validate=from_wtforms(
            [Length(min=4), NoneOf(["nil", "null", "NULL"])]))
        assert field.deserialize("thisisfine") == "thisisfine"
        with pytest.raises(MarshmallowValidationError,
                           match="Field must be at least 4 characters long"):
            field.deserialize("bad")
        with pytest.raises(
                MarshmallowValidationError,
                match="Invalid value, can't be any of: nil, null, NULL.",
        ):
            field.deserialize("null")

        with pytest.raises(MarshmallowValidationError) as excinfo:
            field.deserialize("nil")
        # both errors are returned
        error = excinfo.value
        assert "Invalid value, can't be any of: nil, null, NULL." in error.messages
        assert "Field must be at least 4 characters long." in error.messages
 class MySchema(Schema):
     foo = fields.Field(validate=from_wtforms([Length(max=1)]))
    def test_from_wtforms(self):
        field = fields.Field(validate=from_wtforms([AnyOf(["red", "blue"])]))

        assert field.deserialize("red") == "red"
        with pytest.raises(MarshmallowValidationError, match="Invalid value"):
            field.deserialize("green")
Ejemplo n.º 9
0
 def __init__(self):
     fields = [field for field in dir(Validators) if "__" not in field]
     for field in fields:
         setattr(self, field, from_wtforms(getattr(Validators, field)))