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.

80 lines
2.7 KiB

4 years ago
  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. if (!($isData || typeof $schema == 'number')) {
  20. throw new Error($keyword + ' must be number');
  21. }
  22. out += 'var division' + ($lvl) + ';if (';
  23. if ($isData) {
  24. out += ' ' + ($schemaValue) + ' !== undefined && ( typeof ' + ($schemaValue) + ' != \'number\' || ';
  25. }
  26. out += ' (division' + ($lvl) + ' = ' + ($data) + ' / ' + ($schemaValue) + ', ';
  27. if (it.opts.multipleOfPrecision) {
  28. out += ' Math.abs(Math.round(division' + ($lvl) + ') - division' + ($lvl) + ') > 1e-' + (it.opts.multipleOfPrecision) + ' ';
  29. } else {
  30. out += ' division' + ($lvl) + ' !== parseInt(division' + ($lvl) + ') ';
  31. }
  32. out += ' ) ';
  33. if ($isData) {
  34. out += ' ) ';
  35. }
  36. out += ' ) { ';
  37. var $$outStack = $$outStack || [];
  38. $$outStack.push(out);
  39. out = ''; /* istanbul ignore else */
  40. if (it.createErrors !== false) {
  41. out += ' { keyword: \'' + ('multipleOf') + '\' , dataPath: (dataPath || \'\') + ' + (it.errorPath) + ' , schemaPath: ' + (it.util.toQuotedString($errSchemaPath)) + ' , params: { multipleOf: ' + ($schemaValue) + ' } ';
  42. if (it.opts.messages !== false) {
  43. out += ' , message: \'should be multiple of ';
  44. if ($isData) {
  45. out += '\' + ' + ($schemaValue);
  46. } else {
  47. out += '' + ($schemaValue) + '\'';
  48. }
  49. }
  50. if (it.opts.verbose) {
  51. out += ' , schema: ';
  52. if ($isData) {
  53. out += 'validate.schema' + ($schemaPath);
  54. } else {
  55. out += '' + ($schema);
  56. }
  57. out += ' , parentSchema: validate.schema' + (it.schemaPath) + ' , data: ' + ($data) + ' ';
  58. }
  59. out += ' } ';
  60. } else {
  61. out += ' {} ';
  62. }
  63. var __err = out;
  64. out = $$outStack.pop();
  65. if (!it.compositeRule && $breakOnError) {
  66. /* 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. return out;
  80. }