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

Rename multiple keys to another name #1239

Closed
samueljoli opened this issue Jul 5, 2017 · 10 comments
Closed

Rename multiple keys to another name #1239

samueljoli opened this issue Jul 5, 2017 · 10 comments
Assignees
Labels
feature New functionality or improvement
Milestone

Comments

@samueljoli
Copy link
Contributor

Context

  • node version: 6
  • joi version: 10.3.x
  • environment (node, browser):
  • used with (hapi, standalone, ...):
  • any other relevant information:

What are you trying to achieve or the steps to reproduce ?

const caseInsensitiveOverrides = ['marketcode', 'MarketCode', 'MARKETCODE'];

const schema = Joi.object({
  marketCode  : Joi.string(),
}).rename(caseInsensitiveOverrides, 'marketCode')

I have a need for my params to be case insensitive and would like to implement that by using the rename api. It would be nice if I could pass rename a list of keys that whenever present would rename to a default key.

Example:
=> /route?marketCode=191
=> /route?marketcode=191
=> /route?MARKETCODE=191

I would expect each permutation to be validated and that in my handler the return value would be...

request.query.marketCode // => 191
@DavidTPate
Copy link
Contributor

DavidTPate commented Jul 6, 2017

@samueljoli this seems like a poor use of the Joi.rename() functionality. When dealing with objects we have the Joi.object().pattern() construct to use for cases like this.

The reason why I think it is a bad usage is that you would be going through and needing to support every form of case for your string (I think that would be what 2^10 possibilities?) which is going to make this quite slow. Instead I would use the Joi.object().pattern() to accomplish this with a simple RegExp.

const schema = Joi.object().pattern(/^marketcode$/i, Joi.string());

@DavidTPate DavidTPate added non issue Issue is not a problem or requires changes support Questions, discussions, and general support labels Jul 6, 2017
@DavidTPate DavidTPate self-assigned this Jul 6, 2017
@Marsup
Copy link
Collaborator

Marsup commented Jul 6, 2017

Thought the same thing, except that regexp should be anchored (ie. /^marketcode$/i).

@samueljoli
Copy link
Contributor Author

@Marsup @DavidTPate Thanks I wasn't sure about the best way going about it.

@DavidTPate Whether I used pattern or rename wouldn't I have to account for every case of that string?

Also the only problem with using regex is that while it will validate the param it doesn't address my need to be able to still reference the value of that param in it's default camel case form. Using regex would also mean that I would have to make a check for each case (ex request.query.marketcode || request.query.marketCode || etc...). That's the main reason why I thought rename was the perfect candidate. It's not just about validation but being able to reference it in my src in the expected form.

@Marsup Marsup added request and removed non issue Issue is not a problem or requires changes support Questions, discussions, and general support labels Jul 6, 2017
@Marsup
Copy link
Collaborator

Marsup commented Jul 6, 2017

I'm going to set this issue as a request, taking in a regexp as the source of the rename could make sense indeed.

@samueljoli
Copy link
Contributor Author

samueljoli commented Jul 6, 2017

@Marsup Sweet! I'll look into implementation.

@Marsup
Copy link
Collaborator

Marsup commented Jul 6, 2017

This looks a bit complicated for someone not familiar with joi's code, but feel free to try :)

@samueljoli
Copy link
Contributor Author

@Marsup Is there anything in place for me to get a high level walk through the code base? Otherwise I know a core contributor who I can bug to onboard me.

@WesTyler
Copy link
Contributor

WesTyler commented Jul 6, 2017

(just kidding ;) happy to walk through joi code with you)

@samueljoli
Copy link
Contributor Author

@WesTyler 😈 hehehe looking forward to bugging you 🐛

@Marsup Marsup added this to the 11.3.0 milestone Oct 8, 2017
@Marsup
Copy link
Collaborator

Marsup commented Oct 8, 2017

Merged the PR.

@Marsup Marsup closed this as completed Oct 8, 2017
@hueniverse hueniverse added feature New functionality or improvement and removed request labels Sep 19, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Mar 17, 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

5 participants