Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep properties of invalid schemas #553

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions lib/dot/properties.jst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
{{# def.optimizeValidate }}
#}}

{{## def.removeKey:
{{? $removeAdditionalOnSuccess }}
additionalToRemove{{=$lvl}}.push({{=$key}});
{{??}}
delete {{=$data}}[{{=$key}}];
{{?}}
#}}

{{
var $key = 'key' + $lvl
Expand All @@ -37,6 +44,7 @@
, $additionalIsSchema = typeof $aProperties == 'object'
&& Object.keys($aProperties).length
, $removeAdditional = it.opts.removeAdditional
, $removeAdditionalOnSuccess = it.opts.removeAdditional && it.opts.removeAdditionalOnSuccess
, $checkAdditional = $noAdditional || $additionalIsSchema || $removeAdditional
, $ownProperties = it.opts.ownProperties
, $currentBaseId = it.baseId;
Expand All @@ -57,7 +65,9 @@ var {{=$nextValid}} = true;
{{? $ownProperties }}
var {{=$dataProperties}} = undefined;
{{?}}

{{? $removeAdditionalOnSuccess }}
var additionalToRemove{{=$lvl}} = [];
{{?}}
{{? $checkAdditional }}
{{# def.iterateProperties }}
{{? $someProperties }}
Expand Down Expand Up @@ -86,7 +96,7 @@ var {{=$nextValid}} = true;
if (isAdditional{{=$lvl}}) {
{{?}}
{{? $removeAdditional == 'all' }}
delete {{=$data}}[{{=$key}}];
{{# def.removeKey }}
{{??}}
{{
var $currentErrorPath = it.errorPath;
Expand All @@ -97,7 +107,7 @@ var {{=$nextValid}} = true;
}}
{{? $noAdditional }}
{{? $removeAdditional }}
delete {{=$data}}[{{=$key}}];
{{# def.removeKey }}
{{??}}
{{=$nextValid}} = false;
{{
Expand All @@ -121,7 +131,8 @@ var {{=$nextValid}} = true;
if (errors) validate.errors.length = errors;
else validate.errors = null;
}
delete {{=$data}}[{{=$key}}];
{{=$nextValid}} = true;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting this field to true fixed the bug I mentioned.

{{# def.removeKey }}
}

{{# def.resetCompositeRule }}
Expand Down Expand Up @@ -325,3 +336,9 @@ var {{=$nextValid}} = true;
{{?}}

{{# def.cleanUp }}

{{? $removeAdditionalOnSuccess }}
for (var {{=$idx}}=0; {{=$idx}} < additionalToRemove{{=$lvl}}.length; {{=$idx}}++) {
delete {{=$data}}[additionalToRemove{{=$lvl}}[{{=$idx}}]];
}
{{?}}
151 changes: 95 additions & 56 deletions spec/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,119 @@ var Ajv = require('./ajv')


describe('Ajv Options', function () {
describe('removeAdditional', function() {
it('should remove all additional properties', function() {
var ajv = new Ajv({ removeAdditional: 'all' });
[true, false].forEach(function(removeAdditionalOnSuccess) {
describe('removeAdditional' + (removeAdditionalOnSuccess ? ' with removeAdditionalOnSuccess': ''), function() {
it('should remove all additional properties', function() {
var ajv = new Ajv({ removeAdditional: 'all', removeAdditionalOnSuccess: removeAdditionalOnSuccess });

ajv.addSchema({
id: '//test/fooBar',
properties: { foo: { type: 'string' }, bar: { type: 'string' } }
});

var object = {
foo: 'foo', bar: 'bar', baz: 'baz-to-be-removed'
};

ajv.validate('//test/fooBar', object).should.equal(true);
object.should.have.property('foo');
object.should.have.property('bar');
object.should.not.have.property('baz');

ajv.addSchema({
id: '//test/fooBar',
properties: { foo: { type: 'string' }, bar: { type: 'string' } }
var failingObject = {
foo: 'foo', bar: 42, baz: 'baz-to-be-removed'
};

ajv.validate('//test/fooBar', failingObject).should.equal(false);
failingObject.should.have.property('foo');
failingObject.should.have.property('bar');
if (removeAdditionalOnSuccess)
failingObject.should.have.property('baz');
else
failingObject.should.not.have.property('baz');
});

var object = {
foo: 'foo', bar: 'bar', baz: 'baz-to-be-removed'
};

ajv.validate('//test/fooBar', object).should.equal(true);
object.should.have.property('foo');
object.should.have.property('bar');
object.should.not.have.property('baz');
});
it('should remove properties that would error when `additionalProperties = false`', function() {
var ajv = new Ajv({ removeAdditional: true, removeAdditionalOnSuccess: removeAdditionalOnSuccess });

ajv.addSchema({
id: '//test/fooBar',
properties: { foo: { type: 'string' }, bar: { type: 'string' } },
additionalProperties: false
});

it('should remove properties that would error when `additionalProperties = false`', function() {
var ajv = new Ajv({ removeAdditional: true });
var object = {
foo: 'foo', bar: 'bar', baz: 'baz-to-be-removed'
};

ajv.addSchema({
id: '//test/fooBar',
properties: { foo: { type: 'string' }, bar: { type: 'string' } },
additionalProperties: false
ajv.validate('//test/fooBar', object).should.equal(true);
object.should.have.property('foo');
object.should.have.property('bar');
object.should.not.have.property('baz');

var failingObject = {
foo: 'foo', bar: 42, baz: 'baz-to-be-removed'
};

ajv.validate('//test/fooBar', failingObject).should.equal(false);
failingObject.should.have.property('foo');
failingObject.should.have.property('bar');
if (removeAdditionalOnSuccess)
failingObject.should.have.property('baz');
else
failingObject.should.not.have.property('baz');
});

var object = {
foo: 'foo', bar: 'bar', baz: 'baz-to-be-removed'
};

ajv.validate('//test/fooBar', object).should.equal(true);
object.should.have.property('foo');
object.should.have.property('bar');
object.should.not.have.property('baz');
});
it('should remove properties that would error when `additionalProperties` is a schema', function() {
var ajv = new Ajv({ removeAdditional: 'failing', removeAdditionalOnSuccess: removeAdditionalOnSuccess });

ajv.addSchema({
id: '//test/fooBar',
properties: { foo: { type: 'string' }, bar: { type: 'string' } },
additionalProperties: { type: 'string' }
});

it('should remove properties that would error when `additionalProperties` is a schema', function() {
var ajv = new Ajv({ removeAdditional: 'failing' });
var object = {
foo: 'foo', bar: 'bar', baz: 'baz-to-be-kept', fizz: 1000
};

ajv.addSchema({
id: '//test/fooBar',
properties: { foo: { type: 'string' }, bar: { type: 'string' } },
additionalProperties: { type: 'string' }
});
ajv.validate('//test/fooBar', object).should.equal(true);
object.should.have.property('foo');
object.should.have.property('bar');
object.should.have.property('baz');
object.should.not.have.property('fizz');

var object = {
foo: 'foo', bar: 'bar', baz: 'baz-to-be-kept', fizz: 1000
};
ajv.addSchema({
id: '//test/fooBar2',
properties: { foo: { type: 'string' }, bar: { type: 'string' } },
additionalProperties: { type: 'string', pattern: '^to-be-', maxLength: 10 }
});

ajv.validate('//test/fooBar', object).should.equal(true);
object.should.have.property('foo');
object.should.have.property('bar');
object.should.have.property('baz');
object.should.not.have.property('fizz');
object = {
foo: 'foo', bar: 'bar', baz: 'to-be-kept', quux: 'to-be-removed', fizz: 1000
};

ajv.addSchema({
id: '//test/fooBar2',
properties: { foo: { type: 'string' }, bar: { type: 'string' } },
additionalProperties: { type: 'string', pattern: '^to-be-', maxLength: 10 }
});
ajv.validate('//test/fooBar2', object).should.equal(true);
object.should.have.property('foo');
object.should.have.property('bar');
object.should.have.property('baz');
object.should.not.have.property('fizz');

object = {
foo: 'foo', bar: 'bar', baz: 'to-be-kept', quux: 'to-be-removed', fizz: 1000
};
var failingObject = {
foo: 'foo', bar: 42, baz: 'to-be-kept', quux: 'to-be-removed', fizz: 1000
};

ajv.validate('//test/fooBar2', object).should.equal(true);
object.should.have.property('foo');
object.should.have.property('bar');
object.should.have.property('baz');
object.should.not.have.property('fizz');
ajv.validate('//test/fooBar2', failingObject).should.equal(false);
failingObject.should.have.property('foo');
failingObject.should.have.property('bar');
failingObject.should.have.property('baz');
if (removeAdditionalOnSuccess)
failingObject.should.have.property('fizz');
else
failingObject.should.not.have.property('fizz');
});
});
});

Expand Down