Skip to main content

CompatReport

Struct CompatReport 

Source
pub struct CompatReport {
    pub forward_compatible: bool,
    pub backward_compatible: bool,
    pub issues: Vec<CompatIssue>,
}
Expand description

The full result of a schema diff.

Fields§

§forward_compatible: bool

Whether an old reader can consume data produced by the new schema.

§backward_compatible: bool

Whether a new reader can consume data produced by the old schema.

§issues: Vec<CompatIssue>

Every detected change.

Implementations§

Source§

impl CompatReport

Source

pub fn is_compatible(&self) -> bool

No Critical issues (warnings and notes are tolerated).

Examples found in repository?
examples/contract_engine.rs (line 102)
54fn main() -> nextjson::Result<()> {
55    // 1. 内省编译期 schema。
56    println!("== 1. 编译期 schema ==");
57    println!("{:#?}", schema_of::<Customer>());
58
59    // 2. 导出 JSON Schema(draft-07),可直接交给前端 / OpenAPI / 校验工具。
60    println!("\n== 2. JSON Schema 导出 ==");
61    let json_schema = to_json_schema::<Customer>();
62    println!("{}", to_string_pretty(&json_schema)?);
63
64    // 3. 合规载荷:校验必须零违规。
65    println!("\n== 3. 校验:合规载荷 ==");
66    let good: Customer = from_str(
67        r#"{"name":"Ada Lovelace","loyalty_points":150,"tags":["vip","analyst"],"api_key":"sk-live-abc"}"#,
68    )?;
69    let report = validate_value::<Customer>(&to_value(&good)?);
70    println!(
71        "violations = {}, is_ok = {}",
72        report.violations.len(),
73        report.is_ok()
74    );
75
76    // 4. 敌意载荷:越界字符串、越界整数、越界数组、未知字段——全部被捕获。
77    println!("\n== 4. 校验:敌意载荷 ==");
78    let bad = json!({
79        "name": "a very long name exceeding the declared maximum of thirty-two scalars",
80        "loyalty_points": 9999,
81        "tags": ["t1","t2","t3","t4","t5","t6","t7","t8","t9","t10","t11","t12"],
82        "api_key": "sk-live-secret",
83        "hacker_field": "unknown",
84    });
85    let report = validate_value::<Customer>(&bad);
86    for violation in &report.violations {
87        println!("  violation @ {:?}: {:?}", violation.path, violation.kind);
88    }
89    // sensitive 只出现在脱敏清单里,不进 violations。
90    println!("  敏感路径(用于脱敏): {:?}", report.sensitive_paths());
91
92    // 5. 版本兼容性:编译期 diff,发布前拦截破坏性变更。
93    println!("\n== 5. 版本兼容性:Customer -> CustomerV2 ==");
94    let compat = check_between::<Customer, CustomerV2>();
95    println!(
96        "forward_compatible = {} (旧 reader 能读新数据), backward_compatible = {} (新 reader 能读旧数据)",
97        compat.forward_compatible, compat.backward_compatible
98    );
99    for issue in &compat.issues {
100        println!("  [{:?}] {}: {}", issue.severity, issue.path, issue.message);
101    }
102    assert!(!compat.is_compatible(), "加必填字段必须被判定为不兼容");
103
104    Ok(())
105}
Source

pub fn worst_severity(&self) -> Option<Severity>

The most severe issue, if any.

Trait Implementations§

Source§

impl Clone for CompatReport

Source§

fn clone(&self) -> CompatReport

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CompatReport

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for CompatReport

Source§

impl PartialEq for CompatReport

Source§

fn eq(&self, other: &CompatReport) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for CompatReport

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.