Example #1
0
def test_schema_singledispatch():
    import os

    assert type(b.schema(int)) == b.Pipe
    assert type(b.schema(lambda: 3 < 2)) == b.Predicate
    assert type(b.schema(os.path.exists)) == b.Predicate
    assert type(b.schema({"age": b.Pipe("int")})) == b.Map
Example #2
0
def test_schema_singledispatch():
    import os

    assert type(b.schema(int)) == b.Pipe
    assert type(b.schema(lambda: 3 < 2)) == b.Predicate
    assert type(b.schema(os.path.exists)) == b.Predicate
    assert type(b.schema({"age": b.Pipe("int")})) == b.Map
Example #3
0
def test_nested_map_errors():
    data = {
        "name": 1000,
        "amount": {
            "amount": 1000,
            "currency": 1000
        }
    }

    validator = b.schema({
        "c": b.Type(str),
        "amount": b.Map({
            "amount": b.Type(str),
            "currency": b.Type(str)
        })
    })

    with pytest.raises(SchemaErrors) as exc:
        validator.validate(data)

    expected = {
        'c': 'Field `c` is required.',
        'amount': {
            'amount': "Not of type `<class 'str'>`",
            'currency': "Not of type `<class 'str'>`"
        }
    }
    assert expected == exc.value.errors
Example #4
0
class OrganizationForCreateValidator(v.Validator):
    schema = b.schema({
        "name": b.And(
            t.String(),
            s.NotEmpty(),
        )
    })
Example #5
0
class OrganizationForUpdateValidator(v.Validator):
    schema = b.schema({
        b.Optional("address"): b.And(
            t.String(),
            s.NotEmpty(),
        ),
        b.Optional("state"): b.And(
            t.String(),
            s.NotEmpty(),
        ),
        b.Optional("country"): b.And(
            t.String(),
            s.NotEmpty(),
        ),
        b.Optional("sector"): b.And(
            t.String(),
            s.NotEmpty(),
        ),
        b.Optional("web"): b.And(
            t.String(),
            s.NotEmpty(),
            s.URL(),
        ),
        b.Optional("description"): b.And(
            t.String(),
            s.NotEmpty(),
        ),
    })
Example #6
0
class TestValidate:
    schema = b.schema({
        "name":
        b.Predicate(lambda name: len(name) > 0),
        "age":
        b.And(
            b.Pipe(int, message="User age must be an integer"),
            b.Predicate(lambda age: 18 <= age < 99,
                        message="User has to be an adult"))
    })

    def test_valid(self):
        data = {"name": "skame", "age": "28"}
        assert validate(self.schema, data) == ({
            "name": "skame",
            "age": 28
        }, None)

    def test_not_valid(self):
        assert validate(self.schema, {}) == (None, {
            'name': 'Field `name` is required.',
            'age': 'Field `age` is required.'
        })

    def test_custom_validation_error(self):
        data = {"name": "skame", "age": "1"}
        assert validate(self.schema, data) == (None, {
            "age": "User has to be an adult"
        })
        data = {"name": "skame", "age": ""}
        assert validate(self.schema, data) == (None, {
            "age":
            "User age must be an integer"
        })
Example #7
0
class TestCleanDataOrRaise:
    schema = b.schema({
        "name":
        b.Predicate(lambda name: len(name) > 0),
        "age":
        b.And(
            b.Pipe(int, message="User age must be an integer"),
            b.Predicate(lambda age: 18 <= age < 99,
                        message="User has to be an adult"))
    })

    def test_valid(self):
        data = {"name": "skame", "age": "28"}
        assert clean_data_or_raise(self.schema, data) == {
            "name": "skame",
            "age": 28
        }

    def test_not_valid(self):
        with pytest.raises(SchemaErrors):
            clean_data_or_raise(self.schema, {})

    def test_custom_validation_error(self):
        try:
            clean_data_or_raise(self.schema, {"name": "skame", "age": "1"})
        except SchemaErrors as e:
            assert e.errors["age"] == "User has to be an adult"

        try:
            clean_data_or_raise(self.schema, {"name": "skame", "age": "skame"})
        except SchemaErrors as e:
            assert e.errors["age"] == "User age must be an integer"
Example #8
0
def test_nested_map_errors():
    data = {"name": 1000, "amount": {"amount": 1000, "currency": 1000}}

    validator = b.schema({
        "c":
        b.Type(str),
        "amount":
        b.Map({
            "amount": b.Type(str),
            "currency": b.Type(str)
        })
    })

    with pytest.raises(SchemaErrors) as exc:
        validator.validate(data)

    expected = {
        'c': 'Field `c` is required.',
        'amount': {
            'amount': "Not of type `<class 'str'>`",
            'currency': "Not of type `<class 'str'>`"
        }
    }
    assert expected == exc.value.errors
Example #9
0
        super(RecursiveList, self).__init__(*args, **kwargs)
        self._item_schema = item_schema

    def validate(self, data):
        all_errors = []
        result = super(RecursiveList, self).validate(data)

        for item in data:
            data, errors = validate(self._item_schema, item)
            if errors:
                all_errors.append(errors)

        if all_errors:
            raise SchemaErrors(all_errors)

        return result


turn_character_actions_schema = b.schema({
    "place": b.Pipe(str, message="Place must be present"),
    "action": b.Pipe(str, message="Action must be present"),
    # Optional:
    # "target": b.Pipe(dict, message="Target must be present"),
})


turn_character_schema = b.schema({
    "slug": b.Pipe(str, message="Character slug must be present"),
    "actions": RecursiveList(turn_character_actions_schema),
})