uniqueItems.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. module.exports = function generate_uniqueItems(it, $keyword, $ruleType) {
  3. var out = ' ';
  4. var $lvl = it.level;
  5. var $dataLvl = it.dataLevel;
  6. var $schema = it.schema[$keyword];
  7. var $schemaPath = it.schemaPath + it.util.getProperty($keyword);
  8. var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
  9. var $breakOnError = !it.opts.allErrors;
  10. var $data = 'data' + ($dataLvl || '');
  11. var $valid = 'valid' + $lvl;
  12. var $isData = it.opts.$data && $schema && $schema.$data,
  13. $schemaValue;
  14. if ($isData) {
  15. out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
  16. $schemaValue = 'schema' + $lvl;
  17. } else {
  18. $schemaValue = $schema;
  19. }
  20. if (($schema || $isData) && it.opts.uniqueItems !== false) {
  21. if ($isData) {
  22. out += ' var ' + ($valid) + '; if (' + ($schemaValue) + ' === false || ' + ($schemaValue) + ' === undefined) ' + ($valid) + ' = true; else if (typeof ' + ($schemaValue) + ' != \'boolean\') ' + ($valid) + ' = false; else { ';
  23. }
  24. out += ' var i = ' + ($data) + '.length , ' + ($valid) + ' = true , j; if (i > 1) { ';
  25. var $itemType = it.schema.items && it.schema.items.type,
  26. $typeIsArray = Array.isArray($itemType);
  27. if (!$itemType || $itemType == 'object' || $itemType == 'array' || ($typeIsArray && ($itemType.indexOf('object') >= 0 || $itemType.indexOf('array') >= 0))) {
  28. out += ' outer: for (;i--;) { for (j = i; j--;) { if (equal(' + ($data) + '[i], ' + ($data) + '[j])) { ' + ($valid) + ' = false; break outer; } } } ';
  29. } else {
  30. out += ' var itemIndices = {}, item; for (;i--;) { var item = ' + ($data) + '[i]; ';
  31. var $method = 'checkDataType' + ($typeIsArray ? 's' : '');
  32. out += ' if (' + (it.util[$method]($itemType, 'item', it.opts.strictNumbers, true)) + ') continue; ';
  33. if ($typeIsArray) {
  34. out += ' if (typeof item == \'string\') item = \'"\' + item; ';
  35. }
  36. out += ' if (typeof itemIndices[item] == \'number\') { ' + ($valid) + ' = false; j = itemIndices[item]; break; } itemIndices[item] = i; } ';
  37. }
  38. out += ' } ';
  39. if ($isData) {
  40. out += ' } ';
  41. }
  42. out += ' if (!' + ($valid) + ') { ';
  43. var $$outStack = $$outStack || [];
  44. $$outStack.push(out);
  45. out = ''; /* istanbul ignore else */
  46. if (it.createErrors !== false) {
  47. out += ' { keyword: \'' + ('uniqueItems') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { i: i, j: j } ';
  48. if (it.opts.messages !== false) {
  49. out += ' , message: \'should NOT have duplicate items (items ## \' + j + \' and \' + i + \' are identical)\' ';
  50. }
  51. if (it.opts.verbose) {
  52. out += ' , schema: ';
  53. if ($isData) {
  54. out += 'validate.schema' + ($schemaPath);
  55. } else {
  56. out += '' + ($schema);
  57. }
  58. out += ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  59. }
  60. out += ' } ';
  61. } else {
  62. out += ' {} ';
  63. }
  64. var __err = out;
  65. out = $$outStack.pop();
  66. if (!it.compositeRule && $breakOnError) {
  67. /* istanbul ignore if */
  68. if (it.async) {
  69. out += ' throw new ValidationError([' + (__err) + ']); ';
  70. } else {
  71. out += ' validate.errors = [' + (__err) + ']; return false; ';
  72. }
  73. } else {
  74. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  75. }
  76. out += ' } ';
  77. if ($breakOnError) {
  78. out += ' else { ';
  79. }
  80. } else {
  81. if ($breakOnError) {
  82. out += ' if (true) { ';
  83. }
  84. }
  85. return out;
  86. }