From 2a961de4e8d654b0c2dd4f9468c3e9ba2ad2b2b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Aur=C3=A8le=20DARCHE?= Date: Thu, 15 Jan 2015 11:10:09 +0100 Subject: [PATCH] Fix integers enclosed in quotes 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. --- README.md | 2 +- lib/convict.js | 2 +- test/cases/file_basic.json | 2 +- test/cases/file_basic.out | 2 +- test/format-tests.js | 12 ++++++++++-- 5 files changed, 14 insertions(+), 6 deletions(-) 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); }); });