From 66326990e476c2775d2c7a8f1b604782c579de1d Mon Sep 17 00:00:00 2001 From: andersonjoseph Date: Sun, 4 Nov 2018 20:44:13 -0400 Subject: [PATCH 1/7] add tests for numeric string rule --- test/rules/string.spec.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/rules/string.spec.js b/test/rules/string.spec.js index ed57e5a..e04e2c3 100644 --- a/test/rules/string.spec.js +++ b/test/rules/string.spec.js @@ -76,4 +76,18 @@ describe("Test checkString", () => { expect(check("female", s)).toEqual(true); }); + it("check numeric string", () => { + const s = {type: "string", numeric: true}; + + expect(check("123.1s0", s)).toEqual({type: "stringNumeric", expected: "A numeric string", actual: "123.1s0"}); + expect(check("x", s)).toEqual({type: "stringNumeric", expected: "A numeric string", actual: "x"}); + expect(check("", s)).toEqual({type: "stringNumeric", expected: "A numeric string", actual: ""}); + expect(check(" ", s)).toEqual({type: "stringNumeric", expected: "A numeric string", actual: " "}); + + expect(check("123", s)).toEqual(true); + expect(check("-123", s)).toEqual(true); + expect(check("123.10", s)).toEqual(true); + expect(check("-123.10", s)).toEqual(true); + }); + }); From 70e84e11bc94c71d6a649e873a11322de40d6128 Mon Sep 17 00:00:00 2001 From: andersonjoseph Date: Sun, 4 Nov 2018 20:45:13 -0400 Subject: [PATCH 2/7] add message for numeric string rule --- lib/messages.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/messages.js b/lib/messages.js index 203bd7c..5fbfcc0 100644 --- a/lib/messages.js +++ b/lib/messages.js @@ -11,6 +11,7 @@ module.exports = { stringPattern: "The '{field}' field fails to match the required pattern!", stringContains: "The '{field}' field must contain the '{expected}' text!", stringEnum: "The '{field}' field does not match any of the allowed values!", + stringNumeric: "The '{field}' field must be a numeric string", number: "The '{field}' field must be a number!", numberMin: "The '{field}' field must be greater than or equal to {expected}!", From 87112592face84fc186b9db83bc3ba81d1174de6 Mon Sep 17 00:00:00 2001 From: andersonjoseph Date: Sun, 4 Nov 2018 20:47:00 -0400 Subject: [PATCH 3/7] add numeric string rule --- lib/rules/string.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/rules/string.js b/lib/rules/string.js index 50d2921..7b0c2b8 100644 --- a/lib/rules/string.js +++ b/lib/rules/string.js @@ -1,5 +1,7 @@ "use strict"; +const NUMERIC_PATTERN = /^-?[0-9]\d*(\.\d+)?$/; + module.exports = function checkString(value, schema) { if (typeof value !== "string") { return this.makeError("string"); @@ -9,7 +11,6 @@ module.exports = function checkString(value, schema) { alpha: /^[a-zA-Z]+$/ alphaNum: /^[a-zA-Z0-9]+$/ alphaDash: /^[a-zA-Z0-9_\-]+$/ - */ const valueLength = value.length; @@ -42,5 +43,9 @@ module.exports = function checkString(value, schema) { return this.makeError("stringEnum", schema.enum); } + if (schema.numeric === true && !RegExp(/^-?[1-9]\d*(\.\d+)?$/).test(value) ) { + return this.makeError("stringNumeric", 'A numeric string', value); + } + return true; }; \ No newline at end of file From 90c85bfdc4ef92fadc11ce1f8b5abbbdc7e9a463 Mon Sep 17 00:00:00 2001 From: andersonjoseph Date: Fri, 9 Nov 2018 11:57:00 -0400 Subject: [PATCH 4/7] add charset rules --- lib/rules/string.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/rules/string.js b/lib/rules/string.js index 7b0c2b8..8530215 100644 --- a/lib/rules/string.js +++ b/lib/rules/string.js @@ -1,18 +1,15 @@ "use strict"; const NUMERIC_PATTERN = /^-?[0-9]\d*(\.\d+)?$/; +const ALPHA_PATTERN = /^[a-zA-Z]+$/; +const ALPHANUM_PATTERN = /^[a-zA-Z0-9]+$/; +const ALPHADASH_PATTERN = /^[a-zA-Z0-9_\-]+$/; module.exports = function checkString(value, schema) { if (typeof value !== "string") { return this.makeError("string"); } - /* TODO: charset - alpha: /^[a-zA-Z]+$/ - alphaNum: /^[a-zA-Z0-9]+$/ - alphaDash: /^[a-zA-Z0-9_\-]+$/ - */ - const valueLength = value.length; if (schema.empty === false && valueLength === 0) { @@ -43,8 +40,20 @@ module.exports = function checkString(value, schema) { return this.makeError("stringEnum", schema.enum); } - if (schema.numeric === true && !RegExp(/^-?[1-9]\d*(\.\d+)?$/).test(value) ) { - return this.makeError("stringNumeric", 'A numeric string', value); + if (schema.numeric === true && !NUMERIC_PATTERN.test(value) ) { + return this.makeError("stringNumeric", "A numeric string", value); + } + + if(schema.alpha === true && !ALPHA_PATTERN.test(value)) { + return this.makeError("stringAlpha", "An alphabetic string", value); + } + + if(schema.alphanum === true && !ALPHANUM_PATTERN.test(value)) { + return this.makeError("stringAlphanum", "An alphanumeric string", value); + } + + if(schema.alphadash === true && !ALPHADASH_PATTERN.test(value)) { + return this.makeError("stringAlphadash", 'An alphadash string', value) } return true; From 53879226b227cc0e463e04894948d7dade757f35 Mon Sep 17 00:00:00 2001 From: andersonjoseph Date: Fri, 9 Nov 2018 11:57:27 -0400 Subject: [PATCH 5/7] add charset messages --- lib/messages.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/messages.js b/lib/messages.js index 5fbfcc0..b0bdea6 100644 --- a/lib/messages.js +++ b/lib/messages.js @@ -12,6 +12,9 @@ module.exports = { stringContains: "The '{field}' field must contain the '{expected}' text!", stringEnum: "The '{field}' field does not match any of the allowed values!", stringNumeric: "The '{field}' field must be a numeric string", + stringAlpha: "The '{field}' field must be an alphabetic string", + stringAlphanum: "The '{field}' field must be an alphanumeric string", + stringAlphadash: "The '{field}' field must be an alphadash string", number: "The '{field}' field must be a number!", numberMin: "The '{field}' field must be greater than or equal to {expected}!", From dcb3b7eff60029f3a8f94fad25fc59a626f589a3 Mon Sep 17 00:00:00 2001 From: andersonjoseph Date: Fri, 9 Nov 2018 11:57:44 -0400 Subject: [PATCH 6/7] add charset tests --- test/rules/string.spec.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/rules/string.spec.js b/test/rules/string.spec.js index e04e2c3..97763d8 100644 --- a/test/rules/string.spec.js +++ b/test/rules/string.spec.js @@ -90,4 +90,42 @@ describe("Test checkString", () => { expect(check("-123.10", s)).toEqual(true); }); + it("check alphabetic string", () => { + const s = {type: "string", alpha: true}; + + expect(check("3312", s)).toEqual({type: "stringAlpha", expected: "An alphabetic string", actual: "3312"}); + expect(check("h3ll0", s)).toEqual({type: "stringAlpha", expected: "An alphabetic string", actual: "h3ll0"}); + expect(check("us3rnam3", s)).toEqual({type: "stringAlpha", expected: "An alphabetic string", actual: "us3rnam3"}); + + expect(check("username", s)).toEqual(true); + expect(check("hello", s)).toEqual(true); + expect(check("elliot", s)).toEqual(true); + + }); + + it("check alphanumeric string", () => { + const s = {type: "string", alphanum: true}; + + expect(check("hello_world", s)).toEqual({type: "stringAlphanum", expected: "An alphanumeric string", actual: "hello_world"}); + expect(check("print()", s)).toEqual({type: "stringAlphanum", expected: "An alphanumeric string", actual: "print()"}); + expect(check("user.name", s)).toEqual({type: "stringAlphanum", expected: "An alphanumeric string", actual: "user.name"}); + + expect(check("p4ssword", s)).toEqual(true); + expect(check("anarchy77", s)).toEqual(true); + }); + + it("check alphadash string", () => { + const s = {type: "string", alphadash: true}; + + expect(check("hello world", s)).toEqual({type: "stringAlphadash", expected: "An alphadash string", actual: "hello world"}); + expect(check("hello.world", s)).toEqual({type: "stringAlphadash", expected: "An alphadash string", actual: "hello.world"}); + expect(check("spaced string", s)).toEqual({type: "stringAlphadash", expected: "An alphadash string", actual: "spaced string"}); + + + expect(check("hello_world", s)).toEqual(true); + expect(check("dashed_string", s)).toEqual(true); + + }); + + }); From c527462c052951fe5347b5217bbd5bc0247f1e36 Mon Sep 17 00:00:00 2001 From: andersonjoseph Date: Fri, 9 Nov 2018 12:19:12 -0400 Subject: [PATCH 7/7] fix lint issues --- lib/rules/string.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rules/string.js b/lib/rules/string.js index e67bced..2f3b5b2 100644 --- a/lib/rules/string.js +++ b/lib/rules/string.js @@ -55,7 +55,7 @@ module.exports = function checkString(value, schema) { } if(schema.alphadash === true && !ALPHADASH_PATTERN.test(value)) { - return this.makeError("stringAlphadash", 'An alphadash string', value) + return this.makeError("stringAlphadash", "An alphadash string", value); } return true;