Fix clipboard paste validation accepting invalid notes #706
+114
−16
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pasting arbitrary JSON previously created broken “note” entities because the clipboard importer treated every validation result as truthy. That produced orphaned notes, NaN positions, and cascading save/export failures. This change adds a shared payload classifier so only valid tables/areas/notes are recreated.
Steps to Reproduce
•Copy { "foo": "bar" }.
•Focus the editor canvas and press Ctrl/Cmd+V.
•Observe a blank note with bad coordinates; subsequent saves or exports often error.
Root Cause
ControlPanel.paste() called validator.validate(obj, noteSchema) and used the returned object directly in the if condition. Because the result object is always truthy, the “note” branch ran for any JSON payload, regardless of validity.
Fix
•Introduced classifyClipboardPayload() in src/utils/clipboard.js that checks tables, areas, and notes against their jsonschema definitions and returns null when no schema matches.
•Updated ControlPanel.paste() to consume the classifier and keep the existing offset/id behavior only when the payload is valid. Invalid data is now ignored safely.
•Added tests/clipboard.test.js using Node’s native test runner plus an npm run test script to prevent regressions.
Before / After
•Before: Any JSON blob (or even plain text that parses) spawned a corrupt note.
•After: Only valid tables/areas/notes are pasted; other payloads are ignored without breaking the diagram.
Breaking Changes
None.
Tests
•npm run test (covers the new clipboard classifier)
Checklist
[x] Code formatted and linted locally (run npm run lint after npm install)
[x] Tests added/updated (tests/clipboard.test.js)
[x] Build verified (npm run build pending local install)
[x] No strings or locales changed
[x] No unrelated refactors