Skip to content

Commit 332b30d

Browse files
authored
Merge pull request #1241 from ajv-validator/refactor
Refactor
2 parents 140cfa6 + 1105fd5 commit 332b30d

17 files changed

Lines changed: 72 additions & 97 deletions

lib/compile/util.js

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ module.exports = {
1313
ucs2length: require('./ucs2length'),
1414
varOccurences: varOccurences,
1515
varReplace: varReplace,
16-
cleanUpCode: cleanUpCode,
17-
finalCleanUpCode: finalCleanUpCode,
1816
schemaHasRules: schemaHasRules,
1917
schemaHasRulesExcept: schemaHasRulesExcept,
2018
schemaUnknownRules: schemaUnknownRules,
@@ -139,42 +137,6 @@ function varReplace(str, dataVar, expr) {
139137
}
140138

141139

142-
var EMPTY_ELSE = /else\s*{\s*}/g
143-
, EMPTY_IF_NO_ELSE = /if\s*\([^)]+\)\s*\{\s*\}(?!\s*else)/g
144-
, EMPTY_IF_WITH_ELSE = /if\s*\(([^)]+)\)\s*\{\s*\}\s*else(?!\s*if)/g;
145-
function cleanUpCode(out) {
146-
return out.replace(EMPTY_ELSE, '')
147-
.replace(EMPTY_IF_NO_ELSE, '')
148-
.replace(EMPTY_IF_WITH_ELSE, 'if (!($1))');
149-
}
150-
151-
152-
var ERRORS_REGEXP = /[^v.]errors/g
153-
, REMOVE_ERRORS = /var errors = 0;|var vErrors = null;|validate.errors = vErrors;/g
154-
, REMOVE_ERRORS_ASYNC = /var errors = 0;|var vErrors = null;/g
155-
, RETURN_VALID = 'return errors === 0;'
156-
, RETURN_TRUE = 'validate.errors = null; return true;'
157-
, RETURN_ASYNC = /if \(errors === 0\) return data;\s*else throw new ValidationError\(vErrors\);/
158-
, RETURN_DATA_ASYNC = 'return data;'
159-
, ROOTDATA_REGEXP = /[^A-Za-z_$]rootData[^A-Za-z0-9_$]/g
160-
, REMOVE_ROOTDATA = /if \(rootData === undefined\) rootData = data;/;
161-
162-
function finalCleanUpCode(out, async) {
163-
var matches = out.match(ERRORS_REGEXP);
164-
if (matches && matches.length == 2) {
165-
out = async
166-
? out.replace(REMOVE_ERRORS_ASYNC, '')
167-
.replace(RETURN_ASYNC, RETURN_DATA_ASYNC)
168-
: out.replace(REMOVE_ERRORS, '')
169-
.replace(RETURN_VALID, RETURN_TRUE);
170-
}
171-
172-
matches = out.match(ROOTDATA_REGEXP);
173-
if (!matches || matches.length !== 3) return out;
174-
return out.replace(REMOVE_ROOTDATA, '');
175-
}
176-
177-
178140
function schemaHasRules(schema, rules) {
179141
if (typeof schema == 'boolean') return !schema;
180142
for (var key in schema) if (rules[key]) return true;

lib/dot/_limit.jst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
, $op = $isMax ? '<' : '>'
1818
, $notOp = $isMax ? '>' : '<'
1919
, $errorKeyword = undefined;
20+
21+
if (!($isData || typeof $schema == 'number' || $schema === undefined)) {
22+
throw new Error($keyword + ' must be number');
23+
}
24+
if (!($isDataExcl || $schemaExcl === undefined
25+
|| typeof $schemaExcl == 'number'
26+
|| typeof $schemaExcl == 'boolean')) {
27+
throw new Error($exclusiveKeyword + ' must be number or boolean');
28+
}
2029
}}
2130

2231
{{? $isDataExcl }}

lib/dot/_limitItems.jst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
{{# def.setupKeyword }}
44
{{# def.$data }}
55

6+
{{# def.numberKeyword }}
7+
68
{{ var $op = $keyword == 'maxItems' ? '>' : '<'; }}
79
if ({{# def.$dataNotType:'number' }} {{=$data}}.length {{=$op}} {{=$schemaValue}}) {
810
{{ var $errorKeyword = $keyword; }}

lib/dot/_limitLength.jst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
{{# def.setupKeyword }}
44
{{# def.$data }}
55

6+
{{# def.numberKeyword }}
7+
68
{{ var $op = $keyword == 'maxLength' ? '>' : '<'; }}
79
if ({{# def.$dataNotType:'number' }} {{# def.strLength }} {{=$op}} {{=$schemaValue}}) {
810
{{ var $errorKeyword = $keyword; }}

lib/dot/_limitProperties.jst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
{{# def.setupKeyword }}
44
{{# def.$data }}
55

6+
{{# def.numberKeyword }}
7+
68
{{ var $op = $keyword == 'maxProperties' ? '>' : '<'; }}
79
if ({{# def.$dataNotType:'number' }} Object.keys({{=$data}}).length {{=$op}} {{=$schemaValue}}) {
810
{{ var $errorKeyword = $keyword; }}

lib/dot/allOf.jst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,3 @@
3030
{{= $closingBraces.slice(0,-1) }}
3131
{{?}}
3232
{{?}}
33-
34-
{{# def.cleanUp }}

lib/dot/anyOf.jst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
} else {
4040
{{# def.resetErrors }}
4141
{{? it.opts.allErrors }} } {{?}}
42-
43-
{{# def.cleanUp }}
4442
{{??}}
4543
{{? $breakOnError }}
4644
if (true) {

lib/dot/contains.jst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,3 @@ var {{=$valid}};
5353
{{# def.resetErrors }}
5454
{{?}}
5555
{{? it.opts.allErrors }} } {{?}}
56-
57-
{{# def.cleanUp }}

lib/dot/definitions.def

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@
112112
#}}
113113

114114

115-
{{## def.cleanUp: {{ out = it.util.cleanUpCode(out); }} #}}
116-
117-
118-
{{## def.finalCleanUp: {{ out = it.util.finalCleanUpCode(out, $async); }} #}}
119-
120-
121115
{{## def.$data:
122116
{{
123117
var $isData = it.opts.$data && $schema && $schema.$data
@@ -144,6 +138,13 @@
144138
#}}
145139

146140

141+
{{## def.numberKeyword:
142+
{{? !($isData || typeof $schema == 'number') }}
143+
{{ throw new Error($keyword + ' must be number'); }}
144+
{{?}}
145+
#}}
146+
147+
147148
{{## def.beginDefOut:
148149
{{
149150
var $$outStack = $$outStack || [];

lib/dot/dependencies.jst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,3 @@ var missing{{=$lvl}};
7676
{{= $closingBraces }}
7777
if ({{=$errs}} == errors) {
7878
{{?}}
79-
80-
{{# def.cleanUp }}

0 commit comments

Comments
 (0)