Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: anyRequired keyword, closes #85
- Loading branch information
1 parent
87a637f
commit 4bc66b4
Showing
5 changed files
with
137 additions
and
0 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,24 @@ | ||
'use strict'; | ||
|
||
module.exports = function defFunc(ajv) { | ||
defFunc.definition = { | ||
type: 'object', | ||
macro: function (schema) { | ||
if (schema.length == 0) return {}; | ||
if (schema.length == 1) return { required: schema }; | ||
var schemas = schema.map(function (prop) { | ||
return { required: [prop] }; | ||
}); | ||
return { anyOf: schemas }; | ||
}, | ||
metaSchema: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
} | ||
} | ||
}; | ||
|
||
ajv.addKeyword('anyRequired', 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
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,86 @@ | ||
[ | ||
{ | ||
"description": "anyRequired requires that at least on property in the list is present", | ||
"schema": { | ||
"anyRequired": ["foo"] | ||
}, | ||
"tests": [ | ||
{ | ||
"description": "property present is valid", | ||
"data": {"foo": 1}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "property present with an additional property is valid", | ||
"data": {"foo": 1, "baz": 3}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "no property present is invalid", | ||
"data": { "baz": 1 }, | ||
"valid": false | ||
}, | ||
{ | ||
"description": "empty object is invalid", | ||
"data": {}, | ||
"valid": false | ||
} | ||
] | ||
}, | ||
{ | ||
"description": "multiple properties in prohibited", | ||
"schema": { | ||
"anyRequired": ["foo", "bar"] | ||
}, | ||
"tests": [ | ||
{ | ||
"description": "property present is valid", | ||
"data": {"foo": 1}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "property present with an additional property is valid", | ||
"data": {"foo": 1, "baz": 3}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "all properties present is valid", | ||
"data": {"foo": 1, "bar": 2}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "all properties present with an additional property is valid", | ||
"data": {"foo": 1, "bar": 2, "baz": 3}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "no property present is invalid", | ||
"data": {"baz": 3}, | ||
"valid": false | ||
}, | ||
{ | ||
"description": "empty object is invalid", | ||
"data": {}, | ||
"valid": false | ||
} | ||
] | ||
}, | ||
{ | ||
"description": "anyRequired: [] is always valid", | ||
"schema": { | ||
"anyRequired": [] | ||
}, | ||
"tests": [ | ||
{ | ||
"description": "any object is valid", | ||
"data": {"foo": 1}, | ||
"valid": true | ||
}, | ||
{ | ||
"description": "empty object is valid", | ||
"data": {}, | ||
"valid": true | ||
} | ||
] | ||
} | ||
] |