-
Notifications
You must be signed in to change notification settings - Fork 7
/
validate.ts
55 lines (49 loc) · 1.66 KB
/
validate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type * as MonacoEditor from "monaco-editor";
import { Marker } from "./typings";
import {
DSLSyntaxError,
DSLSyntaxSingleError,
ModelValidationError,
ModelValidationSingleError,
} from "@openfga/syntax-transformer/dist/errors";
import { validator } from "@openfga/syntax-transformer";
export function validateDSL(monaco: typeof MonacoEditor, dsl: string): Marker[] {
const markers: Marker[] = [];
try {
validator.validateDSL(dsl);
} catch (err) {
for (const singleErr of (err as DSLSyntaxError | ModelValidationError).errors) {
let source;
if (singleErr instanceof DSLSyntaxSingleError) {
source = "SyntaxError";
} else if (singleErr instanceof ModelValidationSingleError) {
source = "ModelValidationError";
} else {
throw new Error("Unhandled Exception: " + JSON.stringify(singleErr, null, 4));
}
const extraInformation: Marker["extraInformation"] = {};
const errorMetadata = singleErr.metadata;
if (errorMetadata) {
if ("errorType" in errorMetadata) {
extraInformation.error = errorMetadata.errorType;
}
["typeName", "relation"].forEach((field) => {
if (field in errorMetadata) {
(extraInformation as any)[field] = (errorMetadata as any)[field];
}
});
}
markers.push({
message: singleErr.msg,
severity: monaco.MarkerSeverity.Error,
startColumn: singleErr.column.start,
endColumn: singleErr.column.end,
startLineNumber: singleErr.line.start,
endLineNumber: singleErr.line.end,
source,
extraInformation,
});
}
}
return markers;
}