fix: check only own properties of schema, change property limit for equility check from 5 to 8, fixes #743
parent
5c063d8cf6
commit
cad7dc94e8
|
@ -58,8 +58,8 @@ var {{=$nextValid}} = true;
|
||||||
{{? $someProperties }}
|
{{? $someProperties }}
|
||||||
var isAdditional{{=$lvl}} = !(false
|
var isAdditional{{=$lvl}} = !(false
|
||||||
{{? $schemaKeys.length }}
|
{{? $schemaKeys.length }}
|
||||||
{{? $schemaKeys.length > 5 }}
|
{{? $schemaKeys.length > 8 }}
|
||||||
|| validate.schema{{=$schemaPath}}[{{=$key}}]
|
|| validate.schema{{=$schemaPath}}.hasOwnProperty({{=$key}})
|
||||||
{{??}}
|
{{??}}
|
||||||
{{~ $schemaKeys:$propertyKey }}
|
{{~ $schemaKeys:$propertyKey }}
|
||||||
|| {{=$key}} == {{= it.util.toQuotedString($propertyKey) }}
|
|| {{=$key}} == {{= it.util.toQuotedString($propertyKey) }}
|
||||||
|
|
|
@ -666,3 +666,40 @@ describe('full date format validation should understand leap years', function ()
|
||||||
ajv.validate(schema, invalidDate) .should.equal(false);
|
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']);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
|
@ -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() {
|
it('should remove properties that would error when `additionalProperties` is a schema', function() {
|
||||||
var ajv = new Ajv({ removeAdditional: 'failing' });
|
var ajv = new Ajv({ removeAdditional: 'failing' });
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue