Skip to content
This repository was archived by the owner on Sep 19, 2025. It is now read-only.

Commit 98020da

Browse files
committed
Refactor variable name.
Rename validator -> scope in validate(), also tidy handling of `errors` variable.
1 parent 34cc8d3 commit 98020da

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

lib/schema.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function validate(source, opts, cb) {
6969

7070
var value
7171
, rule
72-
, validator
72+
, scope
7373
, series = []
7474
, state = opts.state || {}
7575
// iterator function series/parallel
@@ -108,7 +108,7 @@ function validate(source, opts, cb) {
108108
if(typeof rule === 'function') {
109109
rule.test = rule;
110110
}else if(typeof rule.test !== 'function') {
111-
// validator plugin functions are static methods
111+
// scope plugin functions are static methods
112112
rule.test = Rule[rule.type];
113113
}
114114

@@ -127,7 +127,7 @@ function validate(source, opts, cb) {
127127

128128
var vars = {}
129129
, k
130-
, validator
130+
, scope
131131
, isDeep;
132132

133133
// assign rule fields first
@@ -145,7 +145,7 @@ function validate(source, opts, cb) {
145145
}
146146
}
147147

148-
// final build in properties
148+
// final built in properties
149149
vars.rule = rule;
150150
vars.field = rule.field;
151151
vars.value = rule.value;
@@ -154,7 +154,7 @@ function validate(source, opts, cb) {
154154
vars.state = state;
155155
vars.messages = messages;
156156

157-
validator = Rule(vars);
157+
scope = Rule(vars);
158158

159159
isDeep = (rule.type === 'object' || rule.type === 'array')
160160
&& typeof(rule.fields) === 'object';
@@ -166,12 +166,11 @@ function validate(source, opts, cb) {
166166
return callback(err);
167167
}
168168

169-
var i
170-
, errors = validator.errors;
169+
//var errors = scope.errors;
171170

172171
// not deep so continue on to next in series
173172
if(!isDeep) {
174-
return callback(err, errors);
173+
return callback(err, scope.errors);
175174

176175
// generate temp schema for nested rules
177176
}else{
@@ -185,6 +184,7 @@ function validate(source, opts, cb) {
185184
, descriptor = rule.fields[key]
186185
, value = rule.value[key]
187186
, schema
187+
, i
188188
, tmp;
189189

190190
// state object is by pointer
@@ -222,7 +222,11 @@ function validate(source, opts, cb) {
222222
// does not exist fail at the rule level and don't
223223
// go deeper
224224
if(descriptor.required && value === undefined) {
225-
tmp = Rule({field: key, rule: descriptor, errors: errors});
225+
tmp = Rule({
226+
field: key,
227+
rule: descriptor,
228+
errors: scope.errors
229+
});
226230
tmp.raise(tmp.reasons.required, messages.required, key);
227231
return cb();
228232
}
@@ -240,22 +244,22 @@ function validate(source, opts, cb) {
240244

241245
schema.validate(value, options, function(err, res) {
242246
if(res && res.errors.length) {
243-
errors = errors.concat(res.errors);
247+
scope.errors = scope.errors.concat(res.errors);
244248
}
245249
cb(err, null);
246250
});
247251
}, function(err, result) {
248252
// bail on first error
249-
if(opts.first && errors && errors.length) {
250-
return complete(err, errors, opts, cb);
253+
if(opts.first && scope.errors && scope.errors.length) {
254+
return complete(err, scope.errors, opts, cb);
251255
}
252-
callback(err, errors);
256+
callback(err, scope.errors);
253257
})
254258
}
255259
}
256260

257261
// invoke rule validation function
258-
rule.test.call(validator, onValidate);
262+
rule.test.call(scope, onValidate);
259263

260264
}, function(err, results) {
261265
complete(err, results, opts, cb);

0 commit comments

Comments
 (0)