diff --git a/README.md b/README.md index b4967fd..2ef6533 100644 --- a/README.md +++ b/README.md @@ -319,10 +319,10 @@ The value of this keyword must be boolean. If the value of the keyword is `false`, the validation succeeds. -If the properties keyword is not used in the same schema object, the validation succeeds. - If the value of the keyword is `true`, the validation succeeds if the data contains all properties defined in `properties` keyword (in the same schema object). +If the `properties` keyword is not present in the same schema object, schema compilation will throw exception. + ```javascript var schema = { properties: { diff --git a/keywords/allRequired.js b/keywords/allRequired.js index 9d694cb..afc73eb 100644 --- a/keywords/allRequired.js +++ b/keywords/allRequired.js @@ -4,14 +4,13 @@ module.exports = function defFunc(ajv) { defFunc.definition = { type: 'object', macro: function (schema, parentSchema) { - if (!schema) return {}; - var properties = Object.keys(parentSchema.properties || {}); - if (properties.length == 0) return {}; - return { required: properties }; + if (!schema) return true; + var properties = Object.keys(parentSchema.properties); + if (properties.length == 0) return true; + return {required: properties}; }, - metaSchema: { - type: 'boolean' - } + metaSchema: {type: 'boolean'}, + dependencies: ['properties'] }; ajv.addKeyword('allRequired', defFunc.definition); diff --git a/spec/allRequired.spec.js b/spec/allRequired.spec.js new file mode 100644 index 0000000..c979a98 --- /dev/null +++ b/spec/allRequired.spec.js @@ -0,0 +1,47 @@ +'use strict'; + +var Ajv = require('ajv'); +var ajvPack = require('ajv-pack'); +var defFunc = require('../keywords/allRequired'); +var defineKeywords = require('..'); +var should = require('chai').should(); + + +describe('keyword "allRequired"', function() { + var ajvs = [ + defFunc(new Ajv), + defineKeywords(new Ajv, 'allRequired'), + defineKeywords(new Ajv), + defFunc(ajvPack.instance(new Ajv({sourceCode: true}))) + ]; + + ajvs.forEach(function (ajv, i) { + it('should validate that all defined properties are present #' + i, function() { + var schema = { + properties: { + foo: true, + bar: true + }, + allRequired: true + }; + ajv.validate(schema, {foo: 1, bar: 2}) .should.equal(true); + ajv.validate(schema, {foo: 1}) .should.equal(false); + }); + }); + + ajvs.forEach(function (ajv, i) { + it('should throw when properties is absent #' + i, function() { + should.throw(function() { + ajv.compile({ allRequired: true }); + }); + }); + }); + + ajvs.forEach(function (ajv, i) { + it('should throw when allRequired schema is invalid #' + i, function() { + should.throw(function() { + ajv.compile({ properties: {foo: true}, allRequired: 1 }); + }); + }); + }); +}); diff --git a/spec/tests/allRequired.json b/spec/tests/allRequired.json index d81dbac..125bff3 100644 --- a/spec/tests/allRequired.json +++ b/spec/tests/allRequired.json @@ -82,23 +82,5 @@ "valid": true } ] - }, - { - "description": "allRequired passed validation if properties keyword is absent", - "schema": { - "allRequired": true - }, - "tests": [ - { - "description": "any object is valid", - "data": {"foo": 1, "bar": 2}, - "valid": true - }, - { - "description": "empty object is valid", - "data": {}, - "valid": true - } - ] } ]