Skip to content

Commit 0f4968a

Browse files
committed
fux optional multi rule
1 parent 1fda5b7 commit 0f4968a

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
--------------------------------------------------
2+
<a name="1.0.0-beta3"></a>
3+
# 1.0.0-beta3 (2019-11-17)
4+
5+
## Changes
6+
- fix optional multi rule.
7+
18
--------------------------------------------------
29
<a name="1.0.0-beta2"></a>
310
# 1.0.0-beta2 (2019-11-15)

lib/validator.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,13 @@ class Validator {
272272
type: "multi",
273273
rules: schema
274274
};
275+
276+
// Check 'optional' flag
277+
const isOptional = schema.rules
278+
.map(s => this.getRuleFromSchema(s))
279+
.every(rule => rule.schema.optional == true);
280+
if (isOptional)
281+
schema.optional = true;
275282
}
276283

277284
const ruleFunction = this.rules[schema.type];

test/integration.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,26 @@ describe("Test multiple rules", () => {
340340
expect(res).toBe(true);
341341
});
342342

343+
it("should work with optional (legacy)", () => {
344+
345+
let schemaOptional = {
346+
a: [
347+
{ type: "number", optional: true },
348+
{ type: "string", optional: true },
349+
]
350+
};
351+
352+
let checkOptional = v.compile(schemaOptional);
353+
354+
expect(checkOptional({})).toBe(true);
355+
expect(checkOptional({ a: 5 })).toBe(true);
356+
expect(checkOptional({ a: "five" })).toBe(true);
357+
expect(checkOptional({ a: false })).toEqual([
358+
{ type: "number", field: "a", actual: false, message: "The 'a' field must be a number." },
359+
{ type: "string", field: "a", actual: false, message: "The 'a' field must be a string." },
360+
]);
361+
});
362+
343363
});
344364

345365
describe("Test multiple rules with objects", () => {

0 commit comments

Comments
 (0)