Skip to content

Commit

Permalink
fix array rule return value issue
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Nov 15, 2019
1 parent da78bf5 commit a1309ad
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
--------------------------------------------------
<a name="1.0.0"></a>
# 1.0.0 (2019-11-xx)
<a name="1.0.0-beta2"></a>
# 1.0.0-beta2 (2019-11-15)

## Changes
- fix array rule return value issue.

--------------------------------------------------
<a name="1.0.0-beta1"></a>
# 1.0.0-beta1 (2019-11-15)

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

Expand Down
12 changes: 8 additions & 4 deletions examples/next.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ v.add("even", function({ schema, messages }, path, context) {
});

const schema = {
id: "number|positive|integer|convert",
name: "string|min: 3|max: 255|padStart: 5",
token: "forbidden|remove",
//id: "number|positive|integer|convert",
//name: "string|min: 3|max: 255|padStart: 5",
//token: "forbidden|remove",
//password: { type: "string", min: 6 },
//confirmPassword: { type: "equal", field: "password" },
//roles: { type: "array", items: "string", min: 1, default: ["user"] },
Expand Down Expand Up @@ -59,7 +59,8 @@ const schema = {
weightMin: "The '${field}' must be greater than {expected}! Actual: {actual}"
}
}*/
num: { type: "even" }
includes: { type: "multi", optional: true, rules: [ { type: "string" }, { type: "array", items: "string" } ] },
//num: { type: "even" }
};

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

createdAt: Date.now(),

//includes: "test1",
//includes: ["test1", "test2"],

weight: 10,
num: 2
};
Expand Down
2 changes: 2 additions & 0 deletions lib/rules/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ module.exports = function({ schema, messages }, path, context) {
*/
src.push(`
}
return value;
`);
}

Expand Down
19 changes: 19 additions & 0 deletions test/rules/array.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,23 @@ describe("Test rule: array", () => {
]);
});

describe("Test sanitization", () => {

it("should trim all items", () => {
let schema = {
roles: { type: "array", items: "string|trim" }
};
let check = v.compile(schema);

const obj = {
roles: [" admin", "user ", " moderator "]
};

expect(check(obj)).toEqual(true);
expect(obj).toEqual({
roles: ["admin", "user", "moderator"]
});
});

});
});
20 changes: 20 additions & 0 deletions test/typescript/rules/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,24 @@ describe('TypeScript Definitions', () => {
]);
});
});

describe("Test sanitization", () => {

it("should trim all items", () => {
let schema = {
roles: { type: "array", items: "string|trim" } as RuleArray
};
let check = v.compile(schema);

const obj = {
roles: [" admin", "user ", " moderator "]
};

expect(check(obj)).toEqual(true);
expect(obj).toEqual({
roles: ["admin", "user", "moderator"]
});
});

});
});

0 comments on commit a1309ad

Please sign in to comment.