|
1 | 1 | "use strict"; |
2 | 2 |
|
3 | 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_\-]+$/; |
4 | 7 |
|
5 | 8 | module.exports = function checkString(value, schema) { |
6 | 9 | if (typeof value !== "string") { |
7 | 10 | return this.makeError("string"); |
8 | 11 | } |
9 | 12 |
|
10 | | - /* TODO: charset |
11 | | - alpha: /^[a-zA-Z]+$/ |
12 | | - alphaNum: /^[a-zA-Z0-9]+$/ |
13 | | - alphaDash: /^[a-zA-Z0-9_\-]+$/ |
14 | | - */ |
15 | | - |
16 | 13 | const valueLength = value.length; |
17 | 14 |
|
18 | 15 | if (schema.empty === false && valueLength === 0) { |
@@ -43,8 +40,20 @@ module.exports = function checkString(value, schema) { |
43 | 40 | return this.makeError("stringEnum", schema.enum); |
44 | 41 | } |
45 | 42 |
|
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) |
48 | 57 | } |
49 | 58 |
|
50 | 59 | return true; |
|
0 commit comments