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
Type declaration for messages
option of any.validate() inconsistent with the documentation and actual code
#2720
Comments
Exactly! run into this myself. |
Having the same problem |
Is there any solution to this issue? |
If anyone else has this doubt, this is the way:
|
Can confirm this is still an issue. It was really annoying. Found out the type declaration was wrong, because language keys are supported in this function: Line 118 in 81277bd
Type declaration for the messages schema can be found here: Line 29 in 81277bd
The most accurate type would indeed be |
I created a quick patch with Use this to fix any errors you might encounter: diff --git a/node_modules/joi/lib/index.d.ts b/node_modules/joi/lib/index.d.ts
index da367a9..3ed262e 100644
--- a/node_modules/joi/lib/index.d.ts
+++ b/node_modules/joi/lib/index.d.ts
@@ -26,7 +26,7 @@ declare namespace Joi {
type BasicType = boolean | number | string | any[] | object | null;
- type LanguageMessages = Record<string, string>;
+ type LanguageMessages = Record<string, string | Record<string, string>>;
type PresenceMode = 'optional' | 'required' | 'forbidden';
|
Context
What are you trying to achieve or the steps to reproduce?
I am trying to supply localized error messages as documented here: https://github.com/sideway/joi/blob/master/API.md#anyvalidatevalue-options
The documentation for
options.errors.language
states that themessages
object is a 'two-dimensional' object:, thus I try to use
schema.validate()
the following way:What was the result you got?
A typescript error on
messages
option:Looking into the library code I have found that the type for messages given here:
https://github.com/sideway/joi/blob/b4dbd59371be9d0fbbaa5f561d5aef929028ad99/lib/index.d.ts#L181-L190
is
LanguageMessages
which is defined here:https://github.com/sideway/joi/blob/b4dbd59371be9d0fbbaa5f561d5aef929028ad99/lib/index.d.ts#L29
, which is just a flat object.
What result did you expect?
I would expect, that the required type for
messages
is declared as one of the following:Record<string, string | Record<string, string>>
Record<string, Record<string, string>>
Record<string, string> | Record<string, Record<string, string>>
Option 1. seems to most accurately describe the data shape actually accepted as the
messages
option. It actually seems possible to provide a flat object with just error keys or a mixed one, as such:, while Option 2. reflects the documentation for
validate()
best.The text was updated successfully, but these errors were encountered: