Skip to content

Commit 8711259

Browse files
add numeric string rule
1 parent 70e84e1 commit 8711259

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

lib/rules/string.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"use strict";
22

3+
const NUMERIC_PATTERN = /^-?[0-9]\d*(\.\d+)?$/;
4+
35
module.exports = function checkString(value, schema) {
46
if (typeof value !== "string") {
57
return this.makeError("string");
@@ -9,7 +11,6 @@ module.exports = function checkString(value, schema) {
911
alpha: /^[a-zA-Z]+$/
1012
alphaNum: /^[a-zA-Z0-9]+$/
1113
alphaDash: /^[a-zA-Z0-9_\-]+$/
12-
1314
*/
1415

1516
const valueLength = value.length;
@@ -42,5 +43,9 @@ module.exports = function checkString(value, schema) {
4243
return this.makeError("stringEnum", schema.enum);
4344
}
4445

46+
if (schema.numeric === true && !RegExp(/^-?[1-9]\d*(\.\d+)?$/).test(value) ) {
47+
return this.makeError("stringNumeric", 'A numeric string', value);
48+
}
49+
4550
return true;
4651
};

0 commit comments

Comments
 (0)