Skip to main content

json_schema_diff/
lib.rs

1#![doc = include_str!("../README.md")]
2#![warn(missing_docs)]
3
4use schemars::schema::{RootSchema, Schema};
5use serde_json::Value;
6use thiserror::Error;
7
8mod diff_walker;
9mod resolver;
10mod types;
11
12pub use types::*;
13
14/// Take two JSON schemas, and compare them.
15///
16/// `lhs` (left-hand side) is the old schema, `rhs` (right-hand side) is the new schema.
17pub fn diff(lhs: Value, rhs: Value) -> Result<Vec<Change>, Error> {
18    let lhs_root: RootSchema = serde_json::from_value(lhs)?;
19    let rhs_root: RootSchema = serde_json::from_value(rhs)?;
20
21    let mut changes = vec![];
22    let mut walker = diff_walker::DiffWalker::new(
23        |change: Change| {
24            changes.push(change);
25        },
26        lhs_root,
27        rhs_root,
28    );
29    walker.diff(
30        "",
31        &mut Schema::Object(walker.lhs_root.schema.clone()),
32        &mut Schema::Object(walker.rhs_root.schema.clone()),
33    )?;
34    Ok(changes)
35}