From 854e72c57f580dcb768af41026683ded905c2cce Mon Sep 17 00:00:00 2001 From: Daniel Naranjo Date: Wed, 21 Apr 2021 12:59:30 -0500 Subject: [PATCH 1/3] add rule for alphabetic and space characters --- README.md | 4 ++++ src/rules.js | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/README.md b/README.md index 898c063..ce5699d 100755 --- a/README.md +++ b/README.md @@ -196,6 +196,10 @@ The field under validation may have alpha-numeric characters, as well as dashes The field under validation must be entirely alpha-numeric characters. +#### alpha_space + +The field under validation may have alphabetic characters, as well as space. + #### array The field under validation must be an array. diff --git a/src/rules.js b/src/rules.js index a865d50..1973c1f 100755 --- a/src/rules.js +++ b/src/rules.js @@ -272,6 +272,10 @@ var rules = { return /^[a-zA-Z0-9]+$/.test(val); }, + alpha_space: function (val) { + return /^[a-zA-Z ]+$/.test(val); + }, + same: function (val, req) { var val1 = this.validator._flattenObject(this.validator.input)[req]; var val2 = val; From 0c76a9c0f050456e1f19a62e846f4e9ff91f2c27 Mon Sep 17 00:00:00 2001 From: Daniel Naranjo Date: Wed, 21 Apr 2021 13:00:01 -0500 Subject: [PATCH 2/3] set spec for test --- spec/alpha_space-rule.js | 32 ++++++++++++++++++++++++++++++++ spec/error-messages.js | 8 ++++++++ 2 files changed, 40 insertions(+) create mode 100644 spec/alpha_space-rule.js diff --git a/spec/alpha_space-rule.js b/spec/alpha_space-rule.js new file mode 100644 index 0000000..3f0b06f --- /dev/null +++ b/spec/alpha_space-rule.js @@ -0,0 +1,32 @@ +const { Validator, expect } = require("./setup.js"); + +describe("alpha_space validation rule", function() { + it("should fail with non alphabetic-space characters", function() { + const validator = new Validator({ name: "Daniel_." }, { name: "alpha_space" }); + expect(validator.passes()).to.be.false; + expect(validator.fails()).to.be.true; + }); + + it("should fail with non-alphabetic characters", function() { + const validator = new Validator({ name: 12 }, { name: "alpha_space" }); + expect(validator.fails()).to.be.false; + expect(validator.passes()).to.be.true; + }); + + it("should pass with only alphabetic-space characters", function() { + const validator = new Validator({ name: "Daniel Naranjo" }, { name: "alpha_space" }); + expect(validator.passes()).to.be.true; + expect(validator.fails()).to.be.false; + }); + + it("should pass when the field is blank / optional", function() { + const validator = new Validator({ name: "" }, { name: "alpha_space" }); + expect(validator.passes()).to.be.true; + }); + + it("should pass when the field does not exist", function() { + const validator = new Validator({}, { name: "alpha_space" }); + expect(validator.passes()).to.be.true; + expect(validator.fails()).to.be.false; + }); +}); diff --git a/spec/error-messages.js b/spec/error-messages.js index ca3389e..17c4f34 100755 --- a/spec/error-messages.js +++ b/spec/error-messages.js @@ -77,6 +77,14 @@ describe("Error messages", function() { ); }); + it("should fail with non alpha space characters", function() { + const validator = new Validator({ name: "Daniel ." }, { name: "alpha_space" }); + expect(validator.passes()).to.be.false; + expect(validator.errors.first("name")).to.equal( + "The name field may only contain alphabetic characters, as well as space." + ); + }); + it("should fail without a matching confirmation field for the field under validation", function() { const validator = new Validator({ password: "abc" }, { password: "confirmed" }); expect(validator.passes()).to.be.false; From c6cf22aab1abc89dc70709f94b335d1a3b5f7d46 Mon Sep 17 00:00:00 2001 From: Daniel Naranjo Date: Tue, 11 May 2021 11:37:28 -0500 Subject: [PATCH 3/3] fix tests but missing 1 error --- spec/alpha_space-rule.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/alpha_space-rule.js b/spec/alpha_space-rule.js index 3f0b06f..85a29c5 100644 --- a/spec/alpha_space-rule.js +++ b/spec/alpha_space-rule.js @@ -3,23 +3,23 @@ const { Validator, expect } = require("./setup.js"); describe("alpha_space validation rule", function() { it("should fail with non alphabetic-space characters", function() { const validator = new Validator({ name: "Daniel_." }, { name: "alpha_space" }); - expect(validator.passes()).to.be.false; expect(validator.fails()).to.be.true; + expect(validator.passes()).to.be.false; }); it("should fail with non-alphabetic characters", function() { const validator = new Validator({ name: 12 }, { name: "alpha_space" }); - expect(validator.fails()).to.be.false; - expect(validator.passes()).to.be.true; + expect(validator.fails()).to.be.true; + expect(validator.passes()).to.be.false; }); it("should pass with only alphabetic-space characters", function() { const validator = new Validator({ name: "Daniel Naranjo" }, { name: "alpha_space" }); - expect(validator.passes()).to.be.true; expect(validator.fails()).to.be.false; + expect(validator.passes()).to.be.true; }); - it("should pass when the field is blank / optional", function() { + it("should pass when the field is an empty string", function() { const validator = new Validator({ name: "" }, { name: "alpha_space" }); expect(validator.passes()).to.be.true; });