fix: check only own properties of schema, change property limit for equility check from 5 to 8, fixes #743

master
Evgeny Poberezkin 2018-03-24 22:18:09 +00:00
parent 5c063d8cf6
commit cad7dc94e8
3 changed files with 79 additions and 2 deletions

View File

@ -58,8 +58,8 @@ var {{=$nextValid}} = true;
{{? $someProperties }}
var isAdditional{{=$lvl}} = !(false
{{? $schemaKeys.length }}
{{? $schemaKeys.length > 5 }}
|| validate.schema{{=$schemaPath}}[{{=$key}}]
{{? $schemaKeys.length > 8 }}
|| validate.schema{{=$schemaPath}}.hasOwnProperty({{=$key}})
{{??}}
{{~ $schemaKeys:$propertyKey }}
|| {{=$key}} == {{= it.util.toQuotedString($propertyKey) }}

View File

@ -666,3 +666,40 @@ describe('full date format validation should understand leap years', function ()
ajv.validate(schema, invalidDate) .should.equal(false);
});
});
describe('property __proto__ should be removed with removeAdditional option, issue #743', function() {
it('should remove additional properties', function() {
var ajv = new Ajv({removeAdditional: true});
var schema = {
properties: {
obj: {
additionalProperties: false,
properties: {
a: { type: 'string' },
b: { type: 'string' },
c: { type: 'string' },
d: { type: 'string' },
e: { type: 'string' },
f: { type: 'string' },
g: { type: 'string' },
h: { type: 'string' },
i: { type: 'string' }
}
}
}
};
var obj= Object.create(null);
obj.__proto__ = null; // should be removed
obj.additional = 'will be removed';
obj.a = 'valid';
obj.b = 'valid';
var data = {obj: obj};
ajv.validate(schema, data) .should.equal(true);
Object.keys(data.obj) .should.eql(['a', 'b']);
});
});

View File

@ -46,6 +46,46 @@ describe('Ajv Options', function () {
});
it('should remove properties that would error when `additionalProperties = false` (many properties, boolean schema)', function() {
var ajv = new Ajv({removeAdditional: true});
var schema = {
properties: {
obj: {
additionalProperties: false,
properties: {
a: { type: 'string' },
b: false,
c: { type: 'string' },
d: { type: 'string' },
e: { type: 'string' },
f: { type: 'string' },
g: { type: 'string' },
h: { type: 'string' },
i: { type: 'string' }
}
}
}
};
var data = {
obj: {
a: 'valid',
b: 'should not be removed',
additional: 'will be removed'
}
};
ajv.validate(schema, data) .should.equal(false);
data .should.eql({
obj: {
a: 'valid',
b: 'should not be removed'
}
});
});
it('should remove properties that would error when `additionalProperties` is a schema', function() {
var ajv = new Ajv({ removeAdditional: 'failing' });