Skip to content

Commit 4370b5c

Browse files
authored
Fix bool enums (#1080)
* add (failing) test case for boolean enum and const values failing with: ``` [ { code: 1, data: { schemaUri: [ 'file:///default_schema_id.yaml' ], values: [ true, false ] }, message: 'Value is not accepted. Valid values: true, false.', range: { end: { character: 15, line: 0 }, start: { character: 11, line: 0 } }, severity: 1, source: 'yaml-schema: file:///default_schema_id.yaml' } ] ``` * fix the issue for `boolean` values in `enum` this is the same fix for `enum` that has been applied a while back for `const` values here: e6165e4 fixes issue: #1078 --------- Co-authored-by: xxx <xxx>
1 parent 2086df4 commit 4370b5c

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/languageservice/parser/jsonParser07.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ function validate(
876876
const val = getNodeValue(node);
877877
let enumValueMatch = false;
878878
for (const e of schema.enum) {
879-
if (val === e || isAutoCompleteEqualMaybe(callFromAutoComplete, node, val, e)) {
879+
if (equals(val, e, node.type) || isAutoCompleteEqualMaybe(callFromAutoComplete, node, val, e)) {
880880
enumValueMatch = true;
881881
break;
882882
}

test/schemaValidation.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,42 @@ describe('Validation Tests', () => {
189189
})
190190
.then(done, done);
191191
});
192+
193+
it('Test that boolean value can be used in enum', (done) => {
194+
schemaProvider.addSchema(SCHEMA_ID, {
195+
type: 'object',
196+
properties: {
197+
analytics: {
198+
enum: [true, false],
199+
},
200+
},
201+
});
202+
const content = 'analytics: true';
203+
const validator = parseSetup(content);
204+
validator
205+
.then(function (result) {
206+
assert.deepStrictEqual(result, []);
207+
})
208+
.then(done, done);
209+
});
210+
211+
it('Test that boolean value can be used in const', (done) => {
212+
schemaProvider.addSchema(SCHEMA_ID, {
213+
type: 'object',
214+
properties: {
215+
analytics: {
216+
const: true,
217+
},
218+
},
219+
});
220+
const content = 'analytics: true';
221+
const validator = parseSetup(content);
222+
validator
223+
.then(function (result) {
224+
assert.deepStrictEqual(result, []);
225+
})
226+
.then(done, done);
227+
});
192228
});
193229

194230
describe('String tests', () => {

0 commit comments

Comments
 (0)