Skip to content
Open
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
22 changes: 22 additions & 0 deletions src/utils/safeFormats.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
8 changes: 6 additions & 2 deletions src/utils/validateSchema.js
Original file line number Diff line number Diff line change
@@ -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;
}
Loading