Skip to content

Commit

Permalink
fixed removeAdditional, tests, added "failing" for removeAdditional; c…
Browse files Browse the repository at this point in the history
…loses #25, related to #23
  • Loading branch information
epoberezkin committed Jul 31, 2015
1 parent 054bf53 commit 62adbbe
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 47 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ Options can have properties `separator` (string used to separate errors, ", " by
## Options

- _allErrors_: check all rules collecting all errors. Default is to return after the first error.
- _removeAdditional_: remove additional properties. Default is not to remove. If the option is 'all', then all additional properties are removed, regardless of `additionalProperties` keyword in schema (and no validation is made for them). If the option is `true` (or truthy), only additional properties with `additionalProperties` keyword equal to `false` are removed.
- _removeAdditional_: remove additional properties. Default is not to remove. If the option is 'all', then all additional properties are removed, regardless of `additionalProperties` keyword in schema (and no validation is made for them). If the option is `true` (or truthy), only additional properties with `additionalProperties` keyword equal to `false` are removed. If the option is 'failing', then additional properties that fail schema validation will be removed too (where `additionalProperties` keyword is schema).
- _verbose_: include the reference to the part of the schema and validated data in errors (false by default).
- _format_: formats validation mode ('fast' by default). Pass 'full' for more correct and slow validation or `false` not to validate formats at all. E.g., 25:00:00 and 2015/14/33 will be invalid time and date in 'full' mode but it will be valid in 'fast' mode.
- _formats_: an object with custom formats. Keys and values will be passed to `addFormat` method.
Expand Down Expand Up @@ -241,6 +241,11 @@ All validation functions are generated using doT templates in dot folder. Templa

## Changes history

##### 0.6.11

Improved/fixed data filtering with `removeAdditional` option.


##### 0.6.10

`removeAdditional` option allowing to remove additional properties.
Expand Down
6 changes: 5 additions & 1 deletion lib/ajv.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,12 @@ function Ajv(opts) {


function addInitialSchemas() {
if (self.opts.meta !== false)
if (self.opts.meta !== false) {
var currentRemoveAdditional = self.opts.removeAdditional;
self.opts.removeAdditional = false;
addSchema(require('./refs/json-schema-draft-04.json'), META_SCHEMA_ID, true);
self.opts.removeAdditional = currentRemoveAdditional;
}

var optsSchemas = self.opts.schemas;
if (!optsSchemas) return;
Expand Down
24 changes: 0 additions & 24 deletions lib/dot/PERFORMANCE.md

This file was deleted.

25 changes: 18 additions & 7 deletions lib/dot/properties.jst
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
, $noAdditional = $aProperties === false
, $additionalIsSchema = typeof $aProperties == 'object'
&& Object.keys($aProperties).length
, $checkAdditional = $noAdditional || $additionalIsSchema
, $removeAdditional = it.opts.removeAdditional;
, $removeAdditional = it.opts.removeAdditional
, $checkAdditional = $noAdditional || $additionalIsSchema || $removeAdditional;
}}


Expand All @@ -23,10 +23,7 @@ var valid{{=$it.level}} = true;

{{? $checkAdditional }}
var propertiesSchema{{=$lvl}} = validate.schema{{=$schemaPath}} || {};
{{?}}


{{? $checkAdditional }}
for (var key{{=$lvl}} in {{=$data}}) {
var isAdditional{{=$lvl}} = propertiesSchema{{=$lvl}}[key{{=$lvl}}] === undefined;

Expand Down Expand Up @@ -58,7 +55,10 @@ var valid{{=$it.level}} = true;
{{# def.error:'additionalProperties' }}
{{? $breakOnError }} break; {{?}}
{{?}}
{{??}}
{{?? $additionalIsSchema }}
{{? $removeAdditional == 'failing' }}
var {{=$errs}} = errors;
{{?}}
{{ /* additionalProperties is schema */
$it.schema = $aProperties;
$it.schemaPath = it.schemaPath + '.additionalProperties';
Expand All @@ -70,7 +70,18 @@ var valid{{=$it.level}} = true;
{{ var $code = it.validate($it); }}
{{# def.optimizeValidate }}

{{? $breakOnError }} if (!valid{{=$it.level}}) break; {{?}}
{{? $removeAdditional == 'failing' }}
if (!valid{{=$it.level}}) {
errors = {{=$errs}};
if (validate.errors !== null) {
if (errors) validate.errors.length = errors;
else validate.errors = null;
}
delete {{=$data}}[key{{=$lvl}}];
}
{{??}}
{{? $breakOnError }} if (!valid{{=$it.level}}) break; {{?}}
{{?}}
{{?}}
{{ it.errorPath = $currentErrorPath; }}
{{?}}
Expand Down
22 changes: 13 additions & 9 deletions lib/dotjs/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@ module.exports = function anonymous(it) {
$aProperties = it.schema.additionalProperties,
$noAdditional = $aProperties === false,
$additionalIsSchema = typeof $aProperties == 'object' && Object.keys($aProperties).length,
$checkAdditional = $noAdditional || $additionalIsSchema,
$removeAdditional = it.opts.removeAdditional;
$removeAdditional = it.opts.removeAdditional,
$checkAdditional = $noAdditional || $additionalIsSchema || $removeAdditional;
out += 'var ' + ($errs) + ' = errors;var valid' + ($it.level) + ' = true;';
if ($checkAdditional) {
out += ' var propertiesSchema' + ($lvl) + ' = validate.schema' + ($schemaPath) + ' || {};';
}
if ($checkAdditional) {
out += ' for (var key' + ($lvl) + ' in ' + ($data) + ') { var isAdditional' + ($lvl) + ' = propertiesSchema' + ($lvl) + '[key' + ($lvl) + '] === undefined; ';
out += ' var propertiesSchema' + ($lvl) + ' = validate.schema' + ($schemaPath) + ' || {}; for (var key' + ($lvl) + ' in ' + ($data) + ') { var isAdditional' + ($lvl) + ' = propertiesSchema' + ($lvl) + '[key' + ($lvl) + '] === undefined; ';
if ($pPropertyKeys.length) {
out += ' if (isAdditional' + ($lvl) + ') { ';
var arr1 = $pPropertyKeys;
Expand Down Expand Up @@ -74,7 +71,10 @@ module.exports = function anonymous(it) {
out += ' break; ';
}
}
} else {
} else if ($additionalIsSchema) {
if ($removeAdditional == 'failing') {
out += ' var ' + ($errs) + ' = errors; ';
}
$it.schema = $aProperties;
$it.schemaPath = it.schemaPath + '.additionalProperties';
$it.errorPath = it.errorPath;
Expand All @@ -86,8 +86,12 @@ module.exports = function anonymous(it) {
} else {
out += ' var ' + ($nextData) + ' = ' + ($passData) + '; ' + ($code) + ' ';
}
if ($breakOnError) {
out += ' if (!valid' + ($it.level) + ') break; ';
if ($removeAdditional == 'failing') {
out += ' if (!valid' + ($it.level) + ') { errors = ' + ($errs) + '; if (validate.errors !== null) { if (errors) validate.errors.length = errors; else validate.errors = null; } delete ' + ($data) + '[key' + ($lvl) + ']; } ';
} else {
if ($breakOnError) {
out += ' if (!valid' + ($it.level) + ') break; ';
}
}
}
it.errorPath = $currentErrorPath;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "0.6.10",
"version": "0.6.11",
"description": "Another JSON Schema Validator",
"main": "lib/ajv.js",
"scripts": {
Expand Down
27 changes: 23 additions & 4 deletions spec/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,29 @@ var Ajv = require(typeof window == 'object' ? 'ajv' : '../lib/ajv')

describe('Ajv Options', function () {
describe('removeAdditional', function() {
it('should remove all additional properties', function() {
var ajv = Ajv({ removeAdditional: 'all' });

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


it('should remove properties that would error when `additionalProperties = false`', function() {
var ajv = Ajv({ removeAdditional: true });

ajv.compile({
ajv.addSchema({
id: '//test/fooBar',
properties: { foo: { type: 'string' }, bar: { type: 'string' } },
additionalProperties: false
Expand All @@ -27,10 +46,10 @@ describe('Ajv Options', function () {
});


it.skip('should remove properties that would error when `additionalProperties` is a schema', function() {
var ajv = Ajv({ removeAdditional: true });
it('should remove properties that would error when `additionalProperties` is a schema', function() {
var ajv = Ajv({ removeAdditional: 'failing' });

ajv.compile({
ajv.addSchema({
id: '//test/fooBar',
properties: { foo: { type: 'string' }, bar: { type: 'string' } },
additionalProperties: { type: 'string' }
Expand Down

0 comments on commit 62adbbe

Please sign in to comment.