AvantaR / AvantaR/nestjs-validation-tips
The code doesn't work when validation pipe has whitelist: true
- Dominant language
- TypeScript
- Stars
- 9
- Forks
- 4
- PR merge metrics
- No merged PRs in 30d
Description
@AvantaR first of all, want to thank you for your time invested in this example repo and dev.to article! It helped me a lot 🍻
Creating this issue as a note for anyone who would try to use this with `whitelist: true;` in ValidationPipe
Whitelist would strip `REQUEST_CONTEXT` from validate() arguments, so you would need to define it in your Dto e.g.:
```
@Validate(ProfileHandleConstraint) // my custom validator which relies on context
slug: string;
@IsObject()
[INJECT_USER_TO_REQ_CONTEXT]: any;
```
I also think that such complicated solution needs verbose errors when used in validator, so I made `getInjectedCurrentUserForValidate` util:
```
....
async validate(text: string, { object }) {
const currentUser = getInjectedCurrentUserForValidate(object);
....
export function getInjectedCurrentUserForValidate(object: any): UserBaseInfo {
const data = object[INJECT_USER_TO_REQ_CONTEXT];
if (data) {
return data.user;
} else {
throw new Error(
'Missing @InjectCurrentUserToReq() in controller or INJECT_USER_TO_REQ_CONTEXT in DTO (see NOTE in this file)',
);
}
}
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Review the ValidationPipe whitelist behavior together with the DTO field marked with INJECT_USER_TO_REQ_CONTEXT, the custom validator, and the getInjectedCurrentUserForValidate utility described in the issue. Confirm how REQUEST_CONTEXT is stripped and determine whether the intended outcome is a code change, clearer errors, or documentation; the issue does not define a specific completion condition.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100