diff --git a/.eslintrc.yml b/.eslintrc.yml index 69a728b..00c187e 100644 --- a/.eslintrc.yml +++ b/.eslintrc.yml @@ -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 diff --git a/keywords/uniqueItemProperties.js b/keywords/uniqueItemProperties.js index 94df2fa..cd670da 100644 --- a/keywords/uniqueItemProperties.js +++ b/keywords/uniqueItemProperties.js @@ -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; @@ -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; } } diff --git a/spec/tests/uniqueItemProperties.json b/spec/tests/uniqueItemProperties.json index 04caf6a..2d77f0e 100644 --- a/spec/tests/uniqueItemProperties.json +++ b/spec/tests/uniqueItemProperties.json @@ -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 + } + ] } ]