Skip to content

Commit af63854

Browse files
authored
Merge pull request #45 from andersonjoseph/master
Add charset checker to string rules
2 parents f20da30 + c527462 commit af63854

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

lib/messages.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ module.exports = {
1111
stringPattern: "The '{field}' field fails to match the required pattern!",
1212
stringContains: "The '{field}' field must contain the '{expected}' text!",
1313
stringEnum: "The '{field}' field does not match any of the allowed values!",
14+
stringNumeric: "The '{field}' field must be a numeric string",
15+
stringAlpha: "The '{field}' field must be an alphabetic string",
16+
stringAlphanum: "The '{field}' field must be an alphanumeric string",
17+
stringAlphadash: "The '{field}' field must be an alphadash string",
1418

1519
number: "The '{field}' field must be a number!",
1620
numberMin: "The '{field}' field must be greater than or equal to {expected}!",

lib/rules/string.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
"use strict";
22

3+
const NUMERIC_PATTERN = /^-?[0-9]\d*(\.\d+)?$/;
4+
const ALPHA_PATTERN = /^[a-zA-Z]+$/;
5+
const ALPHANUM_PATTERN = /^[a-zA-Z0-9]+$/;
6+
const ALPHADASH_PATTERN = /^[a-zA-Z0-9_\-]+$/;
7+
38
module.exports = function checkString(value, schema) {
49
if (typeof value !== "string") {
510
return this.makeError("string");
611
}
712

8-
/* TODO: charset
9-
alpha: /^[a-zA-Z]+$/
10-
alphaNum: /^[a-zA-Z0-9]+$/
11-
alphaDash: /^[a-zA-Z0-9_\-]+$/
12-
13-
*/
14-
1513
const valueLength = value.length;
1614

1715
if (schema.empty === false && valueLength === 0) {
@@ -44,5 +42,21 @@ module.exports = function checkString(value, schema) {
4442
return this.makeError("stringEnum", schema.enum);
4543
}
4644

45+
if (schema.numeric === true && !NUMERIC_PATTERN.test(value) ) {
46+
return this.makeError("stringNumeric", "A numeric string", value);
47+
}
48+
49+
if(schema.alpha === true && !ALPHA_PATTERN.test(value)) {
50+
return this.makeError("stringAlpha", "An alphabetic string", value);
51+
}
52+
53+
if(schema.alphanum === true && !ALPHANUM_PATTERN.test(value)) {
54+
return this.makeError("stringAlphanum", "An alphanumeric string", value);
55+
}
56+
57+
if(schema.alphadash === true && !ALPHADASH_PATTERN.test(value)) {
58+
return this.makeError("stringAlphadash", "An alphadash string", value);
59+
}
60+
4761
return true;
4862
};

test/rules/string.spec.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,56 @@ describe("Test checkString", () => {
8383
expect(check("female", s)).toEqual(true);
8484
});
8585

86+
it("check numeric string", () => {
87+
const s = {type: "string", numeric: true};
88+
89+
expect(check("123.1s0", s)).toEqual({type: "stringNumeric", expected: "A numeric string", actual: "123.1s0"});
90+
expect(check("x", s)).toEqual({type: "stringNumeric", expected: "A numeric string", actual: "x"});
91+
expect(check("", s)).toEqual({type: "stringNumeric", expected: "A numeric string", actual: ""});
92+
expect(check(" ", s)).toEqual({type: "stringNumeric", expected: "A numeric string", actual: " "});
93+
94+
expect(check("123", s)).toEqual(true);
95+
expect(check("-123", s)).toEqual(true);
96+
expect(check("123.10", s)).toEqual(true);
97+
expect(check("-123.10", s)).toEqual(true);
98+
});
99+
100+
it("check alphabetic string", () => {
101+
const s = {type: "string", alpha: true};
102+
103+
expect(check("3312", s)).toEqual({type: "stringAlpha", expected: "An alphabetic string", actual: "3312"});
104+
expect(check("h3ll0", s)).toEqual({type: "stringAlpha", expected: "An alphabetic string", actual: "h3ll0"});
105+
expect(check("us3rnam3", s)).toEqual({type: "stringAlpha", expected: "An alphabetic string", actual: "us3rnam3"});
106+
107+
expect(check("username", s)).toEqual(true);
108+
expect(check("hello", s)).toEqual(true);
109+
expect(check("elliot", s)).toEqual(true);
110+
111+
});
112+
113+
it("check alphanumeric string", () => {
114+
const s = {type: "string", alphanum: true};
115+
116+
expect(check("hello_world", s)).toEqual({type: "stringAlphanum", expected: "An alphanumeric string", actual: "hello_world"});
117+
expect(check("print()", s)).toEqual({type: "stringAlphanum", expected: "An alphanumeric string", actual: "print()"});
118+
expect(check("user.name", s)).toEqual({type: "stringAlphanum", expected: "An alphanumeric string", actual: "user.name"});
119+
120+
expect(check("p4ssword", s)).toEqual(true);
121+
expect(check("anarchy77", s)).toEqual(true);
122+
});
123+
124+
it("check alphadash string", () => {
125+
const s = {type: "string", alphadash: true};
126+
127+
expect(check("hello world", s)).toEqual({type: "stringAlphadash", expected: "An alphadash string", actual: "hello world"});
128+
expect(check("hello.world", s)).toEqual({type: "stringAlphadash", expected: "An alphadash string", actual: "hello.world"});
129+
expect(check("spaced string", s)).toEqual({type: "stringAlphadash", expected: "An alphadash string", actual: "spaced string"});
130+
131+
132+
expect(check("hello_world", s)).toEqual(true);
133+
expect(check("dashed_string", s)).toEqual(true);
134+
135+
});
136+
137+
86138
});

0 commit comments

Comments
 (0)