Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/generators/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,5 @@ export const generateJsonSchema = (

const gen = new Gen(findOption);

return gen.schemaObject(instance);
return gen.property(instance);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No tests cover the new behavior. All existing tests in schema.test.ts pass objects. Tests for generateJsonSchema("hello"), generateJsonSchema(42), generateJsonSchema([1,2]) etc. are needed.

};
1 change: 0 additions & 1 deletion packages/vue-vuetify/dev/views/ExampleView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ const onChange = (event: JsonFormsChangeEvent): void => {
monaco.Uri.parse(toDataUri(props.example.name)),
event.data !== undefined ? JSON.stringify(event.data, null, 2) : '',
);
state.data = event.data;
}
errors.value = event.errors;
};
Expand Down
55 changes: 26 additions & 29 deletions packages/vue/src/components/JsonForms.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,24 @@ import {
defaultMiddleware,
Middleware,
JsonFormsSubStates,
Layout,
} from '@jsonforms/core';
import { JsonFormsChangeEvent, MaybeReadonly } from '../types';
import DispatchRenderer from './DispatchRenderer.vue';

import type Ajv from 'ajv';
import type { ErrorObject } from 'ajv';

// TODO fix @typescript-eslint/ban-types
// eslint-disable-next-line @typescript-eslint/ban-types
const isObject = (elem: any): elem is Object => {
return elem && typeof elem === 'object';
};

const EMPTY: ErrorObject[] = reactive([]);

const createDefaultLayout = (): Layout => ({
type: 'VerticalLayout',
elements: [],
});
const generateUISchema = (schema: JsonSchema) =>
Generate.uiSchema(schema, undefined, undefined, schema) ??
createDefaultLayout();

export default defineComponent({
name: 'JsonForms',
components: {
Expand Down Expand Up @@ -123,13 +126,10 @@ export default defineComponent({
},
emits: ['change'],
data() {
const dataToUse = this.data;
const generatorData = isObject(dataToUse) ? dataToUse : {};
const dataToUse = this.data === undefined ? {} : this.data;
const schemaToUse: JsonSchema =
this.schema ?? Generate.jsonSchema(generatorData);
const uischemaToUse =
this.uischema ??
Generate.uiSchema(schemaToUse, undefined, undefined, schemaToUse);
this.schema ?? Generate.jsonSchema(dataToUse);
const uischemaToUse = this.uischema ?? generateUISchema(schemaToUse);
const initCore = (): JsonFormsCore => {
const initialCore = {
data: dataToUse,
Expand Down Expand Up @@ -189,29 +189,23 @@ export default defineComponent({
},
watch: {
schema(newSchema) {
const generatorData = isObject(this.data) ? this.data : {};
this.schemaToUse = newSchema ?? Generate.jsonSchema(generatorData);
this.schemaToUse = newSchema ?? Generate.jsonSchema(this.dataToUse);
if (!this.uischema) {
this.uischemaToUse = Generate.uiSchema(
this.schemaToUse,
undefined,
undefined,
this.schemaToUse
);
this.uischemaToUse = generateUISchema(this.schemaToUse);
}
},
uischema(newUischema) {
this.uischemaToUse =
newUischema ??
Generate.uiSchema(
this.schemaToUse,
undefined,
undefined,
this.schemaToUse
);
this.uischemaToUse = newUischema ?? generateUISchema(this.schemaToUse);
},
data(newData) {
this.dataToUse = newData;
this.dataToUse = newData === undefined ? {} : newData;

if (!this.schema) {
Comment on lines 200 to +203
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combined with the other change sof the PR, this means that on every user edit (in schema-less mode) the schema and uischema are regenerated from the core's data, which is expensive and likely unnecessary since the shape of the data typically doesn't change between edits the user can make through JSON Forms

We need to differentiate between outside and user based changes and only regenerate on outside changes.

this.schemaToUse = Generate.jsonSchema(this.dataToUse);
if (!this.uischema) {
this.uischemaToUse = generateUISchema(this.schemaToUse);
}
}
},
renderers(newRenderers) {
this.jsonforms.renderers = newRenderers;
Expand Down Expand Up @@ -251,6 +245,9 @@ export default defineComponent({
);
},
eventToEmit(newEvent) {
// update the data so if we change the additionalErrors this won't reset the form data
this.dataToUse = newEvent.data;

this.$emit('change', newEvent);
},
i18n: {
Expand Down
Loading