Skip to content

Commit

Permalink
refactor: make kubernetes urls an array
Browse files Browse the repository at this point in the history
Like this, we can support multiple custom kubernetes associated
text documents. E.g. a custom kubernetes version and multiple
custom resource definitions (CRDs).
  • Loading branch information
tricktron committed Mar 19, 2023
1 parent c739bce commit 908a638
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/languageserver/handlers/notificationHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { LanguageService, SchemaConfiguration } from '../../languageservice/yaml
import {
CustomSchemaRequest,
DynamicCustomSchemaRequestRegistration,
KubernetesURLNotification,
KubernetesURLsNotification,
SchemaAssociationNotification,
SchemaSelectionRequests,
VSCodeContentRequestRegistration,
Expand Down Expand Up @@ -37,13 +37,13 @@ export class NotificationHandlers {
this.schemaAssociationNotificationHandler(associations)
);
this.connection.onNotification(DynamicCustomSchemaRequestRegistration.type, () => this.dynamicSchemaRequestHandler());
this.connection.onNotification(KubernetesURLNotification.type, (url) => this.kubernetesURLNotification(url));
this.connection.onNotification(KubernetesURLsNotification.type, (url) => this.kubernetesURLsNotification(url));
this.connection.onNotification(VSCodeContentRequestRegistration.type, () => this.vscodeContentRequestHandler());
this.connection.onNotification(SchemaSelectionRequests.type, () => this.schemaSelectionRequestHandler());
}

private kubernetesURLNotification(url: string): void {
this.yamlSettings.kubernetesSchemaUrl = url;
private kubernetesURLsNotification(urls: string[]): void {
this.yamlSettings.kubernetesSchemaUrls = urls;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/languageserver/handlers/settingsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ export class SettingsHandler {
languageSettings.schemas.push({ uri, fileMatch: fileMatch, schema: schema, priority: priorityLevel });
}

if (uri === this.yamlSettings.kubernetesSchemaUrl) {
if (this.yamlSettings.kubernetesSchemaUrls.includes(uri)) {
if (fileMatch.constructor === Array) {
fileMatch.forEach((url) => {
this.yamlSettings.specificValidatorPaths.push(url);
Expand All @@ -349,4 +349,4 @@ export class SettingsHandler {
}
return languageSettings;
}
}
}
4 changes: 2 additions & 2 deletions src/requestTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export namespace DynamicCustomSchemaRequestRegistration {
export const type: NotificationType<{}> = new NotificationType('yaml/registerCustomSchemaRequest');
}

export namespace KubernetesURLNotification {
export const type: NotificationType<string> = new NotificationType('yaml/kubernetesURL');
export namespace KubernetesURLsNotification {
export const type: NotificationType<string[]> = new NotificationType('yaml/kubernetesURLs');
}

export namespace VSCodeContentRequestRegistration {
Expand Down
2 changes: 1 addition & 1 deletion src/yamlSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class SettingsState {
customTags = [];
schemaStoreEnabled = true;
schemaStoreUrl = JSON_SCHEMASTORE_URL;
kubernetesSchemaUrl = KUBERNETES_SCHEMA_URL;
kubernetesSchemaUrls = [KUBERNETES_SCHEMA_URL];
indentation: string | undefined = undefined;
disableAdditionalProperties = false;
disableDefaultProperties = false;
Expand Down
19 changes: 8 additions & 11 deletions test/schemaValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1905,26 +1905,23 @@ obj:
expect(telemetry.messages).to.be.empty;
});

it('custom kubernetes schema should return validation errors', (done) => {
it('single custom kubernetes schema should return validation errors', async () => {
const customKubernetesSchemaVersion = 'https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.26.1-standalone-strict/all.json';
yamlSettings.kubernetesSchemaUrls = [customKubernetesSchemaVersion];
const settingsHandler = new SettingsHandler({} as Connection, languageService, yamlSettings, validationHandler, telemetry);
const initialSettings = languageSettingsSetup.withKubernetes(true).languageSettings;
const kubernetesSettings = settingsHandler.configureSchemas(
'https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master/v1.26.1-standalone-strict/all.json',
['**'],
customKubernetesSchemaVersion,
['*k8s.yml'],
undefined,
initialSettings,
SchemaPriority.SchemaAssociation
);
languageService.configure(kubernetesSettings);
const content = `apiVersion: apps/v1\nkind: Deployment\nfoo: bar`;
const validator = parseSetup(content);
validator
.then(function (result) {
assert.equal(result.length, 1);
// eslint-disable-next-line
assert.equal(result[0].message, `Property foo is not allowed.`);
})
.then(done, done);
const result = await parseSetup(content, 'invalid-k8s.yml');
expect(result.length).to.eq(1);
expect(result[0].message).to.eq('Property foo is not allowed.');
});
});
});

0 comments on commit 908a638

Please sign in to comment.