Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
cah-sam-beran committed Aug 31, 2017
1 parent 6bbcd84 commit 0609054
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 50 deletions.
20 changes: 15 additions & 5 deletions lib/dot/properties.jst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
{{# def.optimizeValidate }}
#}}

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

{{
var $key = 'key' + $lvl
Expand All @@ -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;
Expand All @@ -57,7 +66,7 @@ var {{=$nextValid}} = true;
{{? $ownProperties }}
var {{=$dataProperties}} = undefined;
{{?}}
{{? $removeAdditional }}
{{? $removeAdditionalOnSuccess }}
var additionalToRemove{{=$lvl}} = [];
{{?}}
{{? $checkAdditional }}
Expand Down Expand Up @@ -88,7 +97,7 @@ var {{=$nextValid}} = true;
if (isAdditional{{=$lvl}}) {
{{?}}
{{? $removeAdditional == 'all' }}
additionalToRemove{{=$lvl}}.push({{=$key}});
{{# def.removeKey }}
{{??}}
{{
var $currentErrorPath = it.errorPath;
Expand All @@ -99,7 +108,7 @@ var {{=$nextValid}} = true;
}}
{{? $noAdditional }}
{{? $removeAdditional }}
additionalToRemove{{=$lvl}}.push({{=$key}});
{{# def.removeKey }}
{{??}}
{{=$nextValid}} = false;
{{
Expand All @@ -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 }}
Expand Down Expand Up @@ -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}}]];
}
Expand Down
73 changes: 28 additions & 45 deletions spec/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});


Expand All @@ -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');
});
});

Expand Down

0 comments on commit 0609054

Please sign in to comment.