Crate assert_json

Crate assert_json 

Source
Expand description

A easy and declarative way to test JSON input in Rust.

assert_json! is a Rust macro heavily inspired by serde_json::json! macro. Instead of creating a JSON value from a JSON literal, assert_json! makes sure the JSON input conforms to the validation rules specified.

assert_json! also output beautiful error message when a validation error occurs.

#[test]
fn test_json_ok() {
    let json = r#"
        {
            "status": "success",
            "result": {
                "id": 5,
                "name": "charlesvdv"
            }
        }
    "#;

    let name = "charlesvdv";

    assert_json!(json, {
            "status": "success",
            "result": {
                "id": validators::u64(|&v| if v > 0 { Ok(())} else { Err(String::from("id should be greater than 0")) }),
                "name": name,
            }
        }
    );
}

Modules§

validators
Custom validators for different JSON types

Macros§

assert_json
Assert that a json value matches its validation rules

Enums§

Error
Validation error

Traits§

Validator
Abstract the validation action for assert_json! macro.

Type Aliases§

Value
A JSON-value. Used by the Validator trait.