Joi: Passing a valid date string with Joi.date() validation and convert is 'false' returns an error

Created on 8 Jan 2016  路  5Comments  路  Source: sideway/joi

I have a query parameter of date string and set convert option to false. Joi.validate fails and returns error "'startDate' must be a number of milliseconds or valid date string" even though the string is a valid date string.

non issue

Most helpful comment

Looking for raw() maybe ?

All 5 comments

A string is not a date, it needs to be coerced to validate, convert is almost always needed when validating from a JSON. If you need the original string you can add .raw().

I guess that works. Thanks.

@Marsup I ran into this today. My goal is to convert for Joi validation but keep the original value otherwise. Is there any way to do so? E.g.

let schema = Joi.object().keys({
    // format is basically pointless with convert=false
    dateStr: Joi.date().format('YYYY-MM-DD').options({ convert: false }), 
    someNumber: Joi.number()
})

let payload = {
    dateStr: '2016-04-07',
    someNumber: '17'
}

let result = Joi.attempt(payload, schema)

// want: result.dateStr to be a string, result.someNumber to be a number (not string)

Looking for raw() maybe ?

Looking for raw() maybe ?

@Marsup exactly, thanks!

Edit: also apparently I should read more closely as you mentioned this above 馃槪

Was this page helpful?
0 / 5 - 0 ratings