multipleOf.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use strict';
  2. module.exports = function generate_multipleOf(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 $isData = it.opts.$data && $schema && $schema.$data,
  12. $schemaValue;
  13. if ($isData) {
  14. out += ' var schema' + ($lvl) + ' = ' + (it.util.getData($schema.$data, $dataLvl, it.dataPathArr)) + '; ';
  15. $schemaValue = 'schema' + $lvl;
  16. } else {
  17. $schemaValue = $schema;
  18. }
  19. out += 'var division' + ($lvl) + ';if (';
  20. if ($isData) {
  21. out += ' ' + ($schemaValue) + ' !== undefined && ( typeof ' + ($schemaValue) + ' != \'number\' || ';
  22. }
  23. out += ' (division' + ($lvl) + ' = ' + ($data) + ' / ' + ($schemaValue) + ', ';
  24. if (it.opts.multipleOfPrecision) {
  25. out += ' Math.abs(Math.round(division' + ($lvl) + ') - division' + ($lvl) + ') > 1e-' + (it.opts.multipleOfPrecision) + ' ';
  26. } else {
  27. out += ' division' + ($lvl) + ' !== parseInt(division' + ($lvl) + ') ';
  28. }
  29. out += ' ) ';
  30. if ($isData) {
  31. out += ' ) ';
  32. }
  33. out += ' ) { ';
  34. var $$outStack = $$outStack || [];
  35. $$outStack.push(out);
  36. out = ''; /* istanbul ignore else */
  37. if (it.createErrors !== false) {
  38. out += ' { keyword: \'' + ('multipleOf') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { multipleOf: ' + ($schemaValue) + ' } ';
  39. if (it.opts.messages !== false) {
  40. out += ' , message: \'should be multiple of ';
  41. if ($isData) {
  42. out += '\' + ' + ($schemaValue);
  43. } else {
  44. out += '' + ($schemaValue) + '\'';
  45. }
  46. }
  47. if (it.opts.verbose) {
  48. out += ' , schema: ';
  49. if ($isData) {
  50. out += 'validate.schema' + ($schemaPath);
  51. } else {
  52. out += '' + ($schema);
  53. }
  54. out += ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  55. }
  56. out += ' } ';
  57. } else {
  58. out += ' {} ';
  59. }
  60. var __err = out;
  61. out = $$outStack.pop();
  62. if (!it.compositeRule && $breakOnError) {
  63. /* istanbul ignore if */
  64. if (it.async) {
  65. out += ' throw new ValidationError([' + (__err) + ']); ';
  66. } else {
  67. out += ' validate.errors = [' + (__err) + ']; return false; ';
  68. }
  69. } else {
  70. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  71. }
  72. out += '} ';
  73. if ($breakOnError) {
  74. out += ' else { ';
  75. }
  76. return out;
  77. }