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: boolWhether an old reader can consume data produced by the new schema.
backward_compatible: boolWhether a new reader can consume data produced by the old schema.
issues: Vec<CompatIssue>Every detected change.
Implementations§
Source§impl CompatReport
impl CompatReport
Sourcepub fn is_compatible(&self) -> bool
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}Sourcepub fn worst_severity(&self) -> Option<Severity>
pub fn worst_severity(&self) -> Option<Severity>
The most severe issue, if any.
Trait Implementations§
Source§impl Clone for CompatReport
impl Clone for CompatReport
Source§fn clone(&self) -> CompatReport
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for CompatReport
impl Debug for CompatReport
impl Eq for CompatReport
Source§impl PartialEq for CompatReport
impl PartialEq for CompatReport
impl StructuralPartialEq for CompatReport
Auto Trait Implementations§
impl Freeze for CompatReport
impl RefUnwindSafe for CompatReport
impl Send for CompatReport
impl Sync for CompatReport
impl Unpin for CompatReport
impl UnsafeUnpin for CompatReport
impl UnwindSafe for CompatReport
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more