Skip to content

Commit 26ea1b0

Browse files
committed
feat: add xml content type custom validation
1 parent 9bb2e18 commit 26ea1b0

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

package-lock.json

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
"deserialize-error": "0.0.3",
9494
"dompurify": "^2.5.6",
9595
"fast-json-patch": "^3.1.1",
96+
"fast-xml-parser": "^5.2.3",
9697
"graphql": "^15.8.0",
9798
"har-validator": "^5.1.3",
9899
"http-encoding": "^2.0.1",

src/components/editor/monaco.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { defineMonacoThemes } from '../../styles';
66
import { delay } from '../../util/promise';
77
import { asError } from '../../util/error';
88
import { observable, runInAction } from 'mobx';
9+
import { setupXMLValidation } from './xml-validation';
910

1011
export type {
1112
MonacoTypes,
@@ -74,6 +75,8 @@ async function loadMonacoEditor(retries = 5): Promise<void> {
7475
},
7576
});
7677

78+
setupXMLValidation(monaco);
79+
7780
MonacoEditor = rmeModule.default;
7881
} catch (err) {
7982
console.log('Monaco load failed', asError(err).message);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import type * as MonacoTypes from 'monaco-editor'
2+
import { XMLValidator } from 'fast-xml-parser'
3+
4+
export function setupXMLValidation(monaco: typeof MonacoTypes) {
5+
const xmlModels = new Set<MonacoTypes.editor.ITextModel>()
6+
7+
monaco.editor.onWillDisposeModel(model => {
8+
xmlModels.delete(model)
9+
})
10+
11+
monaco.editor.onDidChangeModelLanguage(event => {
12+
const model = event.model
13+
if (model.getModeId() === 'xml' && !xmlModels.has(model)) {
14+
xmlModels.add(model)
15+
model.onDidChangeContent(() => {
16+
const markers: MonacoTypes.editor.IMarkerData[] = []
17+
const text = model.getValue()
18+
19+
if (text.trim()) {
20+
const validationResult = XMLValidator.validate(text, {
21+
allowBooleanAttributes: true,
22+
})
23+
24+
if (validationResult !== true) {
25+
markers.push({
26+
severity: monaco.MarkerSeverity.Error,
27+
startLineNumber: validationResult.err.line,
28+
startColumn: validationResult.err.col,
29+
endLineNumber: validationResult.err.line,
30+
endColumn: validationResult.err.col + 10, // the 10 is totally arbitrary here
31+
message: validationResult.err.msg,
32+
})
33+
}
34+
}
35+
36+
monaco.editor.setModelMarkers(model, 'xml-validation', markers)
37+
})
38+
}
39+
})
40+
}

0 commit comments

Comments
 (0)