multipleOf.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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) { /* istanbul ignore if */
  63. if (it.async) {
  64. out += ' throw new ValidationError([' + (__err) + ']); ';
  65. } else {
  66. out += ' validate.errors = [' + (__err) + ']; return false; ';
  67. }
  68. } else {
  69. out += ' var err = ' + (__err) + '; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
  70. }
  71. out += '} ';
  72. if ($breakOnError) {
  73. out += ' else { ';
  74. }
  75. return out;
  76. }