Skip to content

Commit 976ec2c

Browse files
committed
fix: ignore invalid schema properties
1 parent c495863 commit 976ec2c

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

lib/revalidator.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,11 @@
326326
if (schema.properties) {
327327
props = schema.properties;
328328
for (p in props) {
329-
if (props.hasOwnProperty(p)) {
329+
if (props.hasOwnProperty(p) && getType(props[p]) === 'object') {
330330
visitedProps.push(p);
331331
validateProperty(object, object[p], p, props[p], options, errors);
332+
} else {
333+
console.log('Schema invalid. Property ' + p + ' is not of type object, actual type: ' + getType(props[p]));
332334
}
333335
}
334336
}
@@ -337,7 +339,7 @@
337339
if (schema.patternProperties) {
338340
props = schema.patternProperties;
339341
for (p in props) {
340-
if (props.hasOwnProperty(p)) {
342+
if (props.hasOwnProperty(p) && getType(props[p]) === 'object') {
341343
var re = new RegExp(p);
342344

343345
// Find all object properties that are matching `re`
@@ -349,6 +351,8 @@
349351
}
350352
}
351353
}
354+
} else {
355+
console.log('Schema invalid. Property ' + p + ' is not of type object, actual type: ' + getType(props[p]));
352356
}
353357
}
354358
}
@@ -400,7 +404,6 @@
400404
}
401405
}
402406
}
403-
404407
}
405408

406409
function validateProperty (object, value, property, schema, options, errors) {

test/revalidator.spec.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ describe('revalidator', function () {
214214
});
215215

216216
it('should not cast to an integer when the value is a function', function () {
217-
var func = function () {};
217+
var func = function () {
218+
};
218219
data = {value: func};
219220

220221
expect(val.validate(data, schema, {cast: true})).toEqual({
@@ -354,7 +355,8 @@ describe('revalidator', function () {
354355
});
355356

356357
it('should not cast to a float when the value is a function', function () {
357-
var func = function () {};
358+
var func = function () {
359+
};
358360
data = {value: func};
359361

360362
expect(val.validate(data, schema, {cast: true})).toEqual({
@@ -480,7 +482,8 @@ describe('revalidator', function () {
480482
});
481483

482484
it('should not cast to a number when the value is a function', function () {
483-
var func = function () {};
485+
var func = function () {
486+
};
484487
data = {value: func};
485488

486489
expect(val.validate(data, schema, {cast: true})).toEqual({
@@ -626,7 +629,8 @@ describe('revalidator', function () {
626629
});
627630

628631
it('should not cast to a boolean when the value is a function', function () {
629-
var func = function () {};
632+
var func = function () {
633+
};
630634
data = {value: func};
631635

632636
expect(val.validate(data, schema, {cast: true})).toEqual({
@@ -852,5 +856,34 @@ describe('revalidator', function () {
852856
message: 'is not defined in schema'
853857
});
854858
});
859+
860+
it('should validate properties when schema itself has properties which are not of type object', function () {
861+
schema = {
862+
properties: {
863+
a: undefined,
864+
b: {type: 'string'}
865+
}
866+
};
867+
data = {a: 'a', b: 'b', c: 'c'};
868+
var res = val.validate(data, schema, {});
869+
870+
expect(res.valid).toBeTruthy();
871+
expect(Object.keys(data).length).toBe(3);
872+
});
873+
874+
it('should validate properties when schema itself has properties which are not of type object and remove properties which have schema with invalid properties', function () {
875+
schema = {
876+
properties: {
877+
a: undefined,
878+
b: {type: 'string'},
879+
d: []
880+
}
881+
};
882+
data = {a: 'a', b: 'b', c: 'c', d: 'd'};
883+
var res = val.validate(data, schema, {unknownProperties: 'delete'});
884+
885+
expect(res.valid).toBeTruthy();
886+
expect(Object.keys(data).length).toBe(1);
887+
});
855888
});
856889
});

0 commit comments

Comments
 (0)