Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uniqueItems.js 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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', 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) { /* istanbul ignore if */
  67. if (it.async) {
  68. out += ' throw new ValidationError([' + (__err) + ']); ';
  69. } else {
  70. out += ' validate.errors = [' + (__err) + ']; return false; ';
  71. }
  72. } else {
  73. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  74. }
  75. out += ' } ';
  76. if ($breakOnError) {
  77. out += ' else { ';
  78. }
  79. } else {
  80. if ($breakOnError) {
  81. out += ' if (true) { ';
  82. }
  83. }
  84. return out;
  85. }