Skip to content

Commit

Permalink
Merge pull request #101 from redhat-developer/fix-boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
JPinkney committed Nov 8, 2018
2 parents 3b79ef3 + 85039b4 commit 9351ef5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/languageservice/parser/scalar-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Parse a boolean according to the specification
*
* Return:
* true if its a true value
* false if its a false value
*/
export function parseYamlBoolean(input: string): boolean {
if (["true", "True", "TRUE", 'y', 'Y', 'yes', 'Yes', 'YES', 'on', 'On', 'ON'].lastIndexOf(input) >= 0) {
return true;
}
else if (["false", "False", "FALSE", 'n', 'N', 'no', 'No', 'NO', 'off', 'Off', 'OFF'].lastIndexOf(input) >= 0) {
return false;
}
throw `Invalid boolean "${input}"`
}
5 changes: 3 additions & 2 deletions src/languageservice/parser/yamlParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Kind } from 'yaml-ast-parser'
import { Schema, Type } from 'js-yaml';

import { getLineStartPositions, getPosition } from '../utils/documentPositionCalculator'
import { parseYamlBoolean } from './scalar-type';

export class SingleYAMLDocument extends JSONDocument {
private lines;
Expand Down Expand Up @@ -152,8 +153,8 @@ function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode {

//This is a patch for redirecting values with these strings to be boolean nodes because its not supported in the parser.
let possibleBooleanValues = ['y', 'Y', 'yes', 'Yes', 'YES', 'n', 'N', 'no', 'No', 'NO', 'on', 'On', 'ON', 'off', 'Off', 'OFF'];
if (possibleBooleanValues.indexOf(value.toString()) !== -1) {
return new BooleanASTNode(parent, name, value, node.startPosition, node.endPosition)
if (instance.plainScalar && possibleBooleanValues.indexOf(value.toString()) !== -1) {
return new BooleanASTNode(parent, name, parseYamlBoolean(value), node.startPosition, node.endPosition)
}

switch (type) {
Expand Down
41 changes: 41 additions & 0 deletions test/schemaValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,47 @@ suite("Validation Tests", () => {
}).then(done, done);
});

it('Test that boolean value in quotations is not interpreted as boolean i.e. it errors', (done) => {
let content = `analytics: "no"`;
let validator = parseSetup(content);
validator.then(function(result){
assert.notEqual(result.length, 0);
}).then(done, done);
});

it('Test that boolean value without quotations is valid', (done) => {
let content = `analytics: no`;
let validator = parseSetup(content);
validator.then(function(result){
assert.equal(result.length, 0);
}).then(done, done);
});

it('Test that boolean is valid when inside strings', (done) => {
let content = `cwd: "no"`;
let validator = parseSetup(content);
validator.then(function(result){
assert.equal(result.length, 0);
}).then(done, done);
});

it('Test that boolean is invalid when no strings present and schema wants string', (done) => {
let content = `cwd: no`;
let validator = parseSetup(content);
validator.then(function(result){
assert.notEqual(result.length, 0);
}).then(done, done);
});

it('Basic test', (done) => {
let content = `analytics: true`;
let validator = parseSetup(content);
validator.then(function(result){
assert.equal(result.length, 0);
}).then(done, done);
});


it('Basic test on nodes with children', (done) => {
let content = `scripts:\n preinstall: test1\n postinstall: test2`;
let validator = parseSetup(content);
Expand Down

0 comments on commit 9351ef5

Please sign in to comment.