From 286368fb2380939da60dc694bca05618a9f32d19 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin Date: Sun, 16 Aug 2015 01:10:43 +0100 Subject: [PATCH] optimized required rule compilation --- lib/dot/required.jst | 4 ++-- lib/dotjs/required.js | 36 ++++++++---------------------------- 2 files changed, 10 insertions(+), 30 deletions(-) diff --git a/lib/dot/required.jst b/lib/dot/required.jst index 6a7fdb1..1cd73c1 100644 --- a/lib/dot/required.jst +++ b/lib/dot/required.jst @@ -53,7 +53,7 @@ it.errorPath = ($currentErrorPath + ' + \'' + $missingProperty + '\'').replace('" + "', ''); }} if ({{=$data}}{{=$prop}} === undefined) { - {{# def.error:'required' }} + {{# def.addError:'required' }} } {{~}} {{??}} @@ -61,7 +61,7 @@ for (var {{=$i}} = 0; {{=$i}} < schema{{=$lvl}}.length; {{=$i}}++) { if ({{=$data}}[schema{{=$lvl}}[{{=$i}}]] === undefined) { - {{# def.error:'required' }} + {{# def.addError:'required' }} } } {{?}} diff --git a/lib/dotjs/required.js b/lib/dotjs/required.js index 31bde50..3f8e56e 100644 --- a/lib/dotjs/required.js +++ b/lib/dotjs/required.js @@ -78,21 +78,11 @@ module.exports = function anonymous(it) { var $prop = it.util.getProperty($property), $missingProperty = it.util.escapeQuotes($prop); it.errorPath = ($currentErrorPath + ' + \'' + $missingProperty + '\'').replace('" + "', ''); - out += ' if (' + ($data) + ($prop) + ' === undefined) { '; - if (it.wasTop && $breakOnError) { - out += ' validate.errors = [ { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' '; - if (it.opts.verbose) { - out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); - } - out += ' }]; return false; '; - } else { - out += ' var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' '; - if (it.opts.verbose) { - out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); - } - out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; + out += ' if (' + ($data) + ($prop) + ' === undefined) { var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' '; + if (it.opts.verbose) { + out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); } - out += ' } '; + out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; } '; } } } else { @@ -101,21 +91,11 @@ module.exports = function anonymous(it) { $propertyPath = ' + schema' + $lvl + '[' + $i + '] + ', $missingProperty = '\' + "\'"' + $propertyPath + '"\'" + \''; it.errorPath = ($currentErrorPath + ' + "[\'"' + $propertyPath + '"\']"').replace('" + "', ''); - out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < schema' + ($lvl) + '.length; ' + ($i) + '++) { if (' + ($data) + '[schema' + ($lvl) + '[' + ($i) + ']] === undefined) { '; - if (it.wasTop && $breakOnError) { - out += ' validate.errors = [ { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' '; - if (it.opts.verbose) { - out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); - } - out += ' }]; return false; '; - } else { - out += ' var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' '; - if (it.opts.verbose) { - out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); - } - out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; '; + out += ' for (var ' + ($i) + ' = 0; ' + ($i) + ' < schema' + ($lvl) + '.length; ' + ($i) + '++) { if (' + ($data) + '[schema' + ($lvl) + '[' + ($i) + ']] === undefined) { var err = { keyword: \'' + ('required') + '\', dataPath: (dataPath || \'\') + ' + (it.errorPath) + ', message: \'property ' + ($missingProperty) + ' is required\' '; + if (it.opts.verbose) { + out += ', schema: validate.schema' + ($schemaPath) + ', data: ' + ($data); } - out += ' } } '; + out += ' }; if (validate.errors === null) validate.errors = [err]; else validate.errors.push(err); errors++; } } '; } } it.errorPath = $currentErrorPath;