Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed merge key error with JSON schema #222

Merged
merged 1 commit into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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);
});

});
});
});