From 6e3f9c78328c313a77d819ce5694428004e099fa Mon Sep 17 00:00:00 2001 From: Samuel Imolorhe Date: Tue, 2 Jul 2024 19:03:05 +0200 Subject: [PATCH 1/2] added limited support for conditional property values --- .../__tests__/__fixtures__/schemas.ts | 14 +++++++- .../__tests__/json-completion.spec.ts | 18 ++++++++++ src/features/completion.ts | 35 +++++++++++++++++-- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/features/__tests__/__fixtures__/schemas.ts b/src/features/__tests__/__fixtures__/schemas.ts index e6ba10b..f0f416c 100644 --- a/src/features/__tests__/__fixtures__/schemas.ts +++ b/src/features/__tests__/__fixtures__/schemas.ts @@ -140,7 +140,7 @@ export const testSchemaConditionalProperties = { properties: { type: { type: "string", - enum: ["Test_1", "Test_2"], + enum: ["Test_1", "Test_2", "Test_3"], }, props: { type: "object", @@ -181,5 +181,17 @@ export const testSchemaConditionalProperties = { }, }, }, + { + if: { + properties: { + type: { const: "Test_3" }, + }, + }, + then: { + properties: { + props: { type: "string", enum: ["ace", "abu", "bay"] }, + }, + }, + }, ], } as JSONSchema7; diff --git a/src/features/__tests__/json-completion.spec.ts b/src/features/__tests__/json-completion.spec.ts index 6be53bd..8c4aacf 100644 --- a/src/features/__tests__/json-completion.spec.ts +++ b/src/features/__tests__/json-completion.spec.ts @@ -409,6 +409,24 @@ describe.each([ ], schema: testSchemaConditionalProperties, }, + { + name: "autocomplete for a schema with conditional property values", + mode: MODES.JSON, + docs: ['{ "type": "Test_3", "props": "a|"'], + expectedResults: [ + { + type: "string", + label: "ace", + apply: '"ace"', + }, + { + type: "string", + label: "abu", + apply: '"abu"', + }, + ], + schema: testSchemaConditionalProperties, + }, // JSON5 { name: "return bare property key when no quotes are used", diff --git a/src/features/completion.ts b/src/features/completion.ts index 9c5f2d1..f73efd3 100644 --- a/src/features/completion.ts +++ b/src/features/completion.ts @@ -837,19 +837,50 @@ export class JSONCompletion { if ( !subSchema || subSchema.name === "UnknownPropertyError" || + // TODO: Looking into why this is needed subSchema.enum || subSchema.type === "undefined" || subSchema.type === "null" ) { + debug.log( + "xxx", + "no schema for pointer. Try parent pointer..", + pointer, + subSchema + ); pointer = pointer.replace(/\/[^/]*$/, "/"); - subSchema = draft.getSchema({ pointer }); + pointer = "/" ? "" : pointer; // root pointer should be empty ("/" is a JSON pointer for an empty string key) + subSchema = draft.getSchema({ pointer, data: data ?? undefined }); + debug.log( + "xxx", + "subSchema for parent pointer", + subSchema, + pointer, + data + ); + // resolve all direct child schemas + // TODO: This is a bit hacky, but it works for now + if (subSchema?.properties) { + Object.entries(subSchema.properties).forEach(([key, value]) => { + if (subSchema?.properties && typeof value === "object") { + subSchema.properties[key] = { + ...value, + ...draft.getSchema({ + pointer: pointer + "/" + key, + data: data ?? undefined, + }), + }; + } + }); + debug.log("xxx", "direct children resolved", subSchema); + } } debug.log("xxx", "pointer..", JSON.stringify(pointer)); // For some reason, it returns undefined schema for the root pointer // We use the root schema in that case as the relevant (sub)schema - if (!pointer || pointer === "/") { + if (!subSchema && (!pointer || pointer === "/")) { subSchema = this.expandSchemaProperty(schema, schema) ?? schema; } // const subSchema = new Draft07(this.schema).getSchema(pointer); From fee04cee49f79cfa97de47403e845baf18b93ab0 Mon Sep 17 00:00:00 2001 From: Samuel Date: Tue, 2 Jul 2024 19:27:53 +0200 Subject: [PATCH 2/2] Create nine-timers-develop.md --- .changeset/nine-timers-develop.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/nine-timers-develop.md diff --git a/.changeset/nine-timers-develop.md b/.changeset/nine-timers-develop.md new file mode 100644 index 0000000..32518d4 --- /dev/null +++ b/.changeset/nine-timers-develop.md @@ -0,0 +1,5 @@ +--- +"codemirror-json-schema": patch +--- + +added limited support for conditional property values