-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
127 additions
and
1 deletion.
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,37 @@ | ||
'use strict'; | ||
|
||
module.exports = function defFunc(ajv) { | ||
defFunc.definition = { | ||
compile: function(keys, parentSchema, it) { | ||
var equal = it.util.equal; | ||
return function(data) { | ||
var keyData = keys.reduce(function (props, key) { | ||
props[key] = []; | ||
return props; | ||
}, {}); | ||
|
||
var foundCollision = !!data.find(function (item) { | ||
return !!keys.find(function (key) { | ||
var value = item[key]; | ||
var foundValue = keyData[key].find(function (storedValue) { | ||
return equal(value, storedValue); | ||
}); | ||
if (!foundValue) { | ||
keyData[key].push(value); | ||
return false; | ||
} | ||
return true; | ||
}); | ||
}); | ||
|
||
return !foundCollision; | ||
}; | ||
}, | ||
metaSchema: { | ||
type: 'array' | ||
} | ||
}; | ||
|
||
ajv.addKeyword('uniqueItemProperties', 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,88 @@ | ||
[ | ||
{ | ||
"description": "uniqueItemProperties keyword validation with single property", | ||
"schema": { | ||
"type": "array", | ||
"uniqueItemProperties": ["id"] | ||
}, | ||
"tests": [ | ||
{ | ||
"description": "with all unique ids", | ||
"data": [ | ||
{ "id": 1 }, | ||
{ "id": 2 }, { "id": 3 } ], "valid": true | ||
}, | ||
{ | ||
"description": "without unique ids", | ||
"data": [ | ||
{ "id": 1 }, | ||
{ "id": 1 }, | ||
{ "id": 3 } | ||
], | ||
"valid": false | ||
}, | ||
{ | ||
"description": "with all unique object-ids", | ||
"data": [ | ||
{ "id": { "_id": 1, "date": 1495213151726 } }, | ||
{ "id": { "_id": 2, "date": 1495213151727 } }, | ||
{ "id": { "_id": 3, "date": 1495213151728 } } | ||
], | ||
"valid": true | ||
}, | ||
{ | ||
"description": "without unique object-ids", | ||
"data": [ | ||
{ "id": { "_id": 1, "date": 1495213151726 } }, | ||
{ "id": { "_id": 1, "date": 1495213151726 } }, | ||
{ "id": { "_id": 3, "date": 1495213151728 } } | ||
], | ||
"valid": false | ||
} | ||
] | ||
}, | ||
{ | ||
"description": "uniqueItemProperties keyword validation with multiple properties", | ||
"schema": { | ||
"type": "array", | ||
"uniqueItemProperties": ["id", "name"] | ||
}, | ||
"tests": [ | ||
{ | ||
"description": "with all unique ids and names", | ||
"data": [ | ||
{ "id": 1, "name": "taco" }, | ||
{ "id": 2, "name": "burrito" }, | ||
{ "id": 3, "name": "salsa" } | ||
], | ||
"valid": true | ||
}, | ||
{ | ||
"description": "with unique ids but not unique names", | ||
"data": [ | ||
{ "id": 1, "name": "taco" }, | ||
{ "id": 2, "name": "taco" }, | ||
{ "id": 3, "name": "salsa" } | ||
], | ||
"valid": false | ||
} | ||
] | ||
}, | ||
{ "description": "uniqueItemProperties keyword validation with no properties", | ||
"schema": { | ||
"type": "array", | ||
"uniqueItemProperties": [] | ||
}, | ||
"tests": [ | ||
{ | ||
"description": "with deepEqual like objects", | ||
"data": [ | ||
{ "id": 1 }, | ||
{ "id": 1 }, | ||
{ "id": 1 } | ||
], | ||
"valid": true | ||
} | ||
] | ||
} | ||
] |