diff --git a/README.md b/README.md index 47350d8..e617f8a 100644 --- a/README.md +++ b/README.md @@ -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'})) @@ -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: { @@ -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' } @@ -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 }) @@ -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 }) diff --git a/index.js b/index.js index 445f7db..53d11ed 100644 --- a/index.js +++ b/index.js @@ -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') @@ -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') @@ -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) { diff --git a/test/misc.js b/test/misc.js index 6f22d00..e512f6a 100644 --- a/test/misc.js +++ b/test/misc.js @@ -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() }) @@ -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() }) @@ -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() +})