Skip to content

Commit 612a38a

Browse files
authored
Add error output to Angular component
- Add possibilities to subscribe to ajv errors - Update check to not reupdate the core on data change
1 parent f40e95b commit 612a38a

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

packages/angular/src/jsonforms-root.component.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges
2828
} from '@angular/core';
2929
import { Actions, JsonFormsRendererRegistryEntry, JsonSchema, UISchemaElement, UISchemaTester, ValidationMode } from '@jsonforms/core';
30-
import { Ajv } from 'ajv';
30+
import { Ajv, ErrorObject } from 'ajv';
3131
import { JsonFormsAngularService, USE_STATE_VALUE } from './jsonforms.service';
3232
@Component({
3333
selector: 'jsonforms',
@@ -47,6 +47,10 @@ export class JsonForms implements OnChanges, OnInit {
4747
@Input() validationMode: ValidationMode;
4848
@Input() ajv: Ajv;
4949
@Input() config: any;
50+
@Output() errors = new EventEmitter<ErrorObject[]>();
51+
52+
private previousData:any;
53+
private previousErrors:ErrorObject[];
5054

5155
private initialized = false;
5256

@@ -70,9 +74,15 @@ export class JsonForms implements OnChanges, OnInit {
7074
});
7175
this.jsonformsService.$state.subscribe(state => {
7276
const data = state?.jsonforms?.core?.data;
73-
if (data !== this.data) {
77+
const errors = state?.jsonforms?.core?.errors;
78+
if(this.previousData !== data) {
79+
this.previousData = data;
7480
this.dataChange.emit(data);
7581
}
82+
if(this.previousErrors !== errors) {
83+
this.previousErrors = errors;
84+
this.errors.emit(errors);
85+
}
7686
});
7787
this.initialized = true;
7888
}

packages/angular/src/jsonforms.service.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,10 @@ export class JsonFormsAngularService {
126126

127127
updateCore<T extends CoreActions>(coreAction: T): T {
128128
const coreState = coreReducer(this._state.core, coreAction);
129-
this._state.core = coreState;
130-
this.updateSubject();
129+
if(coreState !== this._state.core) {
130+
this._state.core = coreState;
131+
this.updateSubject();
132+
}
131133
return coreAction;
132134
}
133135

@@ -156,22 +158,28 @@ export class JsonFormsAngularService {
156158
setUiSchema(uischema: UISchemaElement | undefined): void {
157159
const newUiSchema = uischema ?? generateDefaultUISchema(this._state.core.schema);
158160
const coreState = coreReducer(this._state.core, Actions.updateCore(this._state.core.data, this._state.core.schema, newUiSchema));
159-
this._state.core = coreState;
160-
this.updateSubject();
161+
if(coreState !== this._state.core) {
162+
this._state.core = coreState;
163+
this.updateSubject();
164+
}
161165
}
162166

163167
setSchema(schema: JsonSchema | undefined): void {
164168
const coreState = coreReducer(this._state.core,
165169
Actions.updateCore(this._state.core.data, schema ?? generateJsonSchema(this._state.core.data), this._state.core.uischema)
166170
);
167-
this._state.core = coreState;
168-
this.updateSubject();
171+
if(coreState !== this._state.core) {
172+
this._state.core = coreState;
173+
this.updateSubject();
174+
}
169175
}
170176

171177
setData(data: any): void {
172178
const coreState = coreReducer(this._state.core, Actions.updateCore(data, this._state.core.schema, this._state.core.uischema));
173-
this._state.core = coreState;
174-
this.updateSubject();
179+
if(coreState !== this._state.core) {
180+
this._state.core = coreState;
181+
this.updateSubject();
182+
}
175183
}
176184

177185
setLocale(locale: string): void {

0 commit comments

Comments
 (0)