Closed anhpt97 closed 1 year ago
P.S. I'm temporarily using a regular expression to fix this issue:
message = message.replace(/()(\.)(\d[^.]*)()/g, '$1['$3']$4');
I believe that we can't use isNumberString
from class-validator because ValidationPipe
doesn't requires class-validator (see loadValidator
method)
I believe that we can't use
isNumberString
from class-validator becauseValidationPipe
doesn't requires class-validator (seeloadValidator
method)
Yeah, I understand, so I just edited the content of this issue. I used this regular expression:
/^\d/.test(error.property)
(check if "error.property" must start with a numeric character)
What about negative numbers?
What about negative numbers?
Hmmm, so the regular expression would look like this:
/^\-?\d/
It seems that I have not been able to cover all possible scenarios.
After a few hours of searching, I think this is probably the best regular expression:
/[^\p{L}]/gu
In case if the value of "error.property" is all letters, part of the error message will have dot notation style (a.b), otherwise it will have bracket notation style (a['b']). Although I would like it to have dot notation style if error.property = "abc123", for example.
Sorry if my wording is a bit confusing.
Ok can you clarify why exactly do you think that the current behavior is wrong? Just to align the expectations here
@micalevisk I set logging in multiple places in 3 functions ("flattenValidationErrors", "mapChildrenToValidationErrors" and "prependConstraintsWithParentProp") and found out the value of "error.property" is having issues. As you can see from the example above, "username.username.0.username" doesn't seem to be correct but should be "username.username[0].username". (Sorry if my answer doesn't really answer your question.)
I don't know why we have that dot notation (which is invalid for JS) but there is an unit test asserting such scenario, see:
so, to me, the current behavior seems to be correct as it is aligned with the test suite :thinking:
Hmmm, I haven't read through the "validation.pipe.spec.ts" file so I didn't think it was a correct behavior. If that's the author's expectation then I'll probably solve the problem myself, although I don't think "test.0.prop1" is correct 😀.
This is just an error message - it's not supposed to be parsed and executed as JS instruction, that's why we decided that .0.
notation is good enough
Dziękuję za wyjaśnienie, panie Myśliwiec! :)
Is there an existing issue for this?
Current behavior
I just discovered something that I think could be a bug here: https://github.com/nestjs/nest/blob/master/packages/common/pipes/validation.pipe.ts#L266
When I validate a complex DTO:
Result:
Minimum reproduction code
https://stackblitz.com/edit/nestjs-typescript-starter-jtqamz
Steps to reproduce
No response
Expected behavior
I think the code should be fixed to like this:
Result:
Package
Other package
No response
NestJS version
No response
Packages versions
Node.js version
No response
In which operating systems have you tested?
Other
No response