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.

58 lines
2.5 KiB

4 years ago
  1. 'use strict';
  2. module.exports = function generate_patternRequired(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 $key = 'key' + $lvl,
  13. $idx = 'idx' + $lvl,
  14. $matched = 'patternMatched' + $lvl,
  15. $dataProperties = 'dataProperties' + $lvl,
  16. $closingBraces = '',
  17. $ownProperties = it.opts.ownProperties;
  18. out += 'var ' + ($valid) + ' = true;';
  19. if ($ownProperties) {
  20. out += ' var ' + ($dataProperties) + ' = undefined;';
  21. }
  22. var arr1 = $schema;
  23. if (arr1) {
  24. var $pProperty, i1 = -1,
  25. l1 = arr1.length - 1;
  26. while (i1 < l1) {
  27. $pProperty = arr1[i1 += 1];
  28. out += ' var ' + ($matched) + ' = false; ';
  29. if ($ownProperties) {
  30. out += ' ' + ($dataProperties) + ' = ' + ($dataProperties) + ' || Object.keys(' + ($data) + '); for (var ' + ($idx) + '=0; ' + ($idx) + '<' + ($dataProperties) + '.length; ' + ($idx) + '++) { var ' + ($key) + ' = ' + ($dataProperties) + '[' + ($idx) + ']; ';
  31. } else {
  32. out += ' for (var ' + ($key) + ' in ' + ($data) + ') { ';
  33. }
  34. out += ' ' + ($matched) + ' = ' + (it.usePattern($pProperty)) + '.test(' + ($key) + '); if (' + ($matched) + ') break; } ';
  35. var $missingPattern = it.util.escapeQuotes($pProperty);
  36. out += ' if (!' + ($matched) + ') { ' + ($valid) + ' = false; var err = '; /* istanbul ignore else */
  37. if (it.createErrors !== false) {
  38. out += ' { keyword: \'' + ('patternRequired') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { missingPattern: \'' + ($missingPattern) + '\' } ';
  39. if (it.opts.messages !== false) {
  40. out += ' , message: \'should have property matching pattern \\\'' + ($missingPattern) + '\\\'\' ';
  41. }
  42. if (it.opts.verbose) {
  43. out += ' , schema: validate.schema' + ($schemaPath) + ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  44. }
  45. out += ' } ';
  46. } else {
  47. out += ' {} ';
  48. }
  49. out += '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; } ';
  50. if ($breakOnError) {
  51. $closingBraces += '}';
  52. out += ' else { ';
  53. }
  54. }
  55. }
  56. out += '' + ($closingBraces);
  57. return out;
  58. }