Skip to content

feat: Support format #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/diff_walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,37 @@ impl<F: FnMut(Change)> DiffWalker<F> {
Ok(())
}

fn diff_format(&mut self, json_path: &str, lhs: &mut SchemaObject, rhs: &mut SchemaObject) {
match (&lhs.format, &rhs.format) {
(Some(lhs_fmt), Some(rhs_fmt)) if lhs_fmt != rhs_fmt => {
(self.cb)(Change {
path: json_path.to_owned(),
change: ChangeKind::FormatChange {
old_format: lhs_fmt.clone(),
new_format: rhs_fmt.clone(),
},
});
}
(Some(removed_fmt), None) => {
(self.cb)(Change {
path: json_path.to_owned(),
change: ChangeKind::FormatRemove {
removed: removed_fmt.clone(),
},
});
}
(None, Some(added_fmt)) => {
(self.cb)(Change {
path: json_path.to_owned(),
change: ChangeKind::FormatAdd {
added: added_fmt.clone(),
},
});
}
_ => {} // No change or both None
}
}

fn resolve_references(
&self,
lhs: &mut SchemaObject,
Expand Down Expand Up @@ -464,6 +495,7 @@ impl<F: FnMut(Change)> DiffWalker<F> {
self.diff_instance_types(json_path, lhs, rhs);
}
self.diff_const(json_path, lhs, rhs);
self.diff_format(json_path, lhs, rhs);
// If we split the types, we don't want to compare type-specific properties
// because they are already compared in the `Self::diff_any_of`
if !is_lhs_split && !is_rhs_split {
Expand Down
20 changes: 20 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,23 @@ pub enum ChangeKind {
/// The property that is now required
property: String,
},
/// A format constraint has been added.
FormatAdd {
/// The format that was added.
added: String,
},
/// A format constraint has been removed.
FormatRemove {
/// The format that was removed.
removed: String,
},
/// A format constraint has been changed.
FormatChange {
/// The old format value.
old_format: String,
/// The new format value.
new_format: String,
},
}

impl ChangeKind {
Expand Down Expand Up @@ -150,6 +167,9 @@ impl ChangeKind {
Self::TupleChange { .. } => true,
Self::RequiredRemove { .. } => false,
Self::RequiredAdd { .. } => true,
Self::FormatAdd { .. } => true,
Self::FormatRemove { .. } => false,
Self::FormatChange { .. } => true,
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/format/format_add.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"lhs": { "type": "string" },
"rhs": { "type": "string", "format": "email" }
}
4 changes: 4 additions & 0 deletions tests/fixtures/format/format_change.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"lhs": { "type": "string", "format": "uuid" },
"rhs": { "type": "string", "format": "date-time" }
}
4 changes: 4 additions & 0 deletions tests/fixtures/format/format_remove.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"lhs": { "type": "string", "format": "uri" },
"rhs": { "type": "string" }
}
4 changes: 4 additions & 0 deletions tests/fixtures/format/format_unchanged.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"lhs": { "type": "string", "format": "date" },
"rhs": { "type": "string", "format": "date" }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: tests/test.rs
assertion_line: 12
expression: diff
info:
lhs:
type: string
rhs:
format: email
type: string
input_file: tests/fixtures/format/format_add.json
---
[
Change {
path: "",
change: FormatAdd {
added: "email",
},
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: tests/test.rs
assertion_line: 12
expression: diff
info:
lhs:
format: uuid
type: string
rhs:
format: date-time
type: string
input_file: tests/fixtures/format/format_change.json
---
[
Change {
path: "",
change: FormatChange {
old_format: "uuid",
new_format: "date-time",
},
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
source: tests/test.rs
assertion_line: 12
expression: diff
info:
lhs:
format: uri
type: string
rhs:
type: string
input_file: tests/fixtures/format/format_remove.json
---
[
Change {
path: "",
change: FormatRemove {
removed: "uri",
},
},
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: tests/test.rs
assertion_line: 12
expression: diff
info:
lhs:
format: date
type: string
rhs:
format: date
type: string
input_file: tests/fixtures/format/format_unchanged.json
---
[]
Loading