From 9b2b9c1f1c29ff23fac25def783fca35c9e5f03a Mon Sep 17 00:00:00 2001 From: roll Date: Mon, 30 Oct 2017 18:05:42 +0300 Subject: [PATCH] Fixed validation/infer bugs for yearmonth and duration types (#106) * Added date to infer test * Fixed duration, yearmonth validation * Fixed linting --- data/data_infer_formats.csv | 10 +++++----- src/types/duration.js | 3 +++ src/types/yearmonth.js | 9 ++++++--- test/infer.js | 3 +++ 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/data/data_infer_formats.csv b/data/data_infer_formats.csv index 1be4e50..11be321 100644 --- a/data/data_infer_formats.csv +++ b/data/data_infer_formats.csv @@ -1,5 +1,5 @@ -id,location,website,yearmonth -1,"[90,60]",http://www.test.com,2000-10 -2,"[90,50]",http://www.test.de,2010-07 -3,"[90,40]",http://www.test.uk,2009-05 -4,"[90,30]",http://www.test.co.il,2015-04 +id,location,website,yearmonth,date,time,datetime +1,"[90,60]",http://www.test.com,2000-10,2000-10-15,17:55:12,2000-10-15T17:55:12Z +2,"[90,50]",http://www.test.de,2010-07,2001-10-15,17:56:12,2001-10-15T17:56:12Z +3,"[90,40]",http://www.test.uk,2009-05,2002-10-15,12:55:12,2002-10-15T12:55:12Z +4,"[90,30]",http://www.test.co.il,2015-04,2003-10-15,08:55:03,2003-10-15T08:55:03Z diff --git a/src/types/duration.js b/src/types/duration.js index 780b7b4..f85c909 100644 --- a/src/types/duration.js +++ b/src/types/duration.js @@ -11,6 +11,9 @@ function castDuration(format, value) { return ERROR } try { + if (!value.startsWith('P')) { + return ERROR + } value = moment.duration(value) if (!value.as('milliseconds')) { return ERROR diff --git a/src/types/yearmonth.js b/src/types/yearmonth.js index 2e33f3c..f48edba 100644 --- a/src/types/yearmonth.js +++ b/src/types/yearmonth.js @@ -12,9 +12,12 @@ function castYearmonth(format, value) { } } else if (isString(value)) { try { - let [year, month] = value.split('-') - year = parseInt(year, 10) - month = parseInt(month, 10) + const items = value.split('-') + if (items.length !== 2) { + return ERROR + } + const year = parseInt(items[0], 10) + const month = parseInt(items[1], 10) if (!year || !month) { return ERROR } diff --git a/test/infer.js b/test/infer.js index a42ca01..8823d8c 100644 --- a/test/infer.js +++ b/test/infer.js @@ -47,6 +47,9 @@ describe('infer', () => { {name: 'location', type: 'geopoint', format: 'array'}, {name: 'website', type: 'string', format: 'uri'}, {name: 'yearmonth', type: 'yearmonth', format: 'default'}, + {name: 'date', type: 'date', format: 'default'}, + {name: 'time', type: 'time', format: 'default'}, + {name: 'datetime', type: 'datetime', format: 'default'}, ]) })