def test_dict_nested_keys_validation():
    with given:
        sch = schema.dict(
            {"result": schema.dict({
                "id": schema.int,
                "name": schema.str,
            })})
        value = {"result": {"id": 1, "name": "Bob"}}

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == []
def test_dict_nested_missing_key_valudidation_error():
    with given:
        sch = schema.dict(
            {"result": schema.dict({
                "id": schema.int,
                "name": schema.str,
            })})
        value = {"result": {"id": 1}}

    with when:
        result = validate(sch, value)

    with then:
        path = PathHolder()["result"]
        assert result.get_errors() == [
            MissingKeyValidationError(path, value["result"], "name")
        ]
def test_dict_relaxed_no_keys_validation(value: Dict[Any, Any]):
    with given:
        sch = schema.dict({...:...})

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == []
def test_dict_no_keys_validation():
    with given:
        sch = schema.dict({})
        value = {}

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == []
def test_validator_validate_or_fail_multiline():
    with when, raises(Exception) as exception:
        validate_or_fail(schema.dict({
            "id": schema.int,
            "name": schema.str
        }), {})

    with then:
        assert exception.type is ValidationException
        assert str(exception.value) == ("\n - Key _['id'] does not exist"
                                        "\n - Key _['name'] does not exist")
Beispiel #6
0
def test_dict_with_optional_key_validation(value: Dict[Any, Any]):
    with given:
        sch = schema.dict({
            "id": schema.int,
            optional("name"): schema.str,
        })

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == []
def test_dict_extra_key_validation_error():
    with given:
        sch = schema.dict({})
        value = {"id": 1}

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == [
            ExtraKeyValidationError(PathHolder(), value, "id")
        ]
def test_dict_relaxed_keys_validation():
    with given:
        sch = schema.dict({
            "id": schema.int,
            "name": schema.str,
            ...:...,
        })
        value = {"id": 1, "name": "Bob", "is_deleted": False}

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == []
def test_dict_missing_key_validation_error():
    with given:
        sch = schema.dict({
            "id": schema.int,
            "name": schema.str,
        })
        value = {"id": 1}

    with when:
        result = validate(sch, value)

    with then:
        assert result.get_errors() == [
            MissingKeyValidationError(PathHolder(), value, "name")
        ]
def test_dict_validation_kwargs():
    with given:
        sch = schema.dict({
            "id": schema.int,
            "name": schema.str,
        })
        value = {"id": 1}
        path = PathHolder().items[0]["key"]

    with when:
        result = validate(sch, value, path=path)

    with then:
        assert result.get_errors() == [
            MissingKeyValidationError(path, value, "name")
        ]
Beispiel #11
0
def test_dict_with_optional_key_validation_error():
    with given:
        sch = schema.dict({
            "id": schema.int,
            optional("name"): schema.str,
        })

    with when:
        result = validate(sch, {
            "id": 1,
            "name": None
        })

    with then:
        assert result.get_errors() == [
            TypeValidationError(PathHolder()["name"], None, str)
        ]