diff --git a/specs/validate-helpers-spec.js b/specs/validate-helpers-spec.js index 5c06953..cb630f1 100644 --- a/specs/validate-helpers-spec.js +++ b/specs/validate-helpers-spec.js @@ -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); + }); + }); + }); }); diff --git a/specs/validators/exclusion-spec.js b/specs/validators/exclusion-spec.js index d31d6f9..eefdf5c 100644 --- a/specs/validators/exclusion-spec.js +++ b/specs/validators/exclusion-spec.js @@ -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", diff --git a/specs/validators/inclusion-spec.js b/specs/validators/inclusion-spec.js index 950280d..5a332a7 100644 --- a/specs/validators/inclusion-spec.js +++ b/specs/validators/inclusion-spec.js @@ -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", diff --git a/validate.js b/validate.js index adf023b..0c87688 100644 --- a/validate.js +++ b/validate.js @@ -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) { @@ -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 || @@ -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";