Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allRequired keyword, closes #79
- Loading branch information
1 parent
16a5695
commit 87a637f
Showing
6 changed files
with
167 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
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 }; | ||
}, | ||
metaSchema: { | ||
type: 'boolean' | ||
} | ||
}; | ||
|
||
ajv.addKeyword('allRequired', defFunc.definition); | ||
return ajv; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,3 @@ module.exports = function defFunc(ajv) { | |
ajv.addKeyword('prohibited', defFunc.definition); | ||
return ajv; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
[ | ||
{ | ||
"description": "allRequired: true requires the presense of all defined properties", | ||
"schema": { | ||
"properties": { | ||
"foo": true, | ||
"bar": true | ||
}, | ||
"allRequired": true | ||
}, | ||
"tests": [ | ||
{ | ||
"description": "all defined properties present is valid", | ||
"data": {"foo": 1, "bar": 2}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "all defined properties present with an additional property is valid", | ||
"data": {"foo": 1, "bar": 2, "baz": 3}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "some of defined properties present is invalid", | ||
"data": {"foo": 1}, | ||
"valid": false | ||
}, | ||
{ | ||
"description": "some of defined properties present with an additional property is invalid", | ||
"data": {"foo": 1, "baz": 3}, | ||
"valid": false | ||
}, | ||
{ | ||
"description": "none of defined properties present is invalid", | ||
"data": {"baz": 3}, | ||
"valid": false | ||
}, | ||
{ | ||
"description": "empty object is invalid", | ||
"data": {}, | ||
"valid": false | ||
} | ||
] | ||
}, | ||
{ | ||
"description": "allRequired: false is always ignored", | ||
"schema": { | ||
"properties": { | ||
"foo": true, | ||
"bar": true | ||
}, | ||
"allRequired": false | ||
}, | ||
"tests": [ | ||
{ | ||
"description": "all defined properties present is valid", | ||
"data": {"foo": 1, "bar": 2}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "all defined properties present with an additional property is valid", | ||
"data": {"foo": 1, "bar": 2, "baz": 3}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "some of defined properties present is valid", | ||
"data": {"foo": 1}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "some of defined properties present with an additional property is valid", | ||
"data": {"foo": 1, "baz": 3}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "none of defined properties present is valid", | ||
"data": {"baz": 3}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "empty object is valid", | ||
"data": {}, | ||
"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 | ||
} | ||
] | ||
} | ||
] |