Closed CynicalBusiness closed 3 months ago
Are you using it in the frontend? Cause I can't reproduce in node, but there indeed seems to be an issue with the sandbox.
Edit: I forgot to call annotate, now I see the problem 🤦🏻♂️
I was playing around in the sandbox some more, and, it does not look like it is annotate
's fault at all. In fact, the schema I gave is just generally invalid, even without the .when
(where the object is specified directly).
It seems to be the fault of those .default()
calls on the objects. Something about a .required()
key being inside an object with default()
sets it off.
Joi should probably not error like that, but in the meantime, your schema is wrong. You can't both ask to create a default object and require a property in it that you don't provide.
Joi should probably not error like that, but in the meantime, your schema is wrong. You can't both ask to create a default object and require a property in it that you don't provide.
Yep, that looks like it. I just kept managing to trigger it in a strange way.
Changing the .default()
calls for .required()
makes the error go away and schema validate as expected.
I see we reached the same conclusion, you're indeed responsible for making valid defaults, all .default()
(without args) does is use an empty object and apply inner defaults, but if your schema requires some properties or your default value is invalid with the inner schema, it will indeed fail.
Runtime
Node.js
Runtime version
20.10.0
Module version
17.13.3
Last module version without issue
No response
Used with
NestJS, but being validated manually in this case
Any other relevant information
This can be replicated in the sandbox:
Schema:
Data:
I have tried a few different combinations of
.when()
and/orconditional()
but the same problem appears.What are you trying to achieve or the steps to reproduce?
I am attempting to call
.annotate()
on the resulting validation error.What was the result you got?
Error was thrown from within Joi:
What result did you expect?
An annotated string is generated.