uniqueItems.jst 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. {{# def.definitions }}
  2. {{# def.errors }}
  3. {{# def.setupKeyword }}
  4. {{# def.$data }}
  5. {{? ($schema || $isData) && it.opts.uniqueItems !== false }}
  6. {{? $isData }}
  7. var {{=$valid}};
  8. if ({{=$schemaValue}} === false || {{=$schemaValue}} === undefined)
  9. {{=$valid}} = true;
  10. else if (typeof {{=$schemaValue}} != 'boolean')
  11. {{=$valid}} = false;
  12. else {
  13. {{?}}
  14. var i = {{=$data}}.length
  15. , {{=$valid}} = true
  16. , j;
  17. if (i > 1) {
  18. {{
  19. var $itemType = it.schema.items && it.schema.items.type
  20. , $typeIsArray = Array.isArray($itemType);
  21. }}
  22. {{? !$itemType || $itemType == 'object' || $itemType == 'array' ||
  23. ($typeIsArray && ($itemType.indexOf('object') >= 0 || $itemType.indexOf('array') >= 0)) }}
  24. outer:
  25. for (;i--;) {
  26. for (j = i; j--;) {
  27. if (equal({{=$data}}[i], {{=$data}}[j])) {
  28. {{=$valid}} = false;
  29. break outer;
  30. }
  31. }
  32. }
  33. {{??}}
  34. var itemIndices = {}, item;
  35. for (;i--;) {
  36. var item = {{=$data}}[i];
  37. {{ var $method = 'checkDataType' + ($typeIsArray ? 's' : ''); }}
  38. if ({{= it.util[$method]($itemType, 'item', true) }}) continue;
  39. {{? $typeIsArray}}
  40. if (typeof item == 'string') item = '"' + item;
  41. {{?}}
  42. if (typeof itemIndices[item] == 'number') {
  43. {{=$valid}} = false;
  44. j = itemIndices[item];
  45. break;
  46. }
  47. itemIndices[item] = i;
  48. }
  49. {{?}}
  50. }
  51. {{? $isData }} } {{?}}
  52. if (!{{=$valid}}) {
  53. {{# def.error:'uniqueItems' }}
  54. } {{? $breakOnError }} else { {{?}}
  55. {{??}}
  56. {{? $breakOnError }} if (true) { {{?}}
  57. {{?}}