Skip to content

Commit 5c9ee97

Browse files
committed
fix #27
1 parent cdc5148 commit 5c9ee97

File tree

2 files changed

+53
-45
lines changed

2 files changed

+53
-45
lines changed

lib/validator.js

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -184,55 +184,61 @@ Validator.prototype._checkWrapper = function(rules, isMultipleRules) {
184184
}
185185

186186
// Check required fields
187-
if ((value === undefined || value === null) && check.type !== "forbidden") {
188-
if (schema.optional !== true && !Array.isArray(schema)) {
187+
if ((value === undefined || value === null)) {
188+
if (check.type === "forbidden")
189+
continue;
190+
191+
if (schema.optional === true)
192+
continue;
193+
194+
if (!Array.isArray(schema)) {
189195
self.handleResult(errors, stack, self.makeError("required"));
190-
} else
191196
continue;
197+
}
192198

193-
} else {
194-
// Call the checker function
195-
if (check.iterate) {
196-
let errorInCurrentArray = false;
197-
const l = value.length;
198-
for (let i = 0; i < l; i++) {
199-
let _stack = stack + "[" + i + "]";
200-
let res = check.fn.call(self, value[i], schema, _stack, obj);
201-
if (res !== true) {
202-
errorInCurrentArray = true;
203-
self.handleResult(errors, _stack, res);
204-
}
199+
} // else {
200+
// Call the checker function
201+
if (check.iterate) {
202+
let errorInCurrentArray = false;
203+
const l = value.length;
204+
for (let i = 0; i < l; i++) {
205+
let _stack = stack + "[" + i + "]";
206+
let res = check.fn.call(self, value[i], schema, _stack, obj);
207+
if (res !== true) {
208+
errorInCurrentArray = true;
209+
self.handleResult(errors, _stack, res);
205210
}
206-
/**
211+
}
212+
/**
207213
* If this is second part of a multiRule array check and the array
208214
* is valid, then the rule is valid.
209215
*/
210-
if (!errorInCurrentArray && isMultipleRules && check.secondPart) {
211-
return true;
212-
}
213-
} else {
214-
let res = check.fn.call(self, value, schema, stack, obj);
216+
if (!errorInCurrentArray && isMultipleRules && check.secondPart) {
217+
return true;
218+
}
219+
} else {
220+
let res = check.fn.call(self, value, schema, stack, obj);
215221

216-
if (isMultipleRules) {
217-
if (res === true) {
218-
/**
222+
if (isMultipleRules) {
223+
if (res === true) {
224+
/**
219225
* Object and array checks are divided into two internal checks. In case of a multiRule
220226
* we have to make sure to check both parts. Thus we we continue to also do the second
221227
* check if their is one.
222228
*/
223-
const nextRule = rules[i + 1];
224-
if (nextRule && nextRule.secondPart){
225-
continue;
226-
}
227-
// Jump out after first success and clear previous errors
228-
return true;
229+
const nextRule = rules[i + 1];
230+
if (nextRule && nextRule.secondPart){
231+
continue;
229232
}
233+
// Jump out after first success and clear previous errors
234+
return true;
230235
}
231-
232-
if (res !== true)
233-
self.handleResult(errors, stack, res);
234236
}
237+
238+
if (res !== true)
239+
self.handleResult(errors, stack, res);
235240
}
241+
//}
236242
}
237243

238244
return errors.length === 0 ? true : errors;

test/validator.spec.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -586,15 +586,6 @@ describe("Test multiple rules", () => {
586586

587587
let check = v.compile(schema);
588588

589-
let schemaOptional = {
590-
a: [
591-
{ type: "number", optional: true },
592-
{ type: "string", optional: true },
593-
]
594-
};
595-
596-
let checkOptional = v.compile(schema);
597-
598589
it("should give true if value is string", () => {
599590
let obj = { value: "John" };
600591

@@ -640,11 +631,21 @@ describe("Test multiple rules", () => {
640631
});
641632

642633
it("should work with optional", () => {
634+
635+
let schemaOptional = {
636+
a: [
637+
{ type: "number", optional: true },
638+
{ type: "string", optional: true },
639+
]
640+
};
641+
642+
let checkOptional = v.compile(schemaOptional);
643+
644+
643645
let obj = {};
644646
let res = checkOptional(obj);
645647

646648
expect(res).toBe(true);
647-
648649
});
649650

650651
});
@@ -880,13 +881,14 @@ describe("Test multiple rules with mixed types", () => {
880881
});
881882

882883
it("should give error if 'undefined'", () => {
884+
debugger; // eslint-disable-line
883885
const res = check({ value: undefined });
884886

885887
expect(res).toBeInstanceOf(Array);
886888
expect(res.length).toBe(2);
887-
expect(res[0].type).toBe("string");
889+
expect(res[0].type).toBe("required");
888890
expect(res[0].field).toBe("value");
889-
expect(res[1].type).toBe("boolean");
891+
expect(res[1].type).toBe("required");
890892
expect(res[1].field).toBe("value");
891893
});
892894

0 commit comments

Comments
 (0)