fix: uniqueItems when item type is array of types, closes #727

master
Evgeny Poberezkin 2018-03-03 10:35:40 +00:00
parent 33e2a41a94
commit 8bbc1a2107
2 changed files with 96 additions and 6 deletions

View File

@ -18,8 +18,12 @@
, {{=$valid}} = true
, j;
if (i > 1) {
{{ var $itemType = it.schema.items && it.schema.items.type; }}
{{? !$itemType || $itemType == 'object' || $itemType == 'array' }}
{{
var $itemType = it.schema.items && it.schema.items.type
, $typeIsArray = Array.isArray($itemType);
}}
{{? !$itemType || $itemType == 'object' || $itemType == 'array' ||
($typeIsArray && ($itemType.indexOf('object') >= 0 || $itemType.indexOf('array') >= 0)) }}
outer:
for (;i--;) {
for (j = i; j--;) {
@ -33,7 +37,11 @@
var itemIndices = {}, item;
for (;i--;) {
var item = {{=$data}}[i];
if (typeof item != '{{=$itemType}}') continue;
{{ var $method = 'checkDataType' + ($typeIsArray ? 's' : ''); }}
if ({{= it.util[$method]($itemType, 'item', true) }}) continue;
{{? $typeIsArray}}
if (typeof item == 'string') item = '"' + item;
{{?}}
if (typeof itemIndices[item] == 'number') {
{{=$valid}} = false;
j = itemIndices[item];

View File

@ -7,17 +7,17 @@
},
"tests": [
{
"description": "array of unique strings are valid",
"description": "array of unique strings is valid",
"data": ["foo", "bar", "baz"],
"valid": true
},
{
"description": "array of unique items with strings that are properties of hash are valid",
"description": "array of unique items with strings that are properties of hash is valid",
"data": ["toString", "foo"],
"valid": true
},
{
"description": "array of non-unique strings are invalid",
"description": "array of non-unique strings is invalid",
"data": ["foo", "bar", "bar"],
"valid": false
},
@ -27,5 +27,87 @@
"valid": false
}
]
},
{
"description": "uniqueItems with multiple types when the list of types includes array",
"schema": {
"items": { "type": ["array", "string"] },
"uniqueItems": true
},
"tests": [
{
"description": "array of unique items is valid",
"data": [[1], [2], "foo"],
"valid": true
},
{
"description": "array of non-unique items is invalid",
"data": [[1], [1], "foo"],
"valid": false
},
{
"description": "array with incorrect type is invalid",
"data": [{}, 1, 2],
"valid": false
}
]
},
{
"description": "uniqueItems with multiple types when the list of types includes object",
"schema": {
"items": { "type": ["object", "string"] },
"uniqueItems": true
},
"tests": [
{
"description": "array of unique items is valid",
"data": [{"a": 1}, {"b": 2}, "foo"],
"valid": true
},
{
"description": "array of non-unique items is invalid",
"data": [{"a": 1}, {"a": 1}, "foo"],
"valid": false
},
{
"description": "array with incorrect type is invalid",
"data": [[], 1, 2],
"valid": false
}
]
},
{
"description": "uniqueItems with multiple types when all types are scalar",
"schema": {
"items": { "type": ["number", "string", "boolean", "null"] },
"uniqueItems": true
},
"tests": [
{
"description": "array of unique items is valid (string/number)",
"data": ["1", 1, 2],
"valid": true
},
{
"description": "array of unique items is valid (string/boolean)",
"data": ["true", true, false],
"valid": true
},
{
"description": "array of unique items is valid (string/null)",
"data": ["null", null, 0],
"valid": true
},
{
"description": "array of non-unique items is invalid",
"data": [1, 1, 2],
"valid": false
},
{
"description": "array with incorrect type is invalid",
"data": [[], 1, 2],
"valid": false
}
]
}
]