Skip to content

Commit

Permalink
Merge pull request #222 from redhat-developer/merge-key-fix
Browse files Browse the repository at this point in the history
Fixed merge key error with JSON schema
  • Loading branch information
JPinkney committed Jan 22, 2020
2 parents 07ca6ca + 789e241 commit 70228ba
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
33 changes: 31 additions & 2 deletions src/languageservice/parser/jsonParser07.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
18 changes: 17 additions & 1 deletion test/schemaValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -412,7 +429,6 @@ suite('Validation Tests', () => {
assert.equal(result[1].message, `Value is not accepted. Valid values: "ImageStreamImport", "ImageStreamLayers".`);
}).then(done, done);
});

});
});
});

0 comments on commit 70228ba

Please sign in to comment.