uniqueItems.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 ' + ($valid) + ' = true; if (' + ($data) + '.length > 1) { var i = ' + ($data) + '.length, j; outer: for (;i--;) { for (j = i; j--;) { if (equal(' + ($data) + '[i], ' + ($data) + '[j])) { ' + ($valid) + ' = false; break outer; } } } } ';
  25. if ($isData) {
  26. out += ' } ';
  27. }
  28. out += ' if (!' + ($valid) + ') { ';
  29. var $$outStack = $$outStack || [];
  30. $$outStack.push(out);
  31. out = ''; /* istanbul ignore else */
  32. if (it.createErrors !== false) {
  33. out += ' { keyword: \'' + ('uniqueItems') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { i: i, j: j } ';
  34. if (it.opts.messages !== false) {
  35. out += ' , message: \'should NOT have duplicate items (items ## \' + j + \' and \' + i + \' are identical)\' ';
  36. }
  37. if (it.opts.verbose) {
  38. out += ' , schema: ';
  39. if ($isData) {
  40. out += 'validate.schema' + ($schemaPath);
  41. } else {
  42. out += '' + ($schema);
  43. }
  44. out += ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  45. }
  46. out += ' } ';
  47. } else {
  48. out += ' {} ';
  49. }
  50. var __err = out;
  51. out = $$outStack.pop();
  52. if (!it.compositeRule && $breakOnError) { /* istanbul ignore if */
  53. if (it.async) {
  54. out += ' throw new ValidationError([' + (__err) + ']); ';
  55. } else {
  56. out += ' validate.errors = [' + (__err) + ']; return false; ';
  57. }
  58. } else {
  59. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  60. }
  61. out += ' } ';
  62. if ($breakOnError) {
  63. out += ' else { ';
  64. }
  65. } else {
  66. if ($breakOnError) {
  67. out += ' if (true) { ';
  68. }
  69. }
  70. return out;
  71. }