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

Option to validate a date format but not convert to a date object #509

Closed
sandfox opened this issue Dec 5, 2014 · 6 comments
Closed

Option to validate a date format but not convert to a date object #509

sandfox opened this issue Dec 5, 2014 · 6 comments
Assignees
Labels
feature New functionality or improvement
Milestone

Comments

@sandfox
Copy link

sandfox commented Dec 5, 2014

Is there a way to apply a Joi.date().format() to a string so that it will validate it but not transform the value into a date object?

The context of this is that I have a hapijs app with a controller that looks roughly like this.

var controllerPostConfig = {
    validate: {
        payload: Joi.object().keys({
            day: Joi.date().format('YYYY-MM-DD').required(),
            start_time: Joi.date().format('HH:mm:ssZ').required(),
            end_time: Joi.date().format('HH:mm:ssZ').required(),
            user_id: Joi.string().required()
        }).without('day', ['before', 'after'])
        .or(['user_id', 'before', 'after', 'day'])
    },
    handler: function(request, reply) {

        windowDao.create(this.db, request.payload, function(err, window){
            return reply(responseFormatter.resource(window));
        })
    }
}

I'd like to check that day, start_time, end_time are in valid date formats. but I'd like the output of the validation to still be strings (as long as they are valid) because thats the format my database is expecting them in, and adding another transformation layer/function somewhere else in the app seems error prone.

The only work around I can think of is to attempt to regex the incoming strings but that feels semantically wrong and also prone to errors.

Let me know if this isn't clear, or you think is too much of edge case, or just better handled outside of Joi

@Marsup Marsup added the request label Dec 5, 2014
@Marsup Marsup self-assigned this Dec 5, 2014
@Marsup Marsup added this to the 5.1.0 milestone Dec 5, 2014
@Marsup
Copy link
Collaborator

Marsup commented Dec 5, 2014

Just to be clear on this, what you want is the input as it was provided to joi, right ?

@sandfox
Copy link
Author

sandfox commented Dec 6, 2014

@Marsup yep - exactly that.

@Marsup Marsup closed this as completed in a22aa55 Dec 10, 2014
@sandfox
Copy link
Author

sandfox commented Dec 11, 2014

Thanks a bundle for this!

@satazor
Copy link

satazor commented Jan 2, 2015

@Marsup I see that the 5.1.0 milestone is complete and I would like to have this feature. Is it possible to release 5.1.0? Thank you.

@Marsup
Copy link
Collaborator

Marsup commented Jan 2, 2015

@satazor Done.

@hueniverse hueniverse added feature New functionality or improvement and removed request labels Sep 19, 2019
@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
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

4 participants