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

Commit

Permalink
Partially reverted bebe88e to allow "" but reject whitespace, for all…
Browse files Browse the repository at this point in the history
… validators except equality.
  • Loading branch information
[email protected] authored and huan086 committed Nov 7, 2016
1 parent d7f6baa commit fc331ea
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 22 deletions.
4 changes: 2 additions & 2 deletions specs/validators/datetime-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ describe('validators.datetime', function() {
expect(function() { datetime(null, {}); }).not.toThrow();
});

it("allows undefined values", function() {
it("allows empty values", function() {
spyOn(validate.validators.datetime, "parse");
spyOn(validate.validators.datetime, "format");
expect(datetime(null, {})).not.toBeDefined();
expect(datetime(undefined, {})).not.toBeDefined();
expect(datetime("", {})).not.toBeDefined();
expect(validate.validators.datetime.parse).not.toHaveBeenCalled();
expect(validate.validators.datetime.format).not.toHaveBeenCalled();
});
Expand All @@ -60,7 +61,6 @@ describe('validators.datetime', function() {
it("doesn't allow invalid dates", function() {
var expected = "must be a valid date";
expect(datetime("foobar", {})).toEqual(expected);
expect(datetime("", {})).toEqual(expected);
expect(datetime(" ", {})).toEqual(expected);
});

Expand Down
2 changes: 1 addition & 1 deletion specs/validators/email-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('validators.email', function() {
it("allows empty values", function() {
expect(email(null, {})).not.toBeDefined();
expect(email(undefined, {})).not.toBeDefined();
expect(email("", {})).not.toBeDefined();
});

it("doesn't allow non strings", function() {
Expand All @@ -32,7 +33,6 @@ describe('validators.email', function() {

it("doesn't allow 'invalid' emails", function() {
var expected = "is not a valid email";
expect(email("", {})).toEqual(expected);
expect(email(" ", {})).toEqual(expected);
expect(email("foobar", {})).toEqual(expected);
expect(email("foo@bar", {})).toEqual(expected);
Expand Down
2 changes: 1 addition & 1 deletion specs/validators/exclusion-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ describe("validators.exclusion", function() {
it("allows empty values", function() {
expect(exclusion(null, {})).not.toBeDefined();
expect(exclusion(undefined, {})).not.toBeDefined();
expect(exclusion("", {})).not.toBeDefined();
});

it("returns nothing if the value is allowed", function() {
var opts = {within: within};
expect(exclusion("", {})).not.toBeDefined();
expect(exclusion(" ", {})).not.toBeDefined();
expect(exclusion("quux", opts)).not.toBeDefined();
expect(exclusion(false, opts)).not.toBeDefined();
Expand Down
4 changes: 2 additions & 2 deletions specs/validators/format-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ describe("validators.format", function() {
expect(format(null, options2)).not.toBeDefined();
expect(format(undefined, options1)).not.toBeDefined();
expect(format(undefined, options2)).not.toBeDefined();
expect(format("", options1)).not.toBeDefined();
expect(format("", options2)).not.toBeDefined();
});

it("allows values that matches the pattern", function() {
Expand All @@ -21,8 +23,6 @@ describe("validators.format", function() {
});

it("doesn't allow values that doesn't matches the pattern", function() {
expect(format("", options1)).toBeDefined("is invalid");
expect(format("", options2)).toBeDefined("is invalid");
expect(format(" ", options1)).toBeDefined("is invalid");
expect(format(" ", options2)).toBeDefined("is invalid");
expect(format("barfoo", options1)).toEqual("is invalid");
Expand Down
2 changes: 1 addition & 1 deletion specs/validators/inclusion-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe("validators.inclusion", function() {
it("allows empty values", function() {
expect(inclusion(null, {})).not.toBeDefined();
expect(inclusion(undefined, {})).not.toBeDefined();
expect(inclusion("", {})).not.toBeDefined();
});

it("returns nothing if the value is allowed", function() {
Expand All @@ -21,7 +22,6 @@ describe("validators.inclusion", function() {

it("returns an error if the value is not included", function() {
var opts = {within: within};
expect(inclusion("", {})).toBeDefined();
expect(inclusion(" ", {})).toBeDefined();
expect(inclusion("quux", opts)).toEqual("^quux is not included in the list");
expect(inclusion(false, opts)).toEqual("^false is not included in the list");
Expand Down
2 changes: 1 addition & 1 deletion specs/validators/length-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ describe('validator.length', function() {
var options = {is: 10, minimum: 20, maximum: 5};
expect(length(null, options)).not.toBeDefined();
expect(length(undefined, options)).not.toBeDefined();
expect(length("", options)).not.toBeDefined();
});

it("refuses values without a numeric length property", function() {
Expand All @@ -108,7 +109,6 @@ describe('validator.length', function() {
};
expect(length({length: 9}, options)).toHaveLength(3);
expect(length("foobar", options)).toHaveLength(3);
expect(length("", options)).toHaveLength(2);
expect(length(" ", options)).toHaveLength(2);
});

Expand Down
2 changes: 1 addition & 1 deletion specs/validators/numericality-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe("validators.numericality", function() {
it("allows empty values", function() {
expect(numericality(null, {})).not.toBeDefined();
expect(numericality(undefined, {})).not.toBeDefined();
expect(numericality("", {})).not.toBeDefined();
});

it("allows numbers", function() {
Expand All @@ -30,7 +31,6 @@ describe("validators.numericality", function() {

it("doesn't allow non numbers", function() {
var e = "is not a number";
expect(numericality("", {})).toEqual(e);
expect(numericality(" ", {})).toEqual(e);
expect(numericality("foo", {})).toEqual(e);
expect(numericality(NaN, {})).toEqual(e);
Expand Down
2 changes: 1 addition & 1 deletion specs/validators/url-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe("validators.url", function() {
it("allows empty values", function() {
expect(url(null, {})).not.toBeDefined();
expect(url(undefined, {})).not.toBeDefined();
expect(url("", {})).not.toBeDefined();
});

it("doesn't allow non strings", function() {
Expand All @@ -21,7 +22,6 @@ describe("validators.url", function() {
it("doesn't allow 'invalid' urls", function() {
var expected = "is not a valid url";

expect(url("", {})).toBeDefined();
expect(url(" ", {})).toBeDefined();
expect(url("http://", {})).toBeDefined();
expect(url("http://.", {})).toBeDefined();
Expand Down
28 changes: 18 additions & 10 deletions validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,13 @@
}
},

isEmpty: function(value) {
isEmpty: function(value, treatWhitespaceAsEmpty) {
var attr;

if (treatWhitespaceAsEmpty === undefined) {
treatWhitespaceAsEmpty = true;
}

// Null and undefined are empty
if (!v.isDefined(value)) {
return true;
Expand All @@ -326,7 +330,11 @@

// Whitespace only strings are empty
if (v.isString(value)) {
return v.EMPTY_STRING_REGEXP.test(value);
if (treatWhitespaceAsEmpty) {
return v.EMPTY_STRING_REGEXP.test(value);
}

return value === "";
}

// For arrays we use the length property
Expand Down Expand Up @@ -766,7 +774,7 @@
},
length: function(value, options, attribute) {
// Empty values are allowed
if (!v.isDefined(value)) {
if (v.isEmpty(value, false)) {
return;
}

Expand Down Expand Up @@ -814,7 +822,7 @@
},
numericality: function(value, options) {
// Empty values are fine
if (!v.isDefined(value)) {
if (v.isEmpty(value, false)) {
return;
}

Expand Down Expand Up @@ -915,7 +923,7 @@
}

// Empty values are fine
if (!v.isDefined(value)) {
if (v.isEmpty(value, false)) {
return;
}

Expand Down Expand Up @@ -985,7 +993,7 @@
, match;

// Empty values are allowed
if (!v.isDefined(value)) {
if (v.isEmpty(value, false)) {
return;
}
if (!v.isString(value)) {
Expand All @@ -1002,7 +1010,7 @@
},
inclusion: function(value, options) {
// Empty values are fine
if (!v.isDefined(value)) {
if (v.isEmpty(value, false)) {
return;
}
if (v.isArray(options)) {
Expand All @@ -1019,7 +1027,7 @@
},
exclusion: function(value, options) {
// Empty values are fine
if (!v.isDefined(value)) {
if (v.isEmpty(value, false)) {
return;
}
if (v.isArray(options)) {
Expand All @@ -1036,7 +1044,7 @@
options = v.extend({}, this.options, options);
var message = options.message || this.message || "is not a valid email";
// Empty values are fine
if (!v.isDefined(value)) {
if (v.isEmpty(value, false)) {
return;
}
if (!v.isString(value)) {
Expand Down Expand Up @@ -1078,7 +1086,7 @@
// A URL validator that is used to validate URLs with the ability to
// restrict schemes and some domains.
url: function(value, options) {
if (!v.isDefined(value)) {
if (v.isEmpty(value, false)) {
return;
}

Expand Down
Loading

0 comments on commit fc331ea

Please sign in to comment.