-
Notifications
You must be signed in to change notification settings - Fork 19
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
Joi.date and Joi.in raise error #53
Comments
Hey @maltere. I think the issue is that you're not telling Joigoose to convert the values before trying to pass it into Mongo. So Joigoose interprets But because the See here for how to pass additional options to the instance of Joi which is used in Joigoose: https://github.com/yoitsro/joigoose#with-joi-options-which-apply-to-all-validators I'll leave this open for now in case this doesn't fix it, but please close it if this works. |
Thank you @yoitsro for your response! So I tried multiple things according to your answer within my given Test Script: 1. Adding only the convert flagThis means the line were I require joigoose changes to the following: const Joigoose = require('joigoose')(connection, { convert: true }); 2. Also changing the Input to the Mongoose Model
My input to the new Model was actually already the converted Object by Joi. So Mongoose can actually already expect a Date object, because they are. But to be sure this does not produce the error in joigoose I changed the script to create the Model with the original Object. const instance = new Model(obj); instead of const instance = new Model(joiResult.value); However, both attempts produce the same output and error as given above. |
A working example would be, changing the defined const schema = Joi.object().keys({
dates: Joi.array().items(Joi.date()),
assignedOn: Joi.date().allow(null).default(null)
}); Therefore I suggested the problem lays within the use of The output to the mentioned modification would be:
|
Great debugging. Seems like we need to add a test suite for How would you feel about maybe giving this a shot? |
This is an test case provoking the error discussed in issue yoitsro#53.
I added a test case provoking the error. After I took a deeper look into the
/*
* We reference "dates" so Joi is expecting a field on the same level as "assignedOn" called "dates".
*/
const schema = Joi.object().keys({
dates: Joi.array().items(Joi.date()),
assignedOn: Joi.date().allow(Joi.in('dates'), null).default(null)
});
const obj = {
"dates": [
"2021-02-09 16:00"
],
"assignedOn": "2021-02-09 16:00"
};
/*
* For validation we need to provide the whole object,
* because for the validation of "assignedOn" we need to access the values of "dates"
*/
console.log(schema.validate(obj)); I am willing to help fixing this issue, but currently I do not know enough about mongoose or joigoose to fix this issue. |
Thanks for taking a look into this. A known caveat of Joigoose is that there is no peer based validation right now. I think this could be fixed. There are two parts to Joigoose:
As it stands, the conversion stage works relatively well, but the validation stage leaves a lot to be desired. I wonder if the two stages could be decoupled and pretty much just use Joi's validation to perform validation on the input. That was the intention when I started out, but things escalated for one reason or another. I won't have time to look at this in the near future, but I'd happily review a PR. Sorry I can't be of more help at the moment. |
My issue arises with
Joi.date
in combination withJoi.in
and I think it is a bug, but correct me if I am doing something wrong.It seems to me that the mongoose is not validating the given object correctly
Test Script
This is my full Test Script I wrote to further evaluate this issue:
Output
System Information:
Do you need more information from me? I appreciate any help with this.
The text was updated successfully, but these errors were encountered: