Skip to content

Commit 5602557

Browse files
LukasBollsdirix
authored andcommitted
fix(core): Disable schema caching in AJV instance
Set the 'addUsedSchema' option to false during AJV instantiation to prevent schema caching by default. This resolves issues where updating a schema that contains an ID leads to duplicate ID errors in AJV. Closes #2071
1 parent 7ddeb71 commit 5602557

File tree

2 files changed

+4
-20
lines changed

2 files changed

+4
-20
lines changed

packages/core/src/reducers/core.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,6 @@ const initState: JsonFormsCore = {
9090
additionalErrors: [],
9191
};
9292

93-
const reuseAjvForSchema = (ajv: Ajv, schema: JsonSchema): Ajv => {
94-
if (
95-
Object.prototype.hasOwnProperty.call(schema, 'id') ||
96-
Object.prototype.hasOwnProperty.call(schema, '$id')
97-
) {
98-
ajv.removeSchema(schema);
99-
}
100-
return ajv;
101-
};
102-
10393
const getOrCreateAjv = (
10494
state: JsonFormsCore,
10595
action?: InitAction | UpdateCoreAction
@@ -115,12 +105,7 @@ const getOrCreateAjv = (
115105
}
116106
}
117107
}
118-
if (state.ajv) {
119-
return action?.schema
120-
? reuseAjvForSchema(state.ajv, action.schema)
121-
: state.ajv;
122-
}
123-
return createAjv();
108+
return state.ajv ? state.ajv : createAjv();
124109
};
125110

126111
const hasAjvOption = (option: any): option is InitActionOptions => {
@@ -255,7 +240,7 @@ export const coreReducer: Reducer<JsonFormsCore, CoreActions> = (
255240
const needsNewValidator =
256241
action.schema && state.ajv && state.validationMode !== 'NoValidation';
257242
const v = needsNewValidator
258-
? reuseAjvForSchema(state.ajv, action.schema).compile(action.schema)
243+
? state.ajv.compile(action.schema)
259244
: state.validator;
260245
const errors = validate(v, state.data);
261246
return {
@@ -326,9 +311,7 @@ export const coreReducer: Reducer<JsonFormsCore, CoreActions> = (
326311
};
327312
}
328313
if (state.validationMode === 'NoValidation') {
329-
const validator = reuseAjvForSchema(state.ajv, state.schema).compile(
330-
state.schema
331-
);
314+
const validator = state.ajv.compile(state.schema);
332315
const errors = validate(validator, state.data);
333316
return {
334317
...state,

packages/core/src/util/validator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const createAjv = (options?: Options) => {
3131
allErrors: true,
3232
verbose: true,
3333
strict: false,
34+
addUsedSchema: false,
3435
...options,
3536
});
3637
addFormats(ajv);

0 commit comments

Comments
 (0)