Skip to content

Commit 8dba94a

Browse files
committed
fix: prevent crash on invalid URI during JSON import (#561)
1 parent ee542bd commit 8dba94a

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/utils/safeFormats.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Custom JSON Schema format validator for URIs.
3+
*
4+
* @param {string} input The string to validate.
5+
* @returns {boolean} True if the input is a valid URI or an empty string, false otherwise.
6+
*/
7+
export function safeUriValidator(input) {
8+
if (input === "") {
9+
return true;
10+
}
11+
12+
// Trim whitespace from the input.
13+
const trimmedInput = input.trim();
14+
15+
try {
16+
// eslint-disable-next-line no-new
17+
new URL(trimmedInput);
18+
return true;
19+
} catch (e) {
20+
return false;
21+
}
22+
}

src/utils/validateSchema.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { Validator } from "jsonschema";
22
import { ddbSchema, jsonSchema } from "../data/schemas";
3+
import { safeUriValidator } from "./safeFormats";
4+
5+
const validator = new Validator();
6+
validator.customFormats.uri = safeUriValidator;
37

48
export function jsonDiagramIsValid(obj) {
5-
return new Validator().validate(obj, jsonSchema).valid;
9+
return validator.validate(obj, jsonSchema).valid;
610
}
711

812
export function ddbDiagramIsValid(obj) {
9-
return new Validator().validate(obj, ddbSchema).valid;
13+
return validator.validate(obj, ddbSchema).valid;
1014
}

0 commit comments

Comments
 (0)