Skip to content

Commit

Permalink
feat: Add support for changes to "required" properties (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
lynnagara authored May 8, 2023
1 parent bfdbe82 commit 4a1a838
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/diff_walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,36 @@ impl DiffWalker {
Ok(())
}

fn diff_required(
&mut self,
json_path: &str,
lhs: &mut SchemaObject,
rhs: &mut SchemaObject,
) -> Result<(), Error> {
let lhs_required = &lhs.object().required;
let rhs_required = &rhs.object().required;

for removed in lhs_required.difference(rhs_required) {
self.changes.push(Change {
path: json_path.to_owned(),
change: ChangeKind::RequiredRemove {
property: removed.clone(),
},
});
}

for added in rhs_required.difference(lhs_required) {
self.changes.push(Change {
path: json_path.to_owned(),
change: ChangeKind::RequiredAdd {
property: added.clone(),
},
});
}

Ok(())
}

fn resolve_ref<'a>(root_schema: &'a RootSchema, reference: &str) -> Option<&'a Schema> {
if let Some(definition_name) = reference.strip_prefix("#/definitions/") {
let schema_object = root_schema.definitions.get(definition_name)?;
Expand Down Expand Up @@ -302,6 +332,7 @@ impl DiffWalker {
self.diff_range(json_path, lhs, rhs)?;
self.diff_additional_properties(json_path, lhs, rhs)?;
self.diff_array_items(json_path, lhs, rhs)?;
self.diff_required(json_path, lhs, rhs)?;
Ok(())
}
}
Expand Down
51 changes: 51 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,4 +877,55 @@ mod tests {
@"[]"
);
}

#[test]
fn drop_required() {
let lhs = json! {{
"required": ["value"]
}};
let rhs = json! {{
"required": [],
}};

let diff = diff(lhs, rhs).unwrap();

assert_debug_snapshot!(
diff,
@r###"
[
Change {
path: "",
change: RequiredRemove {
property: "value",
},
},
]
"###
);
}

#[test]
fn add_required() {
let lhs = json! {{
"required": []
}};
let rhs = json! {{
"required": ["value"],
}};

let diff = diff(lhs, rhs).unwrap();
assert_debug_snapshot!(
diff,
@r###"
[
Change {
path: "",
change: RequiredAdd {
property: "value",
},
},
]
"###
);
}
}
12 changes: 12 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ pub enum ChangeKind {
/// The new length of the tuple
new_length: usize,
},
/// A previously required property has been removed
RequiredRemove {
/// The property that is no longer required
property: String,
},
/// A previously optional property has been made required
RequiredAdd {
/// The property that is now required
property: String,
},
}

impl ChangeKind {
Expand Down Expand Up @@ -128,6 +138,8 @@ impl ChangeKind {
Self::TupleToArray { .. } => false,
Self::ArrayToTuple { .. } => true,
Self::TupleChange { .. } => true,
Self::RequiredRemove { .. } => false,
Self::RequiredAdd { .. } => true,
}
}
}
Expand Down

0 comments on commit 4a1a838

Please sign in to comment.