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

Errors: context.key is actually the label, not the key #1167

Closed
EvgenyOrekhov opened this issue Apr 24, 2017 · 8 comments
Closed

Errors: context.key is actually the label, not the key #1167

EvgenyOrekhov opened this issue Apr 24, 2017 · 8 comments
Labels
breaking changes Change that can breaking existing code bug Bug or defect
Milestone

Comments

@EvgenyOrekhov
Copy link
Contributor

Context

  • node version: 7.9.0
  • joi version: 10.4.1
  • environment (node, browser): node
  • used with (hapi, standalone, ...): standalone

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

I need to get the key from the context, I expected context.key to be the key, but it turns out it's the label.

const Joi = require("joi");

const context = Joi
    .object({
        someKey: Joi.required().label("some label")
    })
    .validate({})
    .error.details[0].context;

console.log(context);

Which result you had ?

{
    key: "some label"
}

What did you expect ?

{
    key: "someKey"
}

This example on RunKit: https://runkit.com/57d675ee7580c314003cf648/58fe1d4fad401b00120f475a/branches/master

@WesTyler
Copy link
Contributor

Honestly not trying to be snarky here, but I mean.. that's kind of the point of .label() though isn't it?
https://github.com/hapijs/joi/blob/master/API.md#anylabelname

Overrides the key name in error messages.

@EvgenyOrekhov
Copy link
Contributor Author

@WesTyler I'm trying to convert Joi errors to something like

{
    "name": "\"Name\" is required",
    "phone": "\"Phone\" is required"
}

in order to display them in my HTML forms that have inputs with names:

<input name="name">
<input name="phone">

How do you suggest I do that if Joi errors give me only labels that are different for every language?

{
    "Имя": "<translated message>",
    "Телефон": "<translated message>"
}

@Marsup
Copy link
Collaborator

Marsup commented Apr 24, 2017

I noticed this a while ago and waited for someone to complain about it :)
This is a breaking change though, so it'll have to wait for next major.

@Marsup Marsup added breaking changes Change that can breaking existing code bug Bug or defect labels Apr 24, 2017
@WesTyler
Copy link
Contributor

@Marsup What's the change? It looks like it is working as described in the docs, so is the "fix" to change the behavior altogether?

@Marsup
Copy link
Collaborator

Marsup commented Apr 24, 2017

The key should remain the json key, just as path is not affected by the label. There should probably be one more property added to the details.

@EvgenyOrekhov
Copy link
Contributor Author

@Marsup Fixing a bug is not a breaking change, it's a fixing change.
Users that rely on buggy behavior should blame themselves and fix their code.

@WesTyler
Copy link
Contributor

WesTyler commented Apr 24, 2017

That makes sense 👍

Also, just realized I was conflating "override key name in error messages" with "override key name in context details"... Sorry mates haha.

@Marsup
Copy link
Collaborator

Marsup commented Apr 24, 2017

Good luck patching joi without making it a breaking change, unless you choose the dirty path of making key = alias || key and some other thing represent the original key.

@Marsup Marsup added this to the 11.0.0 milestone Aug 9, 2017
@Marsup Marsup closed this as completed in 8565afc Sep 14, 2017
@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 bug Bug or defect
Projects
None yet
Development

No branches or pull requests

3 participants