Skip to content

Commit 32faf9a

Browse files
fix(vue-vuetify): reuse provided ajv instance (#2602)
- Reuse the same AJV instance throughout JSON Forms in vue-vuetify to ensure that user configuration of AJV is retained. - add test that `additionalProperties` reuses the parent form's AJV
1 parent 57ec64e commit 32faf9a

4 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎packages/vue-vuetify/src/complex/components/AdditionalProperties.vue‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ import {
109109
import {
110110
DispatchRenderer,
111111
JsonForms,
112+
useAjv,
112113
useJsonForms,
113114
useJsonFormsControlWithDetail,
114115
useTranslator,
@@ -410,8 +411,8 @@ export default defineComponent({
410411
validationMode: parentValidationMode,
411412
i18n,
412413
middleware,
413-
ajv,
414414
} = useJsonForms();
415+
const ajv = useAjv();
415416
416417
// if the new property name is not specified then hide any errors
417418
const validationMode = computed(() =>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { describe, it, expect, beforeEach } from 'vitest';
2+
import { clearAllIds, createAjv } from '@jsonforms/core';
3+
import { markRaw } from 'vue';
4+
import { extendedVuetifyRenderers } from '../../../src';
5+
import { mountJsonForms } from '../util/util';
6+
7+
// Check that `additionalProperties` reuses the parent form's AJV
8+
describe('AdditionalProperties nested AJV', () => {
9+
// A map whose key `pattern` is legal only without the `u` flag
10+
const schema = {
11+
type: 'object' as const,
12+
properties: {
13+
secretFiles: {
14+
type: 'object' as const,
15+
additionalProperties: { type: 'string' as const },
16+
propertyNames: {
17+
pattern: '^"([^"$\\\\]|\\$(?!{)|\\\\.)*"$',
18+
},
19+
},
20+
},
21+
};
22+
const uischema = { type: 'Control' as const, scope: '#' };
23+
24+
beforeEach(() => {
25+
clearAllIds();
26+
});
27+
28+
it('mounts a map whose key pattern is only valid without the `u` flag', () => {
29+
// A parent form configured with `unicodeRegExp: false`
30+
const ajv = markRaw(createAjv({ unicodeRegExp: false }));
31+
expect(() =>
32+
mountJsonForms(
33+
{ secretFiles: {} },
34+
schema,
35+
extendedVuetifyRenderers,
36+
uischema,
37+
undefined,
38+
undefined,
39+
ajv,
40+
),
41+
).not.toThrow();
42+
});
43+
});

‎packages/vue-vuetify/tests/unit/util/TestComponent.vue‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
:uischema="uischema"
77
:renderers="renderers"
88
:config="config"
9+
:ajv="ajv"
910
:i18n="i18n"
1011
@change="onChange"
1112
/>

‎packages/vue-vuetify/tests/unit/util/util.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
JsonSchema,
55
UISchemaElement,
66
} from '@jsonforms/core';
7+
import type Ajv from 'ajv';
78
import { mount } from '@vue/test-utils';
89
import TestComponent from './TestComponent.vue';
910
import { createVuetify } from 'vuetify';
@@ -25,6 +26,7 @@ export const mountJsonForms = (
2526
uischema?: UISchemaElement,
2627
config?: any,
2728
i18n?: JsonFormsI18nState,
29+
ajv?: Ajv,
2830
) => {
2931
return mount(TestComponent, {
3032
global: {
@@ -37,6 +39,7 @@ export const mountJsonForms = (
3739
config,
3840
renderers: markRaw(renderers),
3941
i18n,
42+
ajv,
4043
},
4144
attachTo: document.body,
4245
});

0 commit comments

Comments
 (0)