Skip to content

Commit

Permalink
Added {useDefault: true} option flag to substituted in default values
Browse files Browse the repository at this point in the history
Fixing issues #48.
  • Loading branch information
jonasfj committed May 11, 2015
1 parent c2341ce commit c5629d2
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,34 @@ var doc = {hello: 'world', notInSchema: true}
console.log(filter(doc)) // {hello: 'world'}
```

## Use default values from schema

is-my-json-valid supports using default values from the schema.
When activated this will set the properties to the value of the `default`
property provided in the schema.

```js
var validate = validator({
type: 'object',
properties: {
hello: {
type: 'string',
default: 'world'
}
}
}, {
useDefault: true
});

var value = {};
validate(value);
console.log(value); // {hello: 'world'}
```

This is a non-standard feature that will mutate the input. It only works
on objects and arrays. But is useful for keeping defaults in sync with values
documented in your schema.

## Verbose mode outputs the value on errors

is-my-json-valid outputs the value causing an error when verbose is set to true
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ var compile = function(schema, cache, root, reporter, opts) {
}
}

if (node.default !== undefined && opts && opts.setDefault === true) {
validate
('if (%s === undefined) {', name)
('%s = %s', name, JSON.stringify(node.default))
('}')
}

if (node.required === true) {
indent++
validate('if (%s === undefined) {', name)
Expand Down
124 changes: 124 additions & 0 deletions test/default_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
var tape = require('tape');
var validator = require('../');

tape('default on object', function(t) {
var schema = {
type: 'object',
properties: {
hello: {
type: 'string',
default: 'world'
}
}
};

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

t.ok(validate(value), 'should be valid');
t.deepEqual(value, {hello: 'world'}, 'should set default');
t.end();
});

tape('default on tuple', function(t) {
var schema = {
type: 'array',
items: [
{
type: 'string',
default: 'val1'
}, {
type: 'string',
default: 'val2'
}
]
};

var validate = validator(schema, {
setDefault: true
});
var value = [];

t.ok(validate(value), 'should be valid');
t.deepEqual(value, ['val1', 'val2'], 'should set default');
t.end();
});

tape('nested default value', function(t) {
var schema = {
type: 'object',
properties: {
obj: {
type: 'object',
properties: {
prop1: {type: 'string', default: 'val1'},
prop2: {type: 'string'}
},
default: {prop2: 'val2'},
required: ['prop2']
}
}
};

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

var value = {};
t.ok(validate(value), 'should be valid');
t.deepEqual(value, {
obj: {prop1: 'val1', prop2: 'val2'}
}, 'should set default');

var value = {obj: {prop1: 'val3', prop2: 'val4'}};
t.ok(validate(value), 'should be valid');
t.deepEqual(value, {
obj: {prop1: 'val3', prop2: 'val4'}
}, 'should override defaults');

t.end();
});

tape('default on arrays', function(t) {
var schema = {
type: 'array',
items: {
type: 'string',
default: 'val1'
},
additionalItems: {
type: 'string',
default: 'val2'
}
};

var validate = validator(schema, {
setDefault: true
});
var value = [];

t.ok(validate(value), 'should be valid');
t.deepEqual(value, [], 'should set default');
t.end();
});

tape('default on object (deactivated by default)', function(t) {
var schema = {
type: 'object',
properties: {
hello: {
type: 'string',
default: 'world'
}
}
};

var validate = validator(schema);
var value = {};

t.ok(validate(value), 'should be valid');
t.deepEqual(value, {}, 'should not set default');
t.end();
});

0 comments on commit c5629d2

Please sign in to comment.