New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validating JSON is not working #62
Comments
Just a fast look, you should provide a custom MonacoProviderService like https://github.com/alauda/ng-monaco-editor#load-ng-monaco-editor-module Just extend it and in async initMonaco() {
const monaco = await super.initMonaco()
// do what you want
// don't forget to return `monaco`
return monaco
} |
Thanks for the blazing fast response @JounQin! Indeed moving the code after // app.module.ts
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { MonacoEditorModule, MonacoProviderService } from 'ng-monaco-editor';
import { AppComponent } from './app.component';
import { CustomMonacoProviderService } from './CustomMonacoProviderService';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
MonacoEditorModule.forRoot({
baseUrl: 'lib',
defaultOptions: {},
}),
],
exports: [
MonacoEditorModule
],
providers: [
{
provide: MonacoProviderService,
useClass: CustomMonacoProviderService,
},
],
bootstrap: [AppComponent]
})
export class AppModule { } and import { MonacoProviderService } from "ng-monaco-editor";
export class CustomMonacoProviderService extends MonacoProviderService {
async initMonaco() {
const monaco = await super.initMonaco();
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
validate: true,
schemas: [{
uri: "http://myserver/foo-schema.json",
fileMatch: ["a://b/foo.json"],
schema: {
type: "object",
properties: {
p1: {
enum: ["v1", "v2"]
},
p2: {
enum: ["x1", "x2"]
}
}
}
}],
});
return monaco;
}
} |
After some further investigation, it seems that settings jsonDefaults diagnostics options indeed works in So, (without the suggested custom loading), this works: import { Component } from '@angular/core';
import * as monaco from "monaco-editor";
@Component({
selector: 'app-root',
styleUrls: ['./app.component.css'],
template:`
<ng-monaco-editor
style="height: 300px"
[modelUri]="modelUri"
[(ngModel)]="jsonCode"
[options]="options"
(editorChange)="editorChange($event)"
></ng-monaco-editor>`,
})
export class AppComponent {
// ...
editorChange(editor: monaco.editor.IStandaloneCodeEditor): void {
(window as any).monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
validate: true,
schemas: [{
uri: "http://myserver/foo-schema.json",
fileMatch: [this.modelUri],
schema: {
type: "object",
properties: {
p1: {
enum: ["v1", "v2"]
},
p2: {
enum: ["x1", "x2"]
}
}
}
}],
});
}
} Note that I obtain the
Also there is a related change in |
You should not load If you want to use If you want to use For typings like |
For both import { Component, OnInit } from '@angular/core'
import { MonacoProviderService } from 'ng-monaco-editor'
@Component({
selector: 'app-root',
styleUrls: ['./app.component.css'],
template: `
<ng-monaco-editor
style="height: 300px"
[modelUri]="modelUri"
[(ngModel)]="jsonCode"
[options]="options"
></ng-monaco-editor>
`,
})
export class AppComponent implements OnInit {
jsonCode = ['{', ' "p1": "v3",', ' "p2": false', '}'].join('\n')
options = {
language: 'json',
}
modelUri = 'a://b/foo.json'
constructor(private readonly monacoProvider: MonacoProviderService) {}
async ngOnInit() {
const monaco = await this.monacoProvider.initMonaco()
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
validate: true,
schemas: [
{
uri: 'http://myserver/foo-schema.json',
fileMatch: [this.modelUri],
schema: {
type: 'object',
properties: {
p1: {
enum: ['v1', 'v2'],
},
p2: {
enum: ['x1', 'x2'],
},
},
},
},
],
})
}
} |
Maybe it's worth to notice this usage into README. |
That's exactly what I was looking for. I will try to create a PR in the readme as well. |
In order to use
monaco-editor
as JSON editor which validates the content of the JSON against a schema, we need a to callmonaco.languages.json.jsonDefaults.setDiagnosticsOptions
, e.g. like in the Configure JSON defaults example in the monaco editor playground.It seems there is no good place to call this method though - if I correctly get the API, the only place where this could be done (where the monaco code will be loaded) is in the
editorChange
event handler:but for some reason the editor is not validating against the schema. Am I calling it in the wrong place (I can see that similar libraries have exposed
monacoLoad
method or similar which is called when the module is fully loaded), or is there a better approach?I prepared a runnable repo here, the problematic code is in
app.component.ts
.The text was updated successfully, but these errors were encountered: