Skip to content

Commit

Permalink
#413 fix validation for empty file
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob committed Mar 18, 2021
1 parent 4499c21 commit 30001e5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
5 changes: 0 additions & 5 deletions src/languageserver/handlers/validationHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ export class ValidationHandler {
return;
}

if (textDocument.getText().length === 0) {
this.connection.sendDiagnostics({ uri: textDocument.uri, diagnostics: [] });
return;
}

return this.languageService
.doValidation(textDocument, isKubernetesAssociatedDocument(textDocument, this.yamlSettings.specificValidatorPaths))
.then(
Expand Down
7 changes: 6 additions & 1 deletion src/languageservice/services/yamlValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ export class YAMLValidation {
return Promise.resolve([]);
}

const yamlDocument: YAMLDocument = parseYAML(textDocument.getText(), this.customTags);
let text = textDocument.getText();
// if text is contains only whitespace wrap all text in object to force schema selection
if (!/\S/.test(text)) {
text = `{${text}}`;
}
const yamlDocument: YAMLDocument = parseYAML(text, this.customTags);
const validationResult = [];

let index = 0;
Expand Down
58 changes: 58 additions & 0 deletions test/schemaValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
BlockMappingEntryError,
DuplicateKeyError,
propertyIsNotAllowed,
MissingRequiredPropWarning,
} from './utils/errorMessages';
import * as assert from 'assert';
import * as path from 'path';
Expand Down Expand Up @@ -1198,4 +1199,61 @@ describe('Validation Tests', () => {
]);
});
});

describe('Empty document validation', () => {
it('should provide validation for empty document', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
scripts: {
type: 'string',
},
},
required: ['scripts'],
});
const content = '';
const result = await parseSetup(content);
assert.strictEqual(result.length, 1);
assert.deepStrictEqual(
result[0],
createDiagnosticWithData(
MissingRequiredPropWarning.replace('{0}', 'scripts'),
0,
0,
0,
0,
DiagnosticSeverity.Error,
`yaml-schema: file:///${SCHEMA_ID}`,
`file:///${SCHEMA_ID}`
)
);
});

it('should provide validation for document which contains only whitespaces', async () => {
languageService.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
scripts: {
type: 'string',
},
},
required: ['scripts'],
});
const content = ' \n \n';
const result = await parseSetup(content);
assert.deepStrictEqual(
result[0],
createDiagnosticWithData(
MissingRequiredPropWarning.replace('{0}', 'scripts'),
0,
0,
0,
1,
DiagnosticSeverity.Error,
`yaml-schema: file:///${SCHEMA_ID}`,
`file:///${SCHEMA_ID}`
)
);
});
});
});

0 comments on commit 30001e5

Please sign in to comment.