From 789e2416acb18d5e90a2b0e4530425f97db7445c Mon Sep 17 00:00:00 2001 From: Josh Pinkney Date: Fri, 17 Jan 2020 12:39:15 -0500 Subject: [PATCH] Fixed merge key error --- src/languageservice/parser/jsonParser07.ts | 33 ++++++++++++++++++++-- test/schemaValidation.test.ts | 18 +++++++++++- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/languageservice/parser/jsonParser07.ts b/src/languageservice/parser/jsonParser07.ts index 8d59f815..04fc07ce 100644 --- a/src/languageservice/parser/jsonParser07.ts +++ b/src/languageservice/parser/jsonParser07.ts @@ -837,8 +837,37 @@ function validate(node: ASTNode, schema: JSONSchema, validationResult: Validatio const unprocessedProperties: string[] = []; for (const propertyNode of node.properties) { const key = propertyNode.keyNode.value; - seenKeys[key] = propertyNode.valueNode; - unprocessedProperties.push(key); + + //Replace the merge key with the actual values of what the node value points to in seen keys + if (key === '<<' && propertyNode.valueNode) { + + switch (propertyNode.valueNode.type) { + case 'object': { + propertyNode.valueNode['properties'].forEach(propASTNode => { + const propKey = propASTNode.keyNode.value; + seenKeys[propKey] = propASTNode.valueNode; + unprocessedProperties.push(propKey); + }); + break; + } + case 'array': { + propertyNode.valueNode['items'].forEach(sequenceNode => { + sequenceNode['properties'].forEach(propASTNode => { + const seqKey = propASTNode.keyNode.value; + seenKeys[seqKey] = propASTNode.valueNode; + unprocessedProperties.push(seqKey); + }); + }); + break; + } + default: { + break; + } + } + } else { + seenKeys[key] = propertyNode.valueNode; + unprocessedProperties.push(key); + } } if (Array.isArray(schema.required)) { diff --git a/test/schemaValidation.test.ts b/test/schemaValidation.test.ts index 0e6d4027..119b69c6 100644 --- a/test/schemaValidation.test.ts +++ b/test/schemaValidation.test.ts @@ -354,6 +354,23 @@ suite('Validation Tests', () => { }); }); + describe('Test anchors specifically against gitlab schema', function () { + it('Test that anchors do not report Property << is not allowed', done => { + languageService.configure({ + schemas: [{ + uri: 'http://json.schemastore.org/gitlab-ci', + fileMatch: ['*.yaml', '*.yml'] + }], + validate: true + }); + const content = '.test-cache: &test-cache\n cache: {}\nnodejs-tests:\n <<: *test-cache\n script: test'; + const validator = parseSetup(content); + validator.then(function (result) { + assert.equal(result.length, 0); + }).then(done, done); + }); + }); + describe('Test with custom schemas', function () { function parseSetup(content: string) { const testTextDocument = setupTextDocument(content); @@ -412,7 +429,6 @@ suite('Validation Tests', () => { assert.equal(result[1].message, `Value is not accepted. Valid values: "ImageStreamImport", "ImageStreamLayers".`); }).then(done, done); }); - }); }); });