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

Commit fc331ea

Browse files
huan086@gmail.comhuan086
huan086@gmail.com
authored andcommitted
Partially reverted bebe88e to allow "" but reject whitespace, for all validators except equality.
1 parent d7f6baa commit fc331ea

11 files changed

+30
-22
lines changed

specs/validators/datetime-spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ describe('validators.datetime', function() {
4444
expect(function() { datetime(null, {}); }).not.toThrow();
4545
});
4646

47-
it("allows undefined values", function() {
47+
it("allows empty values", function() {
4848
spyOn(validate.validators.datetime, "parse");
4949
spyOn(validate.validators.datetime, "format");
5050
expect(datetime(null, {})).not.toBeDefined();
5151
expect(datetime(undefined, {})).not.toBeDefined();
52+
expect(datetime("", {})).not.toBeDefined();
5253
expect(validate.validators.datetime.parse).not.toHaveBeenCalled();
5354
expect(validate.validators.datetime.format).not.toHaveBeenCalled();
5455
});
@@ -60,7 +61,6 @@ describe('validators.datetime', function() {
6061
it("doesn't allow invalid dates", function() {
6162
var expected = "must be a valid date";
6263
expect(datetime("foobar", {})).toEqual(expected);
63-
expect(datetime("", {})).toEqual(expected);
6464
expect(datetime(" ", {})).toEqual(expected);
6565
});
6666

specs/validators/email-spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe('validators.email', function() {
1010
it("allows empty values", function() {
1111
expect(email(null, {})).not.toBeDefined();
1212
expect(email(undefined, {})).not.toBeDefined();
13+
expect(email("", {})).not.toBeDefined();
1314
});
1415

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

3334
it("doesn't allow 'invalid' emails", function() {
3435
var expected = "is not a valid email";
35-
expect(email("", {})).toEqual(expected);
3636
expect(email(" ", {})).toEqual(expected);
3737
expect(email("foobar", {})).toEqual(expected);
3838
expect(email("foo@bar", {})).toEqual(expected);

specs/validators/exclusion-spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ describe("validators.exclusion", function() {
1010
it("allows empty values", function() {
1111
expect(exclusion(null, {})).not.toBeDefined();
1212
expect(exclusion(undefined, {})).not.toBeDefined();
13+
expect(exclusion("", {})).not.toBeDefined();
1314
});
1415

1516
it("returns nothing if the value is allowed", function() {
1617
var opts = {within: within};
17-
expect(exclusion("", {})).not.toBeDefined();
1818
expect(exclusion(" ", {})).not.toBeDefined();
1919
expect(exclusion("quux", opts)).not.toBeDefined();
2020
expect(exclusion(false, opts)).not.toBeDefined();

specs/validators/format-spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ describe("validators.format", function() {
1313
expect(format(null, options2)).not.toBeDefined();
1414
expect(format(undefined, options1)).not.toBeDefined();
1515
expect(format(undefined, options2)).not.toBeDefined();
16+
expect(format("", options1)).not.toBeDefined();
17+
expect(format("", options2)).not.toBeDefined();
1618
});
1719

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

2325
it("doesn't allow values that doesn't matches the pattern", function() {
24-
expect(format("", options1)).toBeDefined("is invalid");
25-
expect(format("", options2)).toBeDefined("is invalid");
2626
expect(format(" ", options1)).toBeDefined("is invalid");
2727
expect(format(" ", options2)).toBeDefined("is invalid");
2828
expect(format("barfoo", options1)).toEqual("is invalid");

specs/validators/inclusion-spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe("validators.inclusion", function() {
1010
it("allows empty values", function() {
1111
expect(inclusion(null, {})).not.toBeDefined();
1212
expect(inclusion(undefined, {})).not.toBeDefined();
13+
expect(inclusion("", {})).not.toBeDefined();
1314
});
1415

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

2223
it("returns an error if the value is not included", function() {
2324
var opts = {within: within};
24-
expect(inclusion("", {})).toBeDefined();
2525
expect(inclusion(" ", {})).toBeDefined();
2626
expect(inclusion("quux", opts)).toEqual("^quux is not included in the list");
2727
expect(inclusion(false, opts)).toEqual("^false is not included in the list");

specs/validators/length-spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ describe('validator.length', function() {
8383
var options = {is: 10, minimum: 20, maximum: 5};
8484
expect(length(null, options)).not.toBeDefined();
8585
expect(length(undefined, options)).not.toBeDefined();
86+
expect(length("", options)).not.toBeDefined();
8687
});
8788

8889
it("refuses values without a numeric length property", function() {
@@ -108,7 +109,6 @@ describe('validator.length', function() {
108109
};
109110
expect(length({length: 9}, options)).toHaveLength(3);
110111
expect(length("foobar", options)).toHaveLength(3);
111-
expect(length("", options)).toHaveLength(2);
112112
expect(length(" ", options)).toHaveLength(2);
113113
});
114114

specs/validators/numericality-spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe("validators.numericality", function() {
2121
it("allows empty values", function() {
2222
expect(numericality(null, {})).not.toBeDefined();
2323
expect(numericality(undefined, {})).not.toBeDefined();
24+
expect(numericality("", {})).not.toBeDefined();
2425
});
2526

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

3132
it("doesn't allow non numbers", function() {
3233
var e = "is not a number";
33-
expect(numericality("", {})).toEqual(e);
3434
expect(numericality(" ", {})).toEqual(e);
3535
expect(numericality("foo", {})).toEqual(e);
3636
expect(numericality(NaN, {})).toEqual(e);

specs/validators/url-spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe("validators.url", function() {
1010
it("allows empty values", function() {
1111
expect(url(null, {})).not.toBeDefined();
1212
expect(url(undefined, {})).not.toBeDefined();
13+
expect(url("", {})).not.toBeDefined();
1314
});
1415

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

24-
expect(url("", {})).toBeDefined();
2525
expect(url(" ", {})).toBeDefined();
2626
expect(url("http://", {})).toBeDefined();
2727
expect(url("http://.", {})).toBeDefined();

validate.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,13 @@
311311
}
312312
},
313313

314-
isEmpty: function(value) {
314+
isEmpty: function(value, treatWhitespaceAsEmpty) {
315315
var attr;
316316

317+
if (treatWhitespaceAsEmpty === undefined) {
318+
treatWhitespaceAsEmpty = true;
319+
}
320+
317321
// Null and undefined are empty
318322
if (!v.isDefined(value)) {
319323
return true;
@@ -326,7 +330,11 @@
326330

327331
// Whitespace only strings are empty
328332
if (v.isString(value)) {
329-
return v.EMPTY_STRING_REGEXP.test(value);
333+
if (treatWhitespaceAsEmpty) {
334+
return v.EMPTY_STRING_REGEXP.test(value);
335+
}
336+
337+
return value === "";
330338
}
331339

332340
// For arrays we use the length property
@@ -766,7 +774,7 @@
766774
},
767775
length: function(value, options, attribute) {
768776
// Empty values are allowed
769-
if (!v.isDefined(value)) {
777+
if (v.isEmpty(value, false)) {
770778
return;
771779
}
772780

@@ -814,7 +822,7 @@
814822
},
815823
numericality: function(value, options) {
816824
// Empty values are fine
817-
if (!v.isDefined(value)) {
825+
if (v.isEmpty(value, false)) {
818826
return;
819827
}
820828

@@ -915,7 +923,7 @@
915923
}
916924

917925
// Empty values are fine
918-
if (!v.isDefined(value)) {
926+
if (v.isEmpty(value, false)) {
919927
return;
920928
}
921929

@@ -985,7 +993,7 @@
985993
, match;
986994

987995
// Empty values are allowed
988-
if (!v.isDefined(value)) {
996+
if (v.isEmpty(value, false)) {
989997
return;
990998
}
991999
if (!v.isString(value)) {
@@ -1002,7 +1010,7 @@
10021010
},
10031011
inclusion: function(value, options) {
10041012
// Empty values are fine
1005-
if (!v.isDefined(value)) {
1013+
if (v.isEmpty(value, false)) {
10061014
return;
10071015
}
10081016
if (v.isArray(options)) {
@@ -1019,7 +1027,7 @@
10191027
},
10201028
exclusion: function(value, options) {
10211029
// Empty values are fine
1022-
if (!v.isDefined(value)) {
1030+
if (v.isEmpty(value, false)) {
10231031
return;
10241032
}
10251033
if (v.isArray(options)) {
@@ -1036,7 +1044,7 @@
10361044
options = v.extend({}, this.options, options);
10371045
var message = options.message || this.message || "is not a valid email";
10381046
// Empty values are fine
1039-
if (!v.isDefined(value)) {
1047+
if (v.isEmpty(value, false)) {
10401048
return;
10411049
}
10421050
if (!v.isString(value)) {
@@ -1078,7 +1086,7 @@
10781086
// A URL validator that is used to validate URLs with the ability to
10791087
// restrict schemes and some domains.
10801088
url: function(value, options) {
1081-
if (!v.isDefined(value)) {
1089+
if (v.isEmpty(value, false)) {
10821090
return;
10831091
}
10841092

0 commit comments

Comments
 (0)