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

Adding optional "converter" function for validating/converting types. #574

Closed
totherik opened this issue Feb 27, 2015 · 5 comments
Closed
Assignees
Labels
feature New functionality or improvement

Comments

@totherik
Copy link

I apologize if this has been discussed in the past (couldn't find a related issue), but just filing this to see if there's an appetite for some version of this functionality. If so, I'm happy to do the work.

The proposal is to add convert support to the type validator by adding an optional converter function to the signature: type(constructor, [name], [converter]);

The converter function would have the form:

Joi.type(CustomType, function (value) {
    return new CustomType(value);
});

A sample back-of-napkin implementation may look like:
SAMPLE REDACTED

Edit: Sample code was woefully incorrect and out of date. Working branch is here.

@totherik
Copy link
Author

NOTE: in the above example the 'converter' variable should default to a noop identity function.

Edit: Back near keyboard, so updated sample code to reflect this change.
Edit 2: Realizing I've overlooked some fundamental behavior in Joi such that this isn't anywhere near the behavior that would have to change for conversion to work correctly. Still interested in hearing if this feature has legs, though, so will dig further.

@Marsup
Copy link
Collaborator

Marsup commented Feb 27, 2015

Don't go too far into this feature, it might be done otherwise.

@Marsup Marsup added the request label Feb 27, 2015
@Marsup Marsup self-assigned this Feb 27, 2015
@totherik
Copy link
Author

Yeah, no worries. Just had some time to work on it so I put together a PR, but I won't be heartbroken if it gets closed.

@Marsup
Copy link
Collaborator

Marsup commented Feb 28, 2015

Please look at #577 and see if it covers your needs. I'll close the PR as well.

@Marsup Marsup closed this as completed Feb 28, 2015
@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

Successfully merging a pull request may close this issue.

3 participants