diff --git a/lib/dot/properties.jst b/lib/dot/properties.jst index 6e82b45ae0..8d56decef6 100644 --- a/lib/dot/properties.jst +++ b/lib/dot/properties.jst @@ -20,6 +20,14 @@ {{# def.optimizeValidate }} #}} +{{## def.removeKey: + console.log('removing', {{=$key}}); + {{? $removeAdditionalOnSuccess }} + additionalToRemove{{=$lvl}}.push({{=$key}}); + {{??}} + delete {{=$data}}[{{=$key}}]; + {{?}} +#}} {{ var $key = 'key' + $lvl @@ -37,6 +45,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; @@ -57,7 +66,7 @@ var {{=$nextValid}} = true; {{? $ownProperties }} var {{=$dataProperties}} = undefined; {{?}} -{{? $removeAdditional }} +{{? $removeAdditionalOnSuccess }} var additionalToRemove{{=$lvl}} = []; {{?}} {{? $checkAdditional }} @@ -88,7 +97,7 @@ var {{=$nextValid}} = true; if (isAdditional{{=$lvl}}) { {{?}} {{? $removeAdditional == 'all' }} - additionalToRemove{{=$lvl}}.push({{=$key}}); + {{# def.removeKey }} {{??}} {{ var $currentErrorPath = it.errorPath; @@ -99,7 +108,7 @@ var {{=$nextValid}} = true; }} {{? $noAdditional }} {{? $removeAdditional }} - additionalToRemove{{=$lvl}}.push({{=$key}}); + {{# def.removeKey }} {{??}} {{=$nextValid}} = false; {{ @@ -118,12 +127,13 @@ var {{=$nextValid}} = true; {{# def.validateAdditional }} if (!{{=$nextValid}}) { + console.log('yep', errors); errors = {{=$errs}}; if (validate.errors !== null) { if (errors) validate.errors.length = errors; else validate.errors = null; } - additionalToRemove{{=$lvl}}.push({{=$key}}); + {{# def.removeKey }} } {{# def.resetCompositeRule }} @@ -328,7 +338,7 @@ var {{=$nextValid}} = true; {{# def.cleanUp }} -{{? $removeAdditional }} +{{? $removeAdditionalOnSuccess }} for (var {{=$idx}}=0; {{=$idx}} < additionalToRemove{{=$lvl}}.length; {{=$idx}}++) { delete {{=$data}}[additionalToRemove{{=$lvl}}[{{=$idx}}]]; } diff --git a/spec/options.spec.js b/spec/options.spec.js index c423239b21..204bd08419 100644 --- a/spec/options.spec.js +++ b/spec/options.spec.js @@ -23,6 +23,15 @@ describe('Ajv Options', function () { 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'); + failingObject.should.not.have.property('baz'); }); @@ -43,65 +52,39 @@ describe('Ajv Options', function () { 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'); + failingObject.should.not.have.property('baz'); }); it('should remove properties that would error when `additionalProperties` is a schema', function() { var ajv = new Ajv({ removeAdditional: 'failing' }); - ajv.addSchema({ - id: '//test/fooBar', - properties: { foo: { type: 'string' }, bar: { type: 'string' } }, - additionalProperties: { type: 'string' } - }); - - var object = { - foo: 'foo', bar: 'bar', baz: 'baz-to-be-kept', fizz: 1000 - }; - - 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'); - ajv.addSchema({ id: '//test/fooBar2', properties: { foo: { type: 'string' }, bar: { type: 'string' } }, additionalProperties: { type: 'string', pattern: '^to-be-', maxLength: 10 } }); - object = { - foo: 'foo', bar: 'bar', baz: 'to-be-kept', quux: 'to-be-removed', fizz: 1000 + var failingObject = { + foo: 232, + bar: 42, + fizz: 'buzz' }; - 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'); - }); - - it('should not remove properties from an invalid schema', function() { - var ajv = new Ajv({ removeAdditional: true }); - - ajv.addSchema({ - id: '//test/fooBar', - properties: { - foo: { type: 'string' }, - bar: { type: 'number' } - }, - additionalProperties: false - }); - - var object = { - foo: 'foo', bar: 'bar', baz: 'baz-not-to-be-removed' - }; - - ajv.validate('//test/fooBar', object).should.equal(false); - object.should.have.property('foo'); - object.should.have.property('bar'); - object.should.have.property('baz'); + console.log(ajv.getSchema('//test/fooBar2').toString()); + ajv.validate('//test/fooBar2', failingObject).should.equal(false); + failingObject.should.have.property('foo'); + failingObject.should.have.property('bar'); + failingObject.should.have.property('baz'); + failingObject.should.not.have.property('fizz'); }); });