예제 #1
0
    def _get_validator__ordered(self):
        from asobibi import validator

        def ordered(k, x, y):
            assert x < y
            return x
        return validator("FstValidator", [(("x", "y"), ordered)])
예제 #2
0
    def test_nested_validator__all(self):
        from asobibi import validator, ValidationError
        from asobibi.construct import ErrorList
        Schema = self._get_schema()

        def odd(k, x):
            if x % 2 != 1:
                raise ValidationError(dict(fmt="not odd, {field}", field=k))
        point = self._get_schema()(x="10", y="20")
        point = self._get_validator__bigger()(point)
        point = self._get_validator__ordered()(point)
        point = validator("Vx", [(Schema.x, odd)])(point)
        point = validator("Vy", [(Schema.y, odd)])(point)

        assert point.validate() is not True
        assert point.result is not True
        assert str(point.errors) == str(ErrorList({'y': ['not odd, y'], 'x': ['not odd, x']}))
예제 #3
0
    def _get_validator__with_extra_data(self, Point):
        from asobibi import validator
        from asobibi import WithExtra
        from asobibi import ValidationError

        def validation_with_extradata(_, x, y, z=0):
            if x * x + y * y != z * z:
                raise ValidationError({"fmt": "not x^2 + y^2 == z^2 ({0} != {1})".format(x * x + y * y, z * z)})

        return validator("Validator",
                         [((Point.x, Point.y), WithExtra(validation_with_extradata))])
예제 #4
0
    def _get_validator__bigger(self):
        from asobibi import validator

        def bigger(k, x, y):
            assert x * x + y * y > 100
        return validator("SndValidator", [(("x", "y"), bigger)])
예제 #5
0
파일: main.py 프로젝트: podhmo/asobibi
submit = Submit(mail="foo", password="******", confirm="@")
assert submit.validate() == False

submit = Submit(mail="*****@*****.**", password="******", confirm="@")
assert submit.validate()
assert submit.result["mail"] == "*****@*****.**"
assert submit.password == "@"
assert submit.confirm == "@"


# validator definition
def same(k, x, y):
    assert x == y

PasswordValidator = validator("PasswordValidator",
                              [((Submit.password, Submit.confirm), same)])

submit = PasswordValidator(
    Submit(mail="*****@*****.**", password="******", confirm="*"))

assert submit.validate() == False
assert submit.errors.keys() == ["password"]

# when optional, if validate candidates fields are optional then, registered validations are not called.
Submit2 = schema(
    "Submit",
    (Unicode("mail", initial="sample@mail", converters=[tiny_email]),
     Unicode("password", required=False),
     Unicode("confirm", required=False)
     ))
submit = PasswordValidator(