Skip to content

Commit 90c85bf

Browse files
add charset rules
1 parent 63cb55e commit 90c85bf

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

lib/rules/string.js

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

33
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_\-]+$/;
47

58
module.exports = function checkString(value, schema) {
69
if (typeof value !== "string") {
710
return this.makeError("string");
811
}
912

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

1815
if (schema.empty === false && valueLength === 0) {
@@ -43,8 +40,20 @@ module.exports = function checkString(value, schema) {
4340
return this.makeError("stringEnum", schema.enum);
4441
}
4542

46-
if (schema.numeric === true && !RegExp(/^-?[1-9]\d*(\.\d+)?$/).test(value) ) {
47-
return this.makeError("stringNumeric", 'A numeric string', value);
43+
if (schema.numeric === true && !NUMERIC_PATTERN.test(value) ) {
44+
return this.makeError("stringNumeric", "A numeric string", value);
45+
}
46+
47+
if(schema.alpha === true && !ALPHA_PATTERN.test(value)) {
48+
return this.makeError("stringAlpha", "An alphabetic string", value);
49+
}
50+
51+
if(schema.alphanum === true && !ALPHANUM_PATTERN.test(value)) {
52+
return this.makeError("stringAlphanum", "An alphanumeric string", value);
53+
}
54+
55+
if(schema.alphadash === true && !ALPHADASH_PATTERN.test(value)) {
56+
return this.makeError("stringAlphadash", 'An alphadash string', value)
4857
}
4958

5059
return true;

0 commit comments

Comments
 (0)