Skip to content

Commit 5d2333f

Browse files
author
Andreas Krummsdorf
committed
fix: also check if a property is required when option ignoreNullValues is enabled
1 parent 9761b2b commit 5d2333f

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

lib/revalidator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@
412412
}
413413
}
414414

415-
if (options.ignoreNullValues && value === null) {
415+
if (options.ignoreNullValues && value === null && !schema.required) {
416416
return;
417417
}
418418

test/tests.spec.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,13 +905,15 @@ describe('Validator', function () {
905905
id: 1,
906906
name: 'wayne'
907907
};
908+
data.$wayne = '123';
908909

909910
var result = val.validate(data, schemaForTest, {unknownProperties: 'delete'});
910911

911912
expect(result.valid).toBe(true);
912913
expect(result.errors.length).toBe(0);
913914
expect(data.someUnknownProperty).toBeUndefined();
914915
expect(data.someUnknownObject).toBeUndefined();
916+
expect(data.$wayne).toBeUndefined();
915917
});
916918

917919
it('validate() should override the default validation options with the current options', function () {
@@ -1395,6 +1397,69 @@ describe('Validator', function () {
13951397
expect(result.errors.length).toBe(2);
13961398
});
13971399

1400+
it('should return a validation error when the option "ignoreNullValues" is enabled but the property is required', function () {
1401+
var schema = {
1402+
properties: {
1403+
name: {
1404+
type: 'string',
1405+
required: true
1406+
},
1407+
names: {
1408+
type: 'array',
1409+
required: true,
1410+
items: {
1411+
type: 'number'
1412+
}
1413+
},
1414+
arr: {
1415+
type: 'array',
1416+
items: {
1417+
type: 'object',
1418+
properties: {
1419+
name: {
1420+
type: 'string'
1421+
},
1422+
name1: {
1423+
type: 'boolean'
1424+
}
1425+
}
1426+
}
1427+
}
1428+
}
1429+
},
1430+
data = {
1431+
name: null,
1432+
names: null
1433+
};
1434+
1435+
var result = val.validate(data, schema);
1436+
1437+
expect(result.valid).toBeFalsy();
1438+
expect(result.errors.length).toBe(2);
1439+
1440+
result = val.validate(data, schema, {ignoreNullValues: true});
1441+
1442+
expect(result.valid).toBeFalsy();
1443+
expect(result.errors.length).toBe(2);
1444+
1445+
data.name = undefined;
1446+
result = val.validate(data, schema, {ignoreNullValues: true});
1447+
1448+
expect(result.valid).toBeFalsy();
1449+
1450+
data.name = 'wayne';
1451+
data.names = [1];
1452+
data.arr = [{name: 'test', name1: true}, null, {name: 'test1', name1: false}];
1453+
result = val.validate(data, schema, {ignoreNullValues: true});
1454+
1455+
expect(result.valid).toBeTruthy();
1456+
1457+
result = val.validate(data, schema);
1458+
1459+
expect(result.valid).toBeFalsy();
1460+
expect(result.errors.length).toBe(1);
1461+
});
1462+
13981463
it('should call the transform function when "options.transform" is a function', function () {
13991464
var schema = {
14001465
properties: {

0 commit comments

Comments
 (0)