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

Commit 2fe8a9e

Browse files
committed
Allows case insensitive searching in within option
1 parent 2f8e8b5 commit 2fe8a9e

4 files changed

Lines changed: 66 additions & 5 deletions

File tree

specs/validate-helpers-spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,22 @@ describe("validate", function() {
328328
it("works with empty objects", function() {
329329
expect(contains({}, "foo")).toBe(false);
330330
});
331+
332+
describe("case insensitive", function() {
333+
it("returns true if the value is a key in the object", function() {
334+
expect(contains({foo: false, bar: "bar"}, "foo", true)).toBe(true);
335+
expect(contains({Foo: false, bar: "bar"}, "foo", true)).toBe(true);
336+
expect(contains({foo: false, bar: "bar"}, "BaR", true)).toBe(true);
337+
});
338+
339+
it("returns false if the value is not a key in the object", function() {
340+
expect(contains({foo: false, bar: "bar"}, "quux", true)).toBe(false);
341+
expect(contains({foo: false, bar: "bar"}, null, true)).toBe(false);
342+
expect(contains({foo: false, bar: "bar"}, 1, true)).toBe(false);
343+
expect(contains({foo: false, bar: "bar"}, true, true)).toBe(false);
344+
});
345+
});
346+
331347
});
332348
});
333349

specs/validators/exclusion-spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ describe("validators.exclusion", function() {
5656
expect(exclusion("baz", ["foo", "bar"])).not.toBeDefined();
5757
});
5858

59+
it("finds case insensitve keys if the within value is an object", function() {
60+
expect(exclusion("Foo", {within: {foO: true}, caseSensitive: false })).toBeDefined();
61+
expect(exclusion("foo", {within: {foo: true, caseSensitive: false}})).toBeDefined();
62+
});
63+
64+
it("finds case insensitve options if within value is an array", function() {
65+
expect(exclusion("Foo", {within: ["foo", "bar"], caseSensitive: false })).toBeDefined();
66+
expect(exclusion("bar", {within: ["foo", "BaR"], caseSensitive: false })).toBeDefined();
67+
expect(exclusion("bar", {within: ["foo", "bar"], caseSensitive: false })).toBeDefined();
68+
});
69+
5970
it("supports default options", function() {
6071
validate.validators.exclusion.options = {
6172
message: "barfoo",

specs/validators/inclusion-spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ describe("validators.inclusion", function() {
4747
expect(inclusion("baz", ["foo", "bar"])).toBeDefined();
4848
});
4949

50+
describe("caseInsensitive", function() {
51+
it("finds keys if the within value is an object", function() {
52+
expect(inclusion("Foo", {within: {foo: true}, caseSensitive: false })).not.toBeDefined();
53+
expect(inclusion("foo", {within: {fOO: true}, caseSensitive: false })).not.toBeDefined();
54+
expect(inclusion("bar", {within: {fOO: true}, caseSensitive: false })).toBeDefined();
55+
});
56+
57+
it("finds options if the within value is an array", function() {
58+
expect(inclusion("Foo", {within: ["foo", "bar"], caseSensitive: false })).not.toBeDefined();
59+
expect(inclusion("bar", {within: ["foo", "BaR"], caseSensitive: false })).not.toBeDefined();
60+
expect(inclusion("bar", {within: ["foo", "bar"], caseSensitive: false })).not.toBeDefined();
61+
expect(inclusion("baz", {within: ["foo", "bar"], caseSensitive: false })).toBeDefined();
62+
});
63+
});
64+
5065
it("supports default options", function() {
5166
validate.validators.inclusion.options = {
5267
message: "barfoo",

validate.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,14 +430,31 @@
430430
return v.isObject(value) && !v.isArray(value) && !v.isFunction(value);
431431
},
432432

433-
contains: function(obj, value) {
433+
contains: function(obj, value, caseInsensitive) {
434+
var caseSensitive = !caseInsensitive;
435+
var toLowerCase = function (k) {
436+
return k && k.toLowerCase ? k.toLowerCase() : k;
437+
};
438+
434439
if (!v.isDefined(obj)) {
435440
return false;
436441
}
437442
if (v.isArray(obj)) {
438-
return obj.indexOf(value) !== -1;
443+
if (caseSensitive) {
444+
return obj.indexOf(value) !== -1;
445+
}
446+
return obj.map(toLowerCase).indexOf(toLowerCase(value)) !== -1;
439447
}
440-
return value in obj;
448+
449+
var valueIsInObj = value in obj;
450+
451+
if (caseSensitive || valueIsInObj) {
452+
return valueIsInObj;
453+
}
454+
455+
return Object.keys(obj)
456+
.map(toLowerCase)
457+
.indexOf(toLowerCase(value)) !== -1;
441458
},
442459

443460
unique: function(array) {
@@ -1021,7 +1038,8 @@
10211038
options = {within: options};
10221039
}
10231040
options = v.extend({}, this.options, options);
1024-
if (v.contains(options.within, value)) {
1041+
var caseInsensitive = options.caseSensitive === false;
1042+
if (v.contains(options.within, value, caseInsensitive)) {
10251043
return;
10261044
}
10271045
var message = options.message ||
@@ -1038,7 +1056,8 @@
10381056
options = {within: options};
10391057
}
10401058
options = v.extend({}, this.options, options);
1041-
if (!v.contains(options.within, value)) {
1059+
var caseInsensitive = options.caseSensitive === false;
1060+
if (!v.contains(options.within, value, caseInsensitive)) {
10421061
return;
10431062
}
10441063
var message = options.message || this.message || "^%{value} is restricted";

0 commit comments

Comments
 (0)