Skip to content

Commit

Permalink
Fixed validation/infer bugs for yearmonth and duration types (#106)
Browse files Browse the repository at this point in the history
* Added date to infer test

* Fixed duration, yearmonth validation

* Fixed linting
  • Loading branch information
roll authored Oct 30, 2017
1 parent 50efdb0 commit 9b2b9c1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
10 changes: 5 additions & 5 deletions data/data_infer_formats.csv
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions src/types/duration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/types/yearmonth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions test/infer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'},
])
})

Expand Down

0 comments on commit 9b2b9c1

Please sign in to comment.