-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Fix #964 Joi wrong value parsing #1097
Fix #964 Joi wrong value parsing #1097
Conversation
lib/errors.js
Outdated
@@ -285,6 +285,9 @@ internals.annotate = function (stripColorCodes) { | |||
delete ref[replacement]; | |||
} | |||
else { | |||
if (typeof ref === 'string') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably the good fix but in the wrong place. If the error is further down the parsed object, this will fail. This should be in the very 1st if of the loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Marsup Could you provide a failing example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just add some more nesting on a
and cause a deeper error, like `a: "{b:{c:"string"}}". Your code basically only works if the error happens on the object being parsed, not inside.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Marsup I tried this:
const schema = {
a: Joi.object({
b: Joi.object({
c: Joi.string()
})
})
};
const input = {
a: '{"b":{"d":"string"}}'
};
expect(() => {
Joi.attempt(input, schema);
}).to.throw(/\"d\" is not allowed/);
done();
and that:
const schema = {
a: Joi.object({
b: Joi.object({
c: Joi.object({
d: Joi.string()
})
})
})
};
const input = {
a: '{"b":{"c":{"d":{}}}}'
};
expect(() => {
Joi.attempt(input, schema);
}).to.throw(/\"d\" must be a string/);
done();
Both cases are not failing without any changes to the fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I should have been more accurate, while this is fixing the failure, it's not fixing the issue. If you look at the error annotation, it's showing the list of errors correctly but can't pinpoint it on the input.
Eg. with your 2nd example :
ValidationError: {
"a": "{\"b\":{\"c\":{\"d\":{}}}}" // There should be a [1] here or on the parsed object
}
[1] "d" must be a string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Marsup I fixed the issue with the '_$miss$_forbiddenKey|1_$end$_'
TypeError.
You are talking about an issue with error annotation.
I added a failing test case for the missing [1]
, but I don't know how to fix it yet.
Moving the typeof ref === 'string'
check to the very 1st if of the loop doesn't help.
@Marsup I fixed the pinpointing of errors. Please review the new changes. |
@Marsup What's the hold-up on this PR? |
Lacking time essentially. Adding more tests around this I noticed some nasty bugs that need fixing as well, and it involves a fair amount of rewrite of this algorithm, which is probably more than you'd be willing to do. I'll merge this PR when I'm sure I can ship the follow up patch with it. |
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. |
Fixes
TypeError: Cannot assign to read only property '_$miss$_forbiddenKey|1_$end$_'
when a parameter is given as a JSON string with incorrect property.