Skip to content

Commit

Permalink
feat: add custom kubernetes schema support
Browse files Browse the repository at this point in the history
  • Loading branch information
tricktron committed Feb 5, 2023
1 parent d3e36a3 commit 1e89034
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/languageserver/handlers/settingsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ export class SettingsHandler {
* @param languageSettings current server settings
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private configureSchemas(
public configureSchemas(
uri: string,
fileMatch: string[],
schema: unknown,
Expand All @@ -338,12 +338,14 @@ export class SettingsHandler {
languageSettings.schemas.push({ uri, fileMatch: fileMatch, schema: schema, priority: priorityLevel });
}

if (fileMatch.constructor === Array && uri === KUBERNETES_SCHEMA_URL) {
fileMatch.forEach((url) => {
this.yamlSettings.specificValidatorPaths.push(url);
});
} else if (uri === KUBERNETES_SCHEMA_URL) {
this.yamlSettings.specificValidatorPaths.push(fileMatch);
if (uri.normalize().includes("kubernetes")) {
if (fileMatch.constructor === Array) {
fileMatch.forEach((url) => {
this.yamlSettings.specificValidatorPaths.push(url);
});
} else {
this.yamlSettings.specificValidatorPaths.push(fileMatch);
}
}

return languageSettings;
Expand Down
31 changes: 30 additions & 1 deletion test/schemaValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ import { Diagnostic, DiagnosticSeverity, Position } from 'vscode-languageserver-
import { expect } from 'chai';
import { SettingsState, TextDocumentTestManager } from '../src/yamlSettings';
import { ValidationHandler } from '../src/languageserver/handlers/validationHandlers';
import { LanguageService } from '../src/languageservice/yamlLanguageService';
import { LanguageService, SchemaPriority } from '../src/languageservice/yamlLanguageService';
import { KUBERNETES_SCHEMA_URL } from '../src/languageservice/utils/schemaUrls';
import { IProblem } from '../src/languageservice/parser/jsonParser07';
import { JSONSchema } from '../src/languageservice/jsonSchema';
import { TestTelemetry } from './utils/testsTypes';
import { SettingsHandler } from '../src/languageserver/handlers/settingsHandlers';
import { Connection } from 'vscode-languageserver';

describe('Validation Tests', () => {
let languageSettingsSetup: ServiceSetup;
Expand Down Expand Up @@ -1902,5 +1904,32 @@ obj:
expect(result[0].message).to.eq('Matches multiple schemas when only one must validate.');
expect(telemetry.messages).to.be.empty;
});

it('custom kubernetes schema should return validation errors', (done) => {
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',
['**'],
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);
});
});
});

0 comments on commit 1e89034

Please sign in to comment.