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

Commit

Permalink
Allows case insensitive searching in within option
Browse files Browse the repository at this point in the history
  • Loading branch information
jakerichan committed May 11, 2020
1 parent 2f8e8b5 commit 120f9f4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
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

0 comments on commit 120f9f4

Please sign in to comment.