Skip to content

Commit a1309ad

Browse files
committed
fix array rule return value issue
1 parent da78bf5 commit a1309ad

File tree

5 files changed

+58
-6
lines changed

5 files changed

+58
-6
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
--------------------------------------------------
2-
<a name="1.0.0"></a>
3-
# 1.0.0 (2019-11-xx)
2+
<a name="1.0.0-beta2"></a>
3+
# 1.0.0-beta2 (2019-11-15)
4+
5+
## Changes
6+
- fix array rule return value issue.
7+
8+
--------------------------------------------------
9+
<a name="1.0.0-beta1"></a>
10+
# 1.0.0-beta1 (2019-11-15)
411

512
The full library has been rewritten. It uses code generators in order to be much faster.
613

examples/next.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ v.add("even", function({ schema, messages }, path, context) {
2020
});
2121

2222
const schema = {
23-
id: "number|positive|integer|convert",
24-
name: "string|min: 3|max: 255|padStart: 5",
25-
token: "forbidden|remove",
23+
//id: "number|positive|integer|convert",
24+
//name: "string|min: 3|max: 255|padStart: 5",
25+
//token: "forbidden|remove",
2626
//password: { type: "string", min: 6 },
2727
//confirmPassword: { type: "equal", field: "password" },
2828
//roles: { type: "array", items: "string", min: 1, default: ["user"] },
@@ -59,7 +59,8 @@ const schema = {
5959
weightMin: "The '${field}' must be greater than {expected}! Actual: {actual}"
6060
}
6161
}*/
62-
num: { type: "even" }
62+
includes: { type: "multi", optional: true, rules: [ { type: "string" }, { type: "array", items: "string" } ] },
63+
//num: { type: "even" }
6364
};
6465

6566
const check = v.compile(schema);
@@ -90,6 +91,9 @@ const obj = {
9091

9192
createdAt: Date.now(),
9293

94+
//includes: "test1",
95+
//includes: ["test1", "test2"],
96+
9397
weight: 10,
9498
num: 2
9599
};

lib/rules/array.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ module.exports = function({ schema, messages }, path, context) {
8787
*/
8888
src.push(`
8989
}
90+
91+
return value;
9092
`);
9193
}
9294

test/rules/array.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,23 @@ describe("Test rule: array", () => {
9191
]);
9292
});
9393

94+
describe("Test sanitization", () => {
95+
96+
it("should trim all items", () => {
97+
let schema = {
98+
roles: { type: "array", items: "string|trim" }
99+
};
100+
let check = v.compile(schema);
101+
102+
const obj = {
103+
roles: [" admin", "user ", " moderator "]
104+
};
105+
106+
expect(check(obj)).toEqual(true);
107+
expect(obj).toEqual({
108+
roles: ["admin", "user", "moderator"]
109+
});
110+
});
111+
112+
});
94113
});

test/typescript/rules/array.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,24 @@ describe('TypeScript Definitions', () => {
9696
]);
9797
});
9898
});
99+
100+
describe("Test sanitization", () => {
101+
102+
it("should trim all items", () => {
103+
let schema = {
104+
roles: { type: "array", items: "string|trim" } as RuleArray
105+
};
106+
let check = v.compile(schema);
107+
108+
const obj = {
109+
roles: [" admin", "user ", " moderator "]
110+
};
111+
112+
expect(check(obj)).toEqual(true);
113+
expect(obj).toEqual({
114+
roles: ["admin", "user", "moderator"]
115+
});
116+
});
117+
118+
});
99119
});

0 commit comments

Comments
 (0)