Skip to content

Commit

Permalink
feat: make "properties" a dependency of "allRequired"
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Feb 9, 2019
1 parent 13f692a commit da6e030
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -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: {
Expand Down
13 changes: 6 additions & 7 deletions keywords/allRequired.js
Expand Up @@ -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);
Expand Down
47 changes: 47 additions & 0 deletions 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 });
});
});
});
});
18 changes: 0 additions & 18 deletions spec/tests/allRequired.json
Expand Up @@ -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
}
]
}
]

0 comments on commit da6e030

Please sign in to comment.