Skip to content

Commit

Permalink
Added isUUID() and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Feb 18, 2011
1 parent 9ef4f9d commit 560bd69
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
15 changes: 15 additions & 0 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,18 @@ Validator.prototype.len = function(min, max) {
}
return this;
}

//Thanks to github.com/sreuter for the idea.
Validator.prototype.isUUID = function(version) {
if (version == 3 || version == 'v3') {
pattern = /[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i;
} else if (version == 4 || version == 'v4') {
pattern = /[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
} else {
pattern = /[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i;
}
if (!this.str.match(pattern)) {
this.error(this.msg || 'Not a UUID');
}
return this;
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{ "name" : "validator",
"description" : "Data validation, filtering and sanitization for node.js",
"version" : "0.1.3",
"version" : "0.1.4",
"homepage" : "http://github.com/chriso/node-validator",
"keywords" : ["validator", "validation", "assert", "params", "sanitization", "xss", "entities", "sanitize", "sanitisation", "input"],
"author" : "Chris O'Hara <cohara87@gmail.com>",
Expand Down
37 changes: 37 additions & 0 deletions test/validator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,43 @@ module.exports = {
Validator.check('abcd').len(1, 3);
assert.ok(false, 'len failed');
} catch (e) {}
},

'test #isUUID()': function () {
////xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

assert.ok(Validator.check('A987FBC9-4BED-3078-CF07-9141BA07C9F3').isUUID());
assert.ok(Validator.check('A987FBC9-4BED-3078-CF07-9141BA07C9F3').isUUID(1));
assert.ok(Validator.check('A987FBC9-4BED-3078-CF07-9141BA07C9F3').isUUID(2));
assert.ok(Validator.check('A987FBC9-4BED-3078-CF07-9141BA07C9F3').isUUID(3));
assert.ok(Validator.check('A987FBC9-4BED-4078-8F07-9141BA07C9F3').isUUID(4));

try {
Validator.check('A987FBC9-4BED-3078-CF07-9141BA07C9F3').isUUID(4);
assert.ok(false, 'isUUID failed');
} catch (e) {}

try {
Validator.check('A987FBC9-4BED-4078-0F07-9141BA07C9F3').isUUID(4);
assert.ok(false, 'isUUID failed');
} catch (e) {}

try {
Validator.check('abc').isUUID();
assert.ok(false, 'isUUID failed');
} catch (e) {}

try {
Validator.check('A987FBC932-4BED-3078-CF07-9141BA07C9').isUUID();
assert.ok(false, 'isUUID failed');
} catch (e) {}

try {
Validator.check('A987FBG9-4BED-3078-CF07-9141BA07C9DE').isUUID();
assert.ok(false, 'isUUID failed');
} catch (e) {}

}

}

4 changes: 2 additions & 2 deletions validator-min.js

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,21 @@
return this;
}

//Thanks to github.com/sreuter for the idea.
Validator.prototype.isUUID = function(version) {
if (version == 3 || version == 'v3') {
pattern = /[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i;
} else if (version == 4 || version == 'v4') {
pattern = /[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
} else {
pattern = /[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i;
}
if (!this.str.match(pattern)) {
this.error(this.msg || 'Not a UUID');
}
return this;
}

var Filter = exports.Filter = function() {}

var whitespace = '\\r\\n\\t\\s';
Expand Down Expand Up @@ -776,4 +791,4 @@
return validator.check(str, fail_msg);
}

})(this);
})(this);

0 comments on commit 560bd69

Please sign in to comment.