Skip to content

Commit a2bdb77

Browse files
committed
Performance enhancements
1 parent 9ac40b3 commit a2bdb77

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

lib/validator.js

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,20 @@ Validator.prototype.compile = function(schema) {
7272
};
7373

7474
Validator.prototype.compileSchemaObject = function(schemaObject) {
75-
if (schemaObject == null || typeof schemaObject !== "object" || Array.isArray(schemaObject)) {
75+
if (schemaObject === null || typeof schemaObject !== "object" || Array.isArray(schemaObject)) {
7676
throw new Error("Invalid schema!");
7777
}
7878

7979
const self = this;
80-
const checks = {};
81-
for (let name in schemaObject) {
82-
checks[name] = this.compileSchemaType(schemaObject[name]);
83-
}
80+
const checks = Object.keys(schemaObject).map(name => ({name: name, fn: this.compileSchemaType(schemaObject[name])}));
8481

8582
return function(value, _, path, parent) {
8683
const errors = [];
87-
for (let name in checks) {
88-
const propertyPath = (path ? path + "." : "") + name;
89-
const res = checks[name](value[name], schemaObject[name], propertyPath, value);
84+
const checksLength = checks.length;
85+
for (let i = 0; i < checksLength; i++) {
86+
const check = checks[i];
87+
const propertyPath = (path !== undefined ? path + "." : "") + check.name;
88+
const res = check.fn(value[check.name], undefined, propertyPath, value);
9089

9190
if (res !== true) {
9291
self.handleResult(errors, propertyPath, res);
@@ -105,18 +104,19 @@ Validator.prototype.compileSchemaType = function(schemaType) {
105104

106105
return function(value, _, path, parent) {
107106
const errors = [];
108-
let validated = false;
109-
for (let i = 0; i < checks.length; i++) {
110-
const res = checks[i](value, schemaType[i], path, parent);
107+
const checksLength = checks.length;
108+
for (let i = 0; i < checksLength; i++) {
109+
const res = checks[i](value, undefined, path, parent);
111110

112111
if (res !== true) {
113112
self.handleResult(errors, path, res);
114113
} else {
115-
validated = true;
114+
// Jump out after first success and clear previous errors
115+
return true;
116116
}
117117
}
118118

119-
return validated ? true : errors;
119+
return errors;
120120
};
121121
}
122122

@@ -130,13 +130,14 @@ Validator.prototype.compileSchemaRule = function(schemaRule) {
130130
};
131131
}
132132

133-
if (!this.rules[schemaRule.type]) {
133+
const checkRule = this.rules[schemaRule.type];
134+
if (!checkRule) {
134135
throw new Error("Invalid '" + schemaRule.type + "' type in validator schema!");
135136
}
136137

137138
const self = this;
138139
let checkContents = null;
139-
140+
140141
if (schemaRule.type === "object") {
141142
if (schemaRule.props) {
142143
checkContents = this.compileSchemaObject(schemaRule.props);
@@ -148,13 +149,26 @@ Validator.prototype.compileSchemaRule = function(schemaRule) {
148149
}
149150

150151
return function(value, _, path, parent) {
151-
const res = self.checkRule(value, schemaRule, path, parent);
152+
const errors = [];
153+
if (value === undefined || value === null) {
154+
if (schemaRule.type === "forbidden")
155+
return true;
156+
157+
if (schemaRule.optional === true)
158+
return true;
159+
160+
self.handleResult(errors, path, self.makeError("required"));
161+
return errors;
162+
}
163+
164+
const res = checkRule.call(self, value, schemaRule, path, parent);
152165
if (res !== true) {
153-
return res;
166+
self.handleResult(errors, path, res);
167+
return errors;
154168
}
155169

156-
if (checkContents) {
157-
return checkContents(value, schemaRule, path, parent);
170+
if (checkContents !== null) {
171+
return checkContents(value, undefined, path, parent);
158172
}
159173

160174
return true;
@@ -167,9 +181,10 @@ Validator.prototype.compileSchemaArray = function(schemaType) {
167181

168182
return function (value, _, path, parent) {
169183
const errors = [];
170-
for (let i = 0; i < value.length; i++) {
171-
const arrayItemPath = (path ? path : "") + "[" + i + "]";
172-
const res = checkArrayItem(value[i], schemaType, arrayItemPath, value);
184+
const valueLength = value.length;
185+
for (let i = 0; i < valueLength; i++) {
186+
const arrayItemPath = (path !== undefined ? path : "") + "[" + i + "]";
187+
const res = checkArrayItem(value[i], undefined, arrayItemPath, value);
173188

174189
if (res !== true) {
175190
self.handleResult(errors, arrayItemPath, res);
@@ -180,20 +195,6 @@ Validator.prototype.compileSchemaArray = function(schemaType) {
180195
}
181196
};
182197

183-
Validator.prototype.checkRule = function(value, schemaRule, path, parent) {
184-
if (value === undefined || value === null) {
185-
if (schemaRule.type === "forbidden")
186-
return true;
187-
188-
if (schemaRule.optional === true)
189-
return true;
190-
191-
return this.makeError("required");
192-
}
193-
194-
return this.rules[schemaRule.type].call(this, value, schemaRule, path, parent);
195-
};
196-
197198
/**
198199
* Handle results from validator functions
199200
*

0 commit comments

Comments
 (0)