@openfga/frontend-utils
Version:
Exposes helpful utilities for building authoring experiences of OpenFGA Models.
55 lines (54 loc) • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateDSL = validateDSL;
const errors_1 = require("@openfga/syntax-transformer/dist/errors");
const syntax_transformer_1 = require("@openfga/syntax-transformer");
function validateDSL(monaco, dsl) {
var _a, _b, _c, _d;
const markers = [];
try {
syntax_transformer_1.validator.validateDSL(dsl);
}
catch (err) {
if (!err.errors) {
return markers;
}
for (const singleErr of err.errors) {
let source;
if (singleErr instanceof errors_1.DSLSyntaxSingleError) {
source = "SyntaxError";
}
else if (singleErr instanceof errors_1.ModelValidationSingleError) {
source = "ModelValidationError";
}
else {
throw new Error("Unhandled Exception: " + JSON.stringify(singleErr, null, 4));
}
const extraInformation = {};
const errorMetadata = singleErr.metadata;
if (errorMetadata) {
if ("errorType" in errorMetadata) {
extraInformation.error = errorMetadata.errorType;
}
["typeName", "relation"].forEach((field) => {
if (field in errorMetadata) {
extraInformation[field] = errorMetadata[field];
}
});
}
// monaco starts range of line & col from position 1
// https://microsoft.github.io/monaco-editor/typedoc/classes/Selection.html#startLineNumber
markers.push({
message: singleErr.msg,
severity: monaco.MarkerSeverity.Error,
startColumn: (((_a = singleErr === null || singleErr === void 0 ? void 0 : singleErr.column) === null || _a === void 0 ? void 0 : _a.start) || 0) + 1,
endColumn: (((_b = singleErr === null || singleErr === void 0 ? void 0 : singleErr.column) === null || _b === void 0 ? void 0 : _b.end) || 0) + 1,
startLineNumber: (((_c = singleErr === null || singleErr === void 0 ? void 0 : singleErr.line) === null || _c === void 0 ? void 0 : _c.start) || 0) + 1,
endLineNumber: (((_d = singleErr === null || singleErr === void 0 ? void 0 : singleErr.line) === null || _d === void 0 ? void 0 : _d.end) || 0) + 1,
source,
extraInformation,
});
}
}
return markers;
}