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

Expose errors from filter feature #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,28 @@ validate(42) // return false

## Filtering away additional properties

is-my-json-valid supports filtering away properties not in the schema
is-my-json-valid supports filtering away properties not in the schema.
The option `filter` changes the semantics of the `additionalProperties: false`,
such that additional properties are deleted instead of causing a validation
error. Be warned this is a non-standard feature, and it will mutate the input
object.

``` js
var filter = validator.filter({
required: true,
var schema = {
type: 'object',
properties: {
hello: {type: 'string', required: true}
hello: {type: 'string'}
},
additionalProperties: false
})
};

var validateAndfilter = validator(schema, {
filter: true
});

var doc = {hello: 'world', notInSchema: true}
console.log(filter(doc)) // {hello: 'world'}
console.log(validateAndfilter(doc)) // true (ie. no error)
console.log(doc) // {hello: 'world'}
```

## Verbose mode outputs the value on errors
Expand Down
19 changes: 8 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ var compile = function(schema, cache, root, reporter, opts) {
return v
}

var validate;
var visit = function(name, node, reporter, filter) {
var properties = node.properties
var type = node.type
Expand Down Expand Up @@ -286,8 +287,12 @@ var compile = function(schema, cache, root, reporter, opts) {
('if (%s) {', additionalProp)

if (node.additionalProperties === false) {
if (filter) validate('delete %s', name+'['+keys+'['+i+']]')
error('has additional properties', null, JSON.stringify(name+'.') + ' + ' + keys + '['+i+']')
if (filter) {
validate('delete %s', name+'['+keys+'['+i+']]')
} else {
error('has additional properties', null,
JSON.stringify(name+'.') + ' + ' + keys + '['+i+']')
}
} else {
visit(name+'['+keys+'['+i+']]', node.additionalProperties, reporter, filter)
}
Expand Down Expand Up @@ -509,7 +514,7 @@ var compile = function(schema, cache, root, reporter, opts) {
while (indent--) validate('}')
}

var validate = genfun
validate = genfun
('function validate(data) {')
('validate.errors = null')
('var errors = 0')
Expand Down Expand Up @@ -543,11 +548,3 @@ module.exports = function(schema, opts) {
if (typeof schema === 'string') schema = JSON.parse(schema)
return compile(schema, {}, schema, true, opts)
}

module.exports.filter = function(schema, opts) {
var validate = module.exports(schema, xtend(opts, {filter: true}))
return function(sch) {
validate(sch)
return sch
}
}
51 changes: 51 additions & 0 deletions test/filter_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var tape = require('tape');
var validator = require('../');

tape('filter with {additionalProperties: false}', function(t) {
t.plan(2);

var schema = {
type: 'object',
properties: {
prop: {type: 'string'}
},
additionalProperties: false
};

var validate = validator(schema, {
filter: true
});

var value = {
prop: 'my-value',
extra: 'should be filtered out'
};

t.ok(validate(value), 'should be valid, as we have {filter: true}');
t.deepEqual(value, {prop: 'my-value'}, 'should filter properties');
});

tape('{filter: true} still produces errors', function(t) {
t.plan(2);

var schema = {
type: 'object',
properties: {
prop: {type: 'string'}
},
required: ['prop'],
additionalProperties: false
};

var validate = validator(schema, {
filter: true
});

var value = {
extra: 'should be filtered out'
};

t.notOk(validate(value), 'should not be valid');
t.ok(validate.errors instanceof Array &&
validate.errors.length > 0, 'should have errors');
});