Skip to content

Commit

Permalink
Merge pull request #59 from madarche/fix-duration-check
Browse files Browse the repository at this point in the history
Fix integers enclosed in quotes for #54
  • Loading branch information
zaach committed Jan 15, 2015
2 parents 133c875 + 2a961de commit f0f7487
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ convict provides serveral predefined formats for validation that you can use ([u
* `url`
* `email`
* `ipaddress` - IPv4 and IPv6 addresses
* `duration` - milliseconds or a human readable string (e.g. "3000", "5 days")
* `duration` - milliseconds or a human readable string (e.g. 3000, "5 days")
* `timestamp` - Unix timestamps or date strings recognized by [moment.js](http://momentjs.com/)
* `nat` - positive integer (natural number)

Expand Down
2 changes: 1 addition & 1 deletion lib/convict.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var types = {
assert(validator.isIP(x), 'must be an IP address');
}, "There is no IPv4 vs IPv6 addresses checking anymore, use \"ipaddress\" instead."),
duration: function(x) {
var err_msg = 'must be a positive integer or human readable string (e.g. "3000", "5 days")';
var err_msg = 'must be a positive integer or human readable string (e.g. 3000, "5 days")';
if (validator.isInt(x)) {
assert(x >= 0, err_msg);
} else {
Expand Down
2 changes: 1 addition & 1 deletion test/cases/file_basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"ip": "10.0.1.101",
"port": 8080,
"session": "4 days",
"cache": "3000"
"cache": 3000
}
2 changes: 1 addition & 1 deletion test/cases/file_basic.out
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"ip": "10.0.1.101",
"port": 8080,
"session": "4 days",
"cache": "3000"
"cache": 3000
}
12 changes: 10 additions & 2 deletions test/format-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ describe('convict formats', function() {
default: '2013-05-05'
},
duration: {
format: 'duration',
default: 604800000
},
duration2: {
format: 'duration',
default: '5 minutes'
},
Expand Down Expand Up @@ -115,8 +119,12 @@ describe('convict formats', function() {
val.must.be(moment('2013-05-05').valueOf());
});

it('must handle duration', function() {
conf.get('foo.duration').must.be(60 * 5 * 1000);
it('must handle duration in milliseconds', function() {
conf.get('foo.duration').must.be(604800000);
});

it('must handle duration in a human readable string', function() {
conf.get('foo.duration2').must.be(60 * 5 * 1000);
});
});

Expand Down

0 comments on commit f0f7487

Please sign in to comment.