Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Allows case insensitive searching in within option of inclusion and exclusion #331

Open
wants to merge 1 commit 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
16 changes: 16 additions & 0 deletions specs/validate-helpers-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,22 @@ describe("validate", function() {
it("works with empty objects", function() {
expect(contains({}, "foo")).toBe(false);
});

describe("case insensitive", function() {
it("returns true if the value is a key in the object", function() {
expect(contains({foo: false, bar: "bar"}, "foo", true)).toBe(true);
expect(contains({Foo: false, bar: "bar"}, "foo", true)).toBe(true);
expect(contains({foo: false, bar: "bar"}, "BaR", true)).toBe(true);
});

it("returns false if the value is not a key in the object", function() {
expect(contains({foo: false, bar: "bar"}, "quux", true)).toBe(false);
expect(contains({foo: false, bar: "bar"}, null, true)).toBe(false);
expect(contains({foo: false, bar: "bar"}, 1, true)).toBe(false);
expect(contains({foo: false, bar: "bar"}, true, true)).toBe(false);
});
});

});
});

Expand Down
13 changes: 13 additions & 0 deletions specs/validators/exclusion-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ describe("validators.exclusion", function() {
expect(exclusion("baz", ["foo", "bar"])).not.toBeDefined();
});

describe("caseSensitive false", function() {
it("finds keys if the within value is an object", function() {
expect(exclusion("Foo", {within: {foO: true}, caseSensitive: false })).toBeDefined();
expect(exclusion("foo", {within: {foo: true, caseSensitive: false}})).toBeDefined();
});

it("finds options if within value is an array", function() {
expect(exclusion("Foo", {within: ["foo", "bar"], caseSensitive: false })).toBeDefined();
expect(exclusion("bar", {within: ["foo", "BaR"], caseSensitive: false })).toBeDefined();
expect(exclusion("bar", {within: ["foo", "bar"], caseSensitive: false })).toBeDefined();
});
});

it("supports default options", function() {
validate.validators.exclusion.options = {
message: "barfoo",
Expand Down
15 changes: 15 additions & 0 deletions specs/validators/inclusion-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ describe("validators.inclusion", function() {
expect(inclusion("baz", ["foo", "bar"])).toBeDefined();
});

describe("caseSensitive false", function() {
it("finds keys if the within value is an object", function() {
expect(inclusion("Foo", {within: {foo: true}, caseSensitive: false })).not.toBeDefined();
expect(inclusion("foo", {within: {fOO: true}, caseSensitive: false })).not.toBeDefined();
expect(inclusion("bar", {within: {fOO: true}, caseSensitive: false })).toBeDefined();
});

it("finds options if the within value is an array", function() {
expect(inclusion("Foo", {within: ["foo", "bar"], caseSensitive: false })).not.toBeDefined();
expect(inclusion("bar", {within: ["foo", "BaR"], caseSensitive: false })).not.toBeDefined();
expect(inclusion("bar", {within: ["foo", "bar"], caseSensitive: false })).not.toBeDefined();
expect(inclusion("baz", {within: ["foo", "bar"], caseSensitive: false })).toBeDefined();
});
});

it("supports default options", function() {
validate.validators.inclusion.options = {
message: "barfoo",
Expand Down
29 changes: 24 additions & 5 deletions validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,14 +430,31 @@
return v.isObject(value) && !v.isArray(value) && !v.isFunction(value);
},

contains: function(obj, value) {
contains: function(obj, value, caseInsensitive) {
var caseSensitive = !caseInsensitive;
var toLowerCase = function (k) {
return k && k.toLowerCase ? k.toLowerCase() : k;
};

if (!v.isDefined(obj)) {
return false;
}
if (v.isArray(obj)) {
return obj.indexOf(value) !== -1;
if (caseSensitive) {
return obj.indexOf(value) !== -1;
}
return obj.map(toLowerCase).indexOf(toLowerCase(value)) !== -1;
}
return value in obj;

var valueIsInObj = value in obj;

if (caseSensitive || valueIsInObj) {
return valueIsInObj;
}

return Object.keys(obj)
.map(toLowerCase)
.indexOf(toLowerCase(value)) !== -1;
},

unique: function(array) {
Expand Down Expand Up @@ -1021,7 +1038,8 @@
options = {within: options};
}
options = v.extend({}, this.options, options);
if (v.contains(options.within, value)) {
var caseInsensitive = options.caseSensitive === false;
if (v.contains(options.within, value, caseInsensitive)) {
return;
}
var message = options.message ||
Expand All @@ -1038,7 +1056,8 @@
options = {within: options};
}
options = v.extend({}, this.options, options);
if (!v.contains(options.within, value)) {
var caseInsensitive = options.caseSensitive === false;
if (!v.contains(options.within, value, caseInsensitive)) {
return;
}
var message = options.message || this.message || "^%{value} is restricted";
Expand Down