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

Date.iso() does not interpret ISO-8601 dates correctly #687

Closed
koresar opened this issue Jul 16, 2015 · 10 comments
Closed

Date.iso() does not interpret ISO-8601 dates correctly #687

koresar opened this issue Jul 16, 2015 · 10 comments
Assignees
Labels
bug Bug or defect

Comments

@koresar
Copy link

koresar commented Jul 16, 2015

var joi = require('joi');
var schema = joi.date().iso();
joi.validate('3333333', schema, function(err, value) {
  console.error(err);
  console.log(value);
});

Run the above with nodejs. It prints: Thu Jan 01 1970 10:55:33 GMT+1000 (AEST)
Whereas the date should be 3333-11-29. See ISO standard Ordinal Dates section.

Additionally, "3333-333" should be parsed to 3333-11-29 too. But we get an error: message: '"value" must be a valid ISO 8601 date'.

A side note.
moment.js does a better job parsing "3333-333" correctly. But it fails to parse "3333333".

@koresar
Copy link
Author

koresar commented Jul 16, 2015

This is not a huge problem until someone tries to validate a following string: "2015".
It is not ISO format, but being accepted by Joi, converts to Thu Jan 01 1970 00:00:02 date.

@Marsup Marsup added the bug Bug or defect label Jul 16, 2015
@Marsup Marsup self-assigned this Jul 16, 2015
@Marsup
Copy link
Collaborator

Marsup commented Jul 16, 2015

Joi converts anything that looks like an integer into one, assuming it's a unix timestamp, it's a design choice, if you don't agree with it you can always set the strict mode.
As for the 2nd case, JavaScript supports a limited version of ISO8601 and we rely on that, if you see a better approach that can take the best of both worlds (considering we already have moment), patch welcome.

EDIT: strict mode will actually fail every time with a string, so you either agree with the choice, or use Joi.string().isoDate() and parse it yourself.

@koresar
Copy link
Author

koresar commented Jul 16, 2015

Thanks for the fix. Is isoDate() published to npm?

@koresar
Copy link
Author

koresar commented Jul 16, 2015

Yep, it's published and working as expected. The isoDate() accepts the 3333-333 string.

@Marsup
Copy link
Collaborator

Marsup commented Jul 16, 2015

It always has been, I just remembered it was not documented.

@Marsup
Copy link
Collaborator

Marsup commented Jul 16, 2015

If you ever find a way to make it work whatever the format, feel free to submit a patch or the steps to follow.

@koresar
Copy link
Author

koresar commented Jul 16, 2015

Yep, I'm thinking of it.

@aaronjameslang
Copy link
Contributor

If this behaviour is by design, can this issue be closed?

@Marsup
Copy link
Collaborator

Marsup commented Jun 11, 2018

I don't think we'll ever come to a fix for this one.

@Marsup Marsup closed this as completed Jun 11, 2018
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug or defect
Projects
None yet
Development

No branches or pull requests

3 participants