Skip to content

Commit 8a38e4c

Browse files
committed
Add enhanced shorthand rules
1 parent 0ab8b2c commit 8a38e4c

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

examples/next.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ let v = new Validator({
88
});
99

1010
const schema = {
11-
//id: { type: "number", positive: true, integer: true, convert: true },
12-
//name: { type: "string", min: 3, max: 255, padStart: 5 },
13-
token: { type: "forbidden", remove: true },
11+
id: "number|positive|integer|convert",
12+
name: "string|min: 3|max: 255|padStart: 5",
13+
token: "forbidden|remove",
1414
//password: { type: "string", min: 6 },
1515
//confirmPassword: { type: "equal", field: "password" },
1616
//roles: { type: "array", items: "string", min: 1, default: ["user"] },

lib/validator.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,31 @@ class Validator {
239239
*/
240240
getRuleFromSchema(schema) {
241241
if (typeof schema === "string") {
242+
const str = schema;
243+
const p = str.split("|").map(s => s.trim());
242244
schema = {
243-
type: schema
245+
type: p[0]
244246
};
247+
p.slice(1).map(s => {
248+
const idx = s.indexOf(":");
249+
if (idx !== -1) {
250+
const key = s.substr(0, idx).trim();
251+
let value = s.substr(idx + 1).trim();
252+
if (value === "true" || value === "false")
253+
value = value === "true";
254+
else if (!Number.isNaN(Number(value))) {
255+
value = Number(value);
256+
}
257+
schema[key] = value;
258+
} else {
259+
// boolean value
260+
if (s.startsWith("no-"))
261+
schema[s.slice(3)] = false;
262+
else
263+
schema[s] = true;
264+
}
265+
});
266+
245267
} else if (Array.isArray(schema)) {
246268
if (schema.length == 0)
247269
throw new Error("Invalid schema.");

test/validator.spec.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,6 @@ describe("Test getRuleFromSchema method", () => {
120120

121121
const v = new Validator();
122122

123-
it("should convert string shorthand", () => {
124-
const res = v.getRuleFromSchema("string");
125-
expect(res.schema).toEqual({ type: "string" });
126-
});
127-
128123
it("should convert array to multi rule", () => {
129124
const res = v.getRuleFromSchema([
130125
"string",
@@ -172,6 +167,25 @@ describe("Test getRuleFromSchema method", () => {
172167
v.compile(schema);
173168
}).toThrowError("Invalid 's' type in validator schema.");
174169
});
170+
171+
describe("Test string shorthard rules", () => {
172+
173+
it("should convert only type", () => {
174+
const res = v.getRuleFromSchema("string");
175+
expect(res.schema).toEqual({ type: "string" });
176+
});
177+
178+
it("should convert with properties", () => {
179+
const res = v.getRuleFromSchema("string|min:3 | max : 10| trim");
180+
expect(res.schema).toEqual({ type: "string", min: 3, max: 10, trim: true });
181+
});
182+
183+
it("should convert with disabled properties", () => {
184+
const res = v.getRuleFromSchema("string|no-empty|trim:true|alpha:false|some:1234kg");
185+
expect(res.schema).toEqual({ type: "string", empty: false, alpha: false, trim: true, some: "1234kg" });
186+
});
187+
188+
});
175189
});
176190

177191
describe("Test compile (integration test)", () => {

0 commit comments

Comments
 (0)