Skip to content

Commit 2181673

Browse files
author
Andreas Krummsdorf
committed
fix: allow type "null" in combination with proprtty "enum"
1 parent b3caafa commit 2181673

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

lib/revalidator.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,19 +556,22 @@
556556
}
557557
}
558558

559+
/* jshint ignore:start */
559560
if (schema['enum']) {
560-
if (schema['type'] === 'array' && isArray(value)) {
561+
if (value === null && isArray(schema.type) && schema.type.indexOf('null') > -1) {
562+
// is allowed
563+
} else if (schema['type'] === 'array' && isArray(value)) {
561564
for (i = 0; i < value.length; i++) {
562565
if (schema['enum'].indexOf(value[i]) === -1) {
563566
error('enum', property, value, schema, errors);
564567
break;
565568
}
566569
}
567-
}
568-
else if (schema['enum'].indexOf(value) === -1) {
570+
} else if (schema['enum'].indexOf(value) === -1) {
569571
error('enum', property, value, schema, errors);
570572
}
571573
}
574+
/* jshint ignore:end */
572575

573576
// Dependencies (see 5.8)
574577
if (typeof schema.dependencies === 'string' &&

test/tests.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1549,5 +1549,21 @@ describe('Validator', function () {
15491549
expect(val.validate({test: 3}, schema).valid).toBeFalsy();
15501550
expect(val.validate({test: 3}, schema).errors[0].expected).toBe('String');
15511551
});
1552+
1553+
it('should handle types ["null", "string"] in combination with property "enum"', function () {
1554+
var schema = {
1555+
properties: {
1556+
test: {
1557+
type: ['null', 'string'],
1558+
enum: ['a', 'b', 'c']
1559+
}
1560+
}
1561+
};
1562+
1563+
expect(val.validate({}, schema).valid).toBeTruthy();
1564+
expect(val.validate({test: 'a'}, schema).valid).toBeTruthy();
1565+
expect(val.validate({test: 'r'}, schema).valid).toBeFalsy();
1566+
expect(val.validate({test: null}, schema).valid).toBeTruthy();
1567+
});
15521568
});
15531569
});

0 commit comments

Comments
 (0)