Skip to content

Commit

Permalink
Fix integers enclosed in quotes
Browse files Browse the repository at this point in the history
TL;DR: No backwards-incompatible API change, no new functionality

Note that the `validate` and `coerce` functions still accept and work
well with duration integers written as strings. This is in line withi
"be liberal in what you accept" and the JavaScript actual good
practices. We want to keep that behavior I think.

So, as it is, this commit is just about documentation and messages
displayed to the user, not about code logic. Everything that worked
before will still work after.
  • Loading branch information
madarche committed Jan 15, 2015
1 parent 133c875 commit 2a961de
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 2a961de

Please sign in to comment.