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

Minor tweaks regarding required: true (encourage v4-style required) #53

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ Simply pass a schema to compile it
var validator = require('is-my-json-valid')

var validate = validator({
required: true,
type: 'object',
properties: {
hello: {
required: true,
type: 'string'
}
}
},
required: ['hello']
})

console.log('should be valid', validate({hello: 'world'}))
Expand Down Expand Up @@ -58,7 +57,6 @@ If you want to add your own custom formats pass them as the formats options to t
``` js
var validate = validator({
type: 'string',
required: true,
format: 'only-a'
}, {
formats: {
Expand All @@ -76,7 +74,6 @@ You can pass in external schemas that you reference using the `$ref` attribute a

``` js
var ext = {
required: true,
type: 'string'
}

Expand All @@ -97,11 +94,11 @@ is-my-json-valid supports filtering away properties not in the schema

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

Expand All @@ -115,14 +112,13 @@ is-my-json-valid outputs the value causing an error when verbose is set to true

``` js
var validate = validator({
required: true,
type: 'object',
properties: {
hello: {
required: true,
type: 'string'
}
}
},
required: ['hello']
}, {
verbose: true
})
Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ var compile = function(schema, cache, root, reporter, opts) {
}
}

if (node.required === true) {
if (node.required === true && opts.requiredV3 === true) {
indent++
validate('if (%s === undefined) {', name)
error('is required')
Expand Down Expand Up @@ -511,6 +511,9 @@ var compile = function(schema, cache, root, reporter, opts) {

var validate = genfun
('function validate(data) {')
('if (data === undefined) {')
('throw new Error("`undefined` is not a valid JSON value");')
('}')
('validate.errors = null')
('var errors = 0')

Expand Down Expand Up @@ -541,7 +544,7 @@ var compile = function(schema, cache, root, reporter, opts) {

module.exports = function(schema, opts) {
if (typeof schema === 'string') schema = JSON.parse(schema)
return compile(schema, {}, schema, true, opts)
return compile(schema, {}, schema, true, xtend({requiredV3: true}, opts))
}

module.exports.filter = function(schema, opts) {
Expand Down
42 changes: 40 additions & 2 deletions test/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ tape('simple', function(t) {
var validate = validator(schema)

t.ok(validate({hello: 'world'}), 'should be valid')
t.notOk(validate(), 'should be invalid')
t.notOk(validate(null), 'should be invalid')
t.notOk(validate({}), 'should be invalid')
t.end()
})
Expand Down Expand Up @@ -111,7 +111,7 @@ tape('array', function(t) {
})

t.notOk(validate({}), 'wrong type')
t.notOk(validate(), 'is required')
t.notOk(validate(null), 'is required')
t.ok(validate(['test']))
t.end()
})
Expand Down Expand Up @@ -364,3 +364,41 @@ tape('Date.now() is an integer', function(t) {
t.ok(validate(Date.now()), 'is integer')
t.end()
})

tape('undefined is invalid input', function(t) {
var schema = {type: 'array'}
var validate = validator(schema)
t.throws(function() {
validate(undefined)
}, 'undefined is not valid JSON')
t.end()
})

tape('disable v3-style required', function(t) {
var schema = {
type: 'object',
properties: {
prop: {
type: 'string',
required: true
}
}
};
var validateV4 = validator(schema, {
requiredV3: false
});
var validateV3 = validator(schema, {
requiredV3: true
});
var validate = validator(schema);
// Sanity check that both can validate with the property
t.ok(validateV4({prop: 'a string'}), 'should validate with prop');
t.ok(validateV3({prop: 'a string'}), 'should validate with prop');
// Check that requiredV3: false causes `required: true` to be ignored
t.ok(validateV4({}), 'v4 should validate without prop');
t.notOk(validateV3({}), 'v3 should not validate without prop');
// Check that `requiredV3: true` is default
t.ok(validate({prop: 'a string'}), 'should validate with prop');
t.notOk(validate({}), 'v3 should not validate without prop');
t.end()
})