diff --git a/src/utils/safeFormats.js b/src/utils/safeFormats.js new file mode 100644 index 00000000..a7b16ef2 --- /dev/null +++ b/src/utils/safeFormats.js @@ -0,0 +1,22 @@ +/** + * Custom JSON Schema format validator for URIs. + * + * @param {string} input The string to validate. + * @returns {boolean} True if the input is a valid URI or an empty string, false otherwise. + */ +export function safeUriValidator(input) { + if (input === "") { + return true; + } + + // Trim whitespace from the input. + const trimmedInput = input.trim(); + + try { + // eslint-disable-next-line no-new + new URL(trimmedInput); + return true; + } catch (e) { + return false; + } +} diff --git a/src/utils/validateSchema.js b/src/utils/validateSchema.js index af0b2879..8d530d32 100644 --- a/src/utils/validateSchema.js +++ b/src/utils/validateSchema.js @@ -1,10 +1,14 @@ import { Validator } from "jsonschema"; import { ddbSchema, jsonSchema } from "../data/schemas"; +import { safeUriValidator } from "./safeFormats"; + +const validator = new Validator(); +validator.customFormats.uri = safeUriValidator; export function jsonDiagramIsValid(obj) { - return new Validator().validate(obj, jsonSchema).valid; + return validator.validate(obj, jsonSchema).valid; } export function ddbDiagramIsValid(obj) { - return new Validator().validate(obj, ddbSchema).valid; + return validator.validate(obj, ddbSchema).valid; }