patternRequired.js 2.2 KB

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