Skip to content

Commit 1f7c659

Browse files
committed
update test for $schema
1 parent 6767a56 commit 1f7c659

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

src/languageservice/services/dollarUtils.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ import { JSONDocument } from '../parser/jsonParser07';
1212
* @param doc
1313
*/
1414
export function getDollarSchema(doc: SingleYAMLDocument | JSONDocument): string | undefined {
15-
if (doc instanceof SingleYAMLDocument && doc.root && doc.root.type === 'object') {
16-
let dollarSchema = doc.root.properties['$schema'];
17-
dollarSchema = typeof dollarSchema === 'string' ? dollarSchema.trim() : undefined;
15+
if ((doc instanceof SingleYAMLDocument || doc instanceof JSONDocument) && doc.root?.type === 'object') {
16+
let dollarSchema: string | undefined = undefined;
17+
for (const property of doc.root.properties) {
18+
if (property.keyNode?.value === '$schema' && typeof property.valueNode?.value === 'string') {
19+
dollarSchema = property.valueNode?.value;
20+
break;
21+
}
22+
}
1823
if (typeof dollarSchema === 'string') {
19-
return dollarSchema.trim();
24+
return dollarSchema;
2025
}
2126
if (dollarSchema) {
2227
console.log('The $schema attribute is not a string, and will be ignored');
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
3+
"properties": {
4+
"dollar-schema": {
5+
"type":"string"
6+
}
7+
}
8+
}

test/schema.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,8 @@ describe('JSON Schema', () => {
590590
// eslint-disable-next-line @typescript-eslint/no-var-requires
591591
const schemaModelineSample = path.join(__dirname, './fixtures/sample-modeline.json');
592592
// eslint-disable-next-line @typescript-eslint/no-var-requires
593+
const schemaDollarSample = path.join(__dirname, './fixtures/sample-dollar-schema.json');
594+
// eslint-disable-next-line @typescript-eslint/no-var-requires
593595
const schemaDefaultSnippetSample = require(path.join(__dirname, './fixtures/defaultSnippets-const-if-else.json'));
594596
const languageSettingsSetup = new ServiceSetup().withCompletion();
595597

@@ -615,12 +617,42 @@ describe('JSON Schema', () => {
615617
});
616618
languageService.configure(languageSettingsSetup.languageSettings);
617619
languageService.registerCustomSchemaProvider((uri: string) => Promise.resolve(uri));
618-
const testTextDocument = setupTextDocument(`# yaml-language-server: $schema=${schemaModelineSample}\n\n`);
620+
const testTextDocument = setupTextDocument(
621+
`# yaml-language-server: $schema=${schemaModelineSample}\n$schema: ${schemaDollarSample}\n\n`
622+
);
619623
const result = await languageService.doComplete(testTextDocument, Position.create(1, 0), false);
620624
assert.strictEqual(result.items.length, 1);
621625
assert.strictEqual(result.items[0].label, 'modeline');
622626
});
623627

628+
it('Explicit $schema takes precedence over all other lower priority schemas', async () => {
629+
languageSettingsSetup
630+
.withSchemaFileMatch({
631+
fileMatch: ['test.yaml'],
632+
uri: TEST_URI,
633+
priority: SchemaPriority.SchemaStore,
634+
schema: schemaStoreSample,
635+
})
636+
.withSchemaFileMatch({
637+
fileMatch: ['test.yaml'],
638+
uri: TEST_URI,
639+
priority: SchemaPriority.SchemaAssociation,
640+
schema: schemaAssociationSample,
641+
})
642+
.withSchemaFileMatch({
643+
fileMatch: ['test.yaml'],
644+
uri: TEST_URI,
645+
priority: SchemaPriority.Settings,
646+
schema: schemaSettingsSample,
647+
});
648+
languageService.configure(languageSettingsSetup.languageSettings);
649+
languageService.registerCustomSchemaProvider((uri: string) => Promise.resolve(uri));
650+
const testTextDocument = setupTextDocument(`$schema: ${schemaDollarSample}\n\n`);
651+
const result = await languageService.doComplete(testTextDocument, Position.create(1, 0), false);
652+
assert.strictEqual(result.items.length, 1);
653+
assert.strictEqual(result.items[0].label, 'dollar-schema');
654+
});
655+
624656
it('Manually setting schema takes precendence over all other lower priority schemas', async () => {
625657
languageSettingsSetup
626658
.withSchemaFileMatch({

0 commit comments

Comments
 (0)