Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] add rule for alphabetic characters, as well as space. #426

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
32 changes: 32 additions & 0 deletions spec/alpha_space-rule.js
Original file line number Diff line number Diff line change
@@ -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.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.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.fails()).to.be.false;
expect(validator.passes()).to.be.true;
});

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;
});

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;
});
});
8 changes: 8 additions & 0 deletions spec/error-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down