Skip to content

Commit

Permalink
Fix isByteLength
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Aug 3, 2015
1 parent 7384f32 commit 09f0c6d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe('Validators', function () {
, 'invalid.com'
, '@invalid.com'
, '[email protected].'
, 'somename@gmail.com'
, '[email protected].'
, '[email protected]'
]
Expand Down Expand Up @@ -742,8 +743,8 @@ describe('Validators', function () {
});

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

it('should validate UUIDs', function () {
Expand Down
3 changes: 2 additions & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@
};

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

validator.isUUID = function (str, version) {
Expand Down
Loading

0 comments on commit 09f0c6d

Please sign in to comment.