Editor allows trailing commas in JSON
Describe the bug
The editor does not report trailing commas in a JSON format as errors. The error should be flagged as Trailing comma json (519) but this does not happen.
To Reproduce Use the editor as follows:
<Editor language="json"/>
With this setup everything works as expected except the trailing comma issue:
Additional context
I am using the editor in a NextJS application.
All other functionalities work as expected.
I tried playing with the options (such as: trailingCommas: "error") but without any luck.
Thanks in advance
I believe this is not something that this package controls directly but rather the default 'json' implementation of the Monaco editor itself. The good news is, using this package, we can pass specific diagnostic settings to Monaco.
I've had luck with this by doing the following:
import { useMonaco } from '@monaco-editor/react';
then, in your component:
const monaco = useMonaco();
useEffect(() => {
monaco &&
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
...monaco.languages.json.jsonDefaults.diagnosticsOptions,
trailingCommas: 'error', // this will show a validation error as desired
comments: 'error', // this disables comments in JSON (not what you asked, but something I find useful)
});
}, [monaco]);