2025-04-19 23:12:19 -04:00

115 lines
2.9 KiB
JavaScript

'use strict';
module.exports = function generate_multipleOf(it, $keyword, $ruleType) {
var out = ' ';
var $lvl = it.level;
var $dataLvl = it.dataLevel;
var $schema = it.schema[$keyword];
var $schemaPath = it.schemaPath + it.util.getProperty($keyword);
var $errSchemaPath = it.errSchemaPath + '/' + $keyword;
var $breakOnError = !it.opts.allErrors;
var $data = 'data' + ($dataLvl || '');
var $isData = it.opts.$data && $schema && $schema.$data,
$schemaValue;
if ($isData) {
out +=
' var schema' +
$lvl +
' = ' +
it.util.getData($schema.$data, $dataLvl, it.dataPathArr) +
'; ';
$schemaValue = 'schema' + $lvl;
} else {
$schemaValue = $schema;
}
if (!($isData || typeof $schema == 'number')) {
throw new Error($keyword + ' must be number');
}
out += 'var division' + $lvl + ';if (';
if ($isData) {
out +=
' ' +
$schemaValue +
' !== undefined && ( typeof ' +
$schemaValue +
" != 'number' || ";
}
out += ' (division' + $lvl + ' = ' + $data + ' / ' + $schemaValue + ', ';
if (it.opts.multipleOfPrecision) {
out +=
' Math.abs(Math.round(division' +
$lvl +
') - division' +
$lvl +
') > 1e-' +
it.opts.multipleOfPrecision +
' ';
} else {
out += ' division' + $lvl + ' !== parseInt(division' + $lvl + ') ';
}
out += ' ) ';
if ($isData) {
out += ' ) ';
}
out += ' ) { ';
var $$outStack = $$outStack || [];
$$outStack.push(out);
out = ''; /* istanbul ignore else */
if (it.createErrors !== false) {
out +=
" { keyword: '" +
'multipleOf' +
"' , dataPath: (dataPath || '') + " +
it.errorPath +
' , schemaPath: ' +
it.util.toQuotedString($errSchemaPath) +
' , params: { multipleOf: ' +
$schemaValue +
' } ';
if (it.opts.messages !== false) {
out += " , message: 'should be multiple of ";
if ($isData) {
out += "' + " + $schemaValue;
} else {
out += '' + $schemaValue + "'";
}
}
if (it.opts.verbose) {
out += ' , schema: ';
if ($isData) {
out += 'validate.schema' + $schemaPath;
} else {
out += '' + $schema;
}
out +=
' , parentSchema: validate.schema' +
it.schemaPath +
' , data: ' +
$data +
' ';
}
out += ' } ';
} else {
out += ' {} ';
}
var __err = out;
out = $$outStack.pop();
if (!it.compositeRule && $breakOnError) {
/* istanbul ignore if */
if (it.async) {
out += ' throw new ValidationError([' + __err + ']); ';
} else {
out += ' validate.errors = [' + __err + ']; return false; ';
}
} else {
out +=
' var err = ' +
__err +
'; if (vErrors === null) vErrors = [err]; else vErrors.push(err); errors++; ';
}
out += '} ';
if ($breakOnError) {
out += ' else { ';
}
return out;
};