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

Added {useDefault: true} option flag to substituted in default values #55

Open
wants to merge 1 commit 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
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();
});