Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
sydcanem opened this issue Jan 8, 2016 · 5 comments
Assignees
Labels
non issue Issue is not a problem or requires changes

Comments

@sydcanem
Copy link

sydcanem commented Jan 8, 2016

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.

@Marsup
Copy link
Collaborator

Marsup commented Jan 8, 2016

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().

@sydcanem
Copy link
Author

sydcanem commented Jan 8, 2016

I guess that works. Thanks.

@sydcanem sydcanem closed this as completed Jan 8, 2016
@Marsup Marsup added the non issue Issue is not a problem or requires changes label Jan 8, 2016
@Marsup Marsup self-assigned this Jan 8, 2016
@hulbert
Copy link

hulbert commented Apr 7, 2016

@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)

@Marsup
Copy link
Collaborator

Marsup commented Apr 8, 2016

Looking for raw() maybe ?

@hulbert
Copy link

hulbert commented Apr 8, 2016

Looking for raw() maybe ?

@Marsup exactly, thanks!

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
non issue Issue is not a problem or requires changes
Projects
None yet
Development

No branches or pull requests

3 participants