Skip to content

Helpful regexp message#484

Closed
martinheidegger wants to merge 2 commits intohapijs:masterfrom
martinheidegger:helpful_regexp_message
Closed

Helpful regexp message#484
martinheidegger wants to merge 2 commits intohapijs:masterfrom
martinheidegger:helpful_regexp_message

Conversation

@martinheidegger
Copy link
Contributor

A user of mine complained that the joi output is not verbose enough. It didn't show the value that didn't match the regular expression. I added a few properties and added a new locale support. Right now I only added the value to the string object. While I added this I also added a new approach to encoding. Please treat this as an experimental pull request and a suggestion to discuss if the current way how the differentiation between language code and user messages work.

@Marsup
Copy link
Collaborator

Marsup commented Nov 17, 2014

While language might be improved, I don't see much value in customizing the key name nor doing your own stringify. Can you explain your intent on these ?

@martinheidegger
Copy link
Contributor Author

The "stringify" method is nothing but a extraction of the former one-liner, an effort for cleaning code.

Regarding the key customisation: It allowed me to prefix the defaults in a custom localisation. Also i can add some formatting using colors when I use joi in a command line context.

@Marsup Marsup added the feature New functionality or improvement label Dec 13, 2014
@Marsup Marsup self-assigned this Dec 13, 2014
@Marsup
Copy link
Collaborator

Marsup commented Dec 13, 2014

I think you shouldn't enforce the quotes without your stringify, I'd rather go with a key defined as '"{{!key}}" '.
The [] in plain english sentences don't look like a great win (eg. array or index's tests).

On a side note, I'd like people's opinion on the semver side, I don't think it necessitates a major bump, unless people are parsing language, any votes ?

@martinheidegger
Copy link
Contributor Author

The [] in plain english sentences is looking a little uncomfortable, agreed. But it is fixing an the current problem that hello, world and ["hello","world"] generate the same output. With the braces those two cases are clearly distinguishable thus making debugging easier. Same thing goes for the quotes in the key.

@Marsup
Copy link
Collaborator

Marsup commented Dec 13, 2014

You had to change every single test message to add quotes, that doesn't look like it was enforced before ;)
Don't you think quotes without the array are enough to identify it's not part of the sentence but an actual value ?

@martinheidegger
Copy link
Contributor Author

I did have the problem to distinguish once. I think thats why I changed it (its all been a while since I started that PR ;) )

@ivan-kleshnin
Copy link

Don't want to raise another language related issue but it drives me mad.

verifyPassword: Joi.any().valid(Joi.ref('password')).options({language: {any: {allowOnly: "Passwords don't match"}}}).label('')

Even this mess doesn' work as "smart" check inside Joi prohibits empty labels...
Please, please do something with this language option.
Still don't get why it can't be like this:

verifyPassword: Joi.ref('password').customMessage("Passwords don't match")

without all those wrappers and undocumented keys (what is allowOnly?).

@Marsup
Copy link
Collaborator

Marsup commented Feb 2, 2015

@ivan-kleshnin It seems to me you are hoping this message would end up somewhere on the front-end, Joi is not for that. Open another issue to describe more in depth what you want to achieve.

@ivan-kleshnin
Copy link

@Marsup I'm aware of multiple projects which already use Joi for frontend validation. So whatever the initial intent of this library was – it's not perfectly valid anymore. Is this such a big problem to fix?
I understand your desire to minimize the responsibilities of library but the absense of adequate custom messages seems very strange...

@Marsup
Copy link
Collaborator

Marsup commented Feb 7, 2015

I don't consider what you're saying a problem, so I won't "fix" it. Open another issue to explain in details.

@ivan-kleshnin
Copy link

@Marsup, as you wish #546 but all these are about the same.

@Marsup Marsup added this to the 6.0 milestone Feb 8, 2015
@Marsup Marsup closed this in efa01ae Feb 8, 2015
@Marsup
Copy link
Collaborator

Marsup commented Feb 8, 2015

Hey @martinheidegger, sorry for the time it took me.
I've rebased your commits against master and did a lot of modifications to how the language works, can you check if it's aligned with what you intended to do please ?

@martinheidegger
Copy link
Contributor Author

Looking at it now (After some time passed) I think I had two intentions: a) Make sure that I don't get confused by the error messages b) Make it possible to change the templates more flexible.

The changes are a slight step forward in terms of a) but I am not sure if b) could be done better.

I am thinking now that it might be better to enter the values & keys as "objects" with a good .toString method. This way they can be "stringified" differently if the output ought to be html or should have color formatting.

@Marsup
Copy link
Collaborator

Marsup commented Feb 8, 2015

b) is why I've left as much as I could in the template, I don't want to impose any character (like [ or ]) from the code.
I think toString will be the default option for anything that's not an array or a special value, did you have something else in mind ?

@martinheidegger
Copy link
Contributor Author

b) I think i mentioned it in the comment but it leaves some indistinguishable cases open by not having [] in the stringification. Also I am not sure (havn't figured out) where in the chain the stringification takes place but I assumed when the error is created?

@Marsup
Copy link
Collaborator

Marsup commented Feb 8, 2015

I've noticed your comment, still thinking about solutions to that.
The stringification takes place at the end of the validation, not when they are created.

@Marsup
Copy link
Collaborator

Marsup commented Feb 8, 2015

What do you think about a boolean switch to wrap or not arrays ?

@Marsup
Copy link
Collaborator

Marsup commented Feb 8, 2015

Or defining separately both enclosing characters ?

@Marsup
Copy link
Collaborator

Marsup commented Feb 9, 2015

@martinheidegger does 69904a3 fix your concerns ?

@martinheidegger
Copy link
Contributor Author

I think it does. Thank you for going the extra mile.

@Marsup Marsup added the breaking changes Change that can breaking existing code label Feb 22, 2015
@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

breaking changes Change that can breaking existing code feature New functionality or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants