From 0dc37498d7276becceb48d92dc367648f4676415 Mon Sep 17 00:00:00 2001 From: Pinghao Wu Date: Sun, 12 May 2024 03:03:37 +0800 Subject: [PATCH] Add support for yaml flow mapping and sequence (#94) To test, copy package.json demo content to package.yaml editor Before: ![20240511-043150](https://github.com/acao/codemirror-json-schema/assets/11596445/95bac852-61cd-4323-900f-ae866e0fdb6e) It does not mark missing name and format errors in .contributors[0] After: ![20240511-043212](https://github.com/acao/codemirror-json-schema/assets/11596445/b452aac1-8a84-49a9-952d-809c17b62a12) Since yaml is a superset of json, I've also make all json validation test cases to be run under yaml mode. --- .changeset/twelve-starfishes-thank.md | 5 +++++ src/__tests__/json-validation.spec.ts | 10 +++++++--- src/constants.ts | 2 ++ 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 .changeset/twelve-starfishes-thank.md diff --git a/.changeset/twelve-starfishes-thank.md b/.changeset/twelve-starfishes-thank.md new file mode 100644 index 0000000..ec0fc2b --- /dev/null +++ b/.changeset/twelve-starfishes-thank.md @@ -0,0 +1,5 @@ +--- +"codemirror-json-schema": patch +--- + +Add support for YAML flow sequences and flow mappings diff --git a/src/__tests__/json-validation.spec.ts b/src/__tests__/json-validation.spec.ts index ad0e458..dc55dda 100644 --- a/src/__tests__/json-validation.spec.ts +++ b/src/__tests__/json-validation.spec.ts @@ -41,7 +41,7 @@ const expectErrors = ( }; describe("json-validation", () => { - it.each([ + const jsonSuite = [ { name: "provide range for a value error", mode: MODES.JSON, @@ -137,6 +137,9 @@ describe("json-validation", () => { ], schema: testSchema2, }, + ]; + it.each([ + ...jsonSuite, // JSON5 { name: "provide range for a value error", @@ -234,6 +237,7 @@ describe("json-validation", () => { schema: testSchema2, }, // YAML + ...jsonSuite.map((t) => ({ ...t, mode: MODES.YAML })), { name: "provide range for a value error", mode: MODES.YAML, @@ -286,8 +290,8 @@ object: {} `, errors: [ { - from: 13, - to: 19, + from: 21, + to: 23, message: "The required property `foo` is missing at `object`", }, ], diff --git a/src/constants.ts b/src/constants.ts index 58ea668..c7643e9 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -25,6 +25,8 @@ export const YAML_TOKENS_MAPPING: Record< Key: TOKENS.PROPERTY_NAME, BlockSequence: TOKENS.ARRAY, BlockMapping: TOKENS.OBJECT, + FlowSequence: TOKENS.ARRAY, + FlowMapping: TOKENS.OBJECT, QuotedLiteral: TOKENS.STRING, Literal: TOKENS.STRING, // best guess Stream: TOKENS.JSON_TEXT,