Skip to content

Commit cbdf1a2

Browse files
committed
fix: validate JSON inputs in JSONTools component before diffing
- Added checks to ensure both JSON inputs are objects or arrays before processing. - Displayed an error message if inputs are invalid, improving user feedback and preventing runtime errors.
1 parent 837cf14 commit cbdf1a2

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/pages/JSONTools.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,12 @@ export default function JSONTools() {
128128
try {
129129
const left = safeParseJson(leftJson)
130130
const right = safeParseJson(rightJson)
131-
const changes = diffJson(left, right)
131+
// diffJson expects string | object, ensure parsed values are objects
132+
if (typeof left !== 'object' || left === null || typeof right !== 'object' || right === null) {
133+
toast.error('Both JSON inputs must be objects or arrays')
134+
return
135+
}
136+
const changes = diffJson(left as object, right as object)
132137
setDiffResult(changes)
133138
toast.success('Diff generated')
134139
} catch (err) {

0 commit comments

Comments
 (0)