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