Skip to content

Commit

Permalink
Merge pull request #104 from epoberezkin/uniqueItemProperties-null-item
Browse files Browse the repository at this point in the history
Unique item properties with null item
  • Loading branch information
epoberezkin committed Jul 6, 2019
2 parents 9ebcae2 + d30b9a3 commit 6f9b7ff
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Expand Up @@ -19,7 +19,7 @@ rules:
# no-console: [ 2, { allow: [ warn, error ] } ]
no-console: 0
block-scoped-var: 2
complexity: [ 2, 15 ]
complexity: [ 2, 18 ]
curly: [ 2, multi-or-nest, consistent ]
dot-location: [ 2, property ]
dot-notation: 2
Expand Down
6 changes: 3 additions & 3 deletions keywords/uniqueItemProperties.js
Expand Up @@ -16,7 +16,7 @@ module.exports = function defFunc(ajv) {
if (scalar[k]) {
var hash = {};
for (i = data.length; i--;) {
if (typeof data[i] != 'object') continue;
if (!data[i] || typeof data[i] != 'object') continue;
var prop = data[i][key];
if (prop && typeof prop == 'object') continue;
if (typeof prop == 'string') prop = '"' + prop;
Expand All @@ -25,9 +25,9 @@ module.exports = function defFunc(ajv) {
}
} else {
for (i = data.length; i--;) {
if (typeof data[i] != 'object') continue;
if (!data[i] || typeof data[i] != 'object') continue;
for (var j = i; j--;) {
if (typeof data[j] == 'object' && equal(data[i][key], data[j][key]))
if (data[j] && typeof data[j] == 'object' && equal(data[i][key], data[j][key]))
return false;
}
}
Expand Down
68 changes: 68 additions & 0 deletions spec/tests/uniqueItemProperties.json
Expand Up @@ -395,5 +395,73 @@
"valid": false
}
]
},
{
"description": "uniqueItemProperties keyword with null item(s)",
"schema": {
"type": "array",
"uniqueItemProperties": ["id"],
"items": {
"properties": {
"id": {"type": "integer"}
}
}
},
"tests": [
{
"description": "with all unique ids and null items is valid",
"data": [
{ "id": 1 },
{ "id": 2 },
null,
null
],
"valid": true
},
{
"description": "with non-unique ids and null item is invalid",
"data": [
{ "id": 1 },
{ "id": 1 },
null,
null
],
"valid": false
}
]
},
{
"description": "uniqueItemProperties keyword with null item(s) and object keys",
"schema": {
"type": "array",
"uniqueItemProperties": ["id"],
"items": {
"properties": {
"id": {"type": "object"}
}
}
},
"tests": [
{
"description": "with all unique ids and null items is valid",
"data": [
{ "id": {"_id": 1} },
{ "id": {"_id": 2} },
null,
null
],
"valid": true
},
{
"description": "with non-unique ids and null item is invalid",
"data": [
{ "id": {"_id": 1} },
{ "id": {"_id": 1} },
null,
null
],
"valid": false
}
]
}
]

0 comments on commit 6f9b7ff

Please sign in to comment.