Skip to content

Commit 09f0c6d

Browse files
committed
Fix isByteLength
1 parent 7384f32 commit 09f0c6d

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

test/validators.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ describe('Validators', function () {
5757
, 'invalid.com'
5858
, '@invalid.com'
5959
, 'foo@bar.com.'
60+
, 'somename@gmail.com'
6061
, 'foo@bar.co.uk.'
6162
, 'z@co.c'
6263
]
@@ -742,8 +743,8 @@ describe('Validators', function () {
742743
});
743744

744745
it('should validate strings by byte length', function () {
745-
test({ validator: 'isByteLength', args: [2], valid: ['abc', 'de', 'abcd'], invalid: [ '', 'a' ] });
746-
test({ validator: 'isByteLength', args: [2, 3], valid: ['abc', 'de'], invalid: [ '', 'a', 'abcd' ] });
746+
test({ validator: 'isByteLength', args: [2], valid: ['abc', 'de', 'abcd', 'gmail'], invalid: [ '', 'a' ] });
747+
test({ validator: 'isByteLength', args: [2, 3], valid: ['abc', 'de', 'g'], invalid: [ '', 'a', 'abcd', 'gm' ] });
747748
});
748749

749750
it('should validate UUIDs', function () {

validator.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,8 @@
435435
};
436436

437437
validator.isByteLength = function (str, min, max) {
438-
return str.length >= min && (typeof max === 'undefined' || str.length <= max);
438+
var len = encodeURI(str).split(/%..|./).length - 1;
439+
return len >= min && (typeof max === 'undefined' || len <= max);
439440
};
440441

441442
validator.isUUID = function (str, version) {

0 commit comments

Comments
 (0)