Skip to main content

json_contains

Function json_contains 

Source
pub fn json_contains(actual: &Value, expected: &Value) -> Result<(), String>
Expand description

Checks if actual contains all fields from expected.

Returns Ok(()) if matching succeeds, or Err(path) where path is the JSON path to the first mismatch.

§Matching Rules

  • Objects: All keys in expected must exist in actual with matching values. Extra keys in actual are ignored.
  • Arrays: Must match exactly (same length, same elements in order).
  • Primitives: Must be equal.

§Example

use fastapi_core::testing::json_contains;
use serde_json::json;

// Partial match succeeds - actual has extra "email" field
let actual = json!({"id": 1, "name": "Alice", "email": "alice@example.com"});
let expected = json!({"name": "Alice"});
assert!(json_contains(&actual, &expected).is_ok());

// Mismatch fails
let expected = json!({"name": "Bob"});
assert!(json_contains(&actual, &expected).is_err());