diff --git a/README.md b/README.md index b71d6ae6..b487cff1 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/lib/convict.js b/lib/convict.js index 82dda858..0deae491 100644 --- a/lib/convict.js +++ b/lib/convict.js @@ -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 { diff --git a/test/cases/file_basic.json b/test/cases/file_basic.json index 62c1b509..0c1ea3ef 100644 --- a/test/cases/file_basic.json +++ b/test/cases/file_basic.json @@ -2,5 +2,5 @@ "ip": "10.0.1.101", "port": 8080, "session": "4 days", - "cache": "3000" + "cache": 3000 } diff --git a/test/cases/file_basic.out b/test/cases/file_basic.out index 62c1b509..0c1ea3ef 100644 --- a/test/cases/file_basic.out +++ b/test/cases/file_basic.out @@ -2,5 +2,5 @@ "ip": "10.0.1.101", "port": 8080, "session": "4 days", - "cache": "3000" + "cache": 3000 } diff --git a/test/format-tests.js b/test/format-tests.js index 4f756604..4696aeef 100644 --- a/test/format-tests.js +++ b/test/format-tests.js @@ -36,6 +36,10 @@ describe('convict formats', function() { default: '2013-05-05' }, duration: { + format: 'duration', + default: 604800000 + }, + duration2: { format: 'duration', default: '5 minutes' }, @@ -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); }); });