2

I am trying to validate that the headers of the request contain some specific data, and I am using NestJS. I found this information. While this is what I want to do, and it looks proper, the ClassType reference does not exist, and I am not sure what to use instead.

From the example, the decorator is referring to.

request-header.decorator.ts

import { createParamDecorator, ExecutionContext } from '@nestjs/commom'
import { plainToClass } from 'class-transformer';
// The import below fails
import { ClassType } from 'class-transformer/ClassTransformer';
import { validateOrReject } from 'class-validator';

export const RequestHeader = createParamDecorator(
  async (value:  ClassType<unknown>, ctx: ExecutionContext) => {

    // extract headers
    const headers = ctx.switchToHttp().getRequest().headers;

    // Convert headers to DTO object
    const dto = plainToClass(value, headers, { excludeExtraneousValues: true });
    
    // Validate 
    await validateOrReject(dto);

    // return header dto object 
    return dto;
  },
);

1 Answer 1

3

Rather than passing the type through a decorator like this, I'd suggest making a custom decorator and setting the validateCustomDecorators option for the ValidationPipe to true. The decorator would look something like

const Header = createParamDecorator((data: unknown, context: ExecutionContext) => {
  const req = context.switchToHttp().getRequest();
  if (data) {
    return req.headers[data];
  }
  return req.headers;
});

And now instead of @Header() from @nestjs/common you can you @Header() from this file and get the ValidationPipe to run after applying the appropriate type metadata

Sign up to request clarification or add additional context in comments.

9 Comments

I get an error on the return req.headers[data]; which is stating that the Type 'unknown' cannot be used as an index type.ts(2538)
Probably need to add some type/data checking in the if (data). Something like if (data && typeof data === 'string') and it should be okay.
Sorry, new to NestJS, and the use of decorators is giving me some grief as they are not a straight flow through in the debugger. I am using the ` @RequestHeader HeaderInfo:SVSHeadersDTO` and that is then giving me an additonal error error TS1237: The return type of a parameter decorator function must be either 'void' or 'any'. Unable to resolve signature of parameter decorator when called as an expression. 44 @RequestHeader HeaderInfo:SVSHeadersDTO ~~~~~~~~~~~~~~
Let me accept your answer as I believe it is correct, as I assumed it would look something like that, since I have worked with the headers and the .getRequest()/.getResponse() in interceptors. I will have to make a bigger question around it all, as I need to introduce a custom validator as well.
Try using @RequestHeader() as a function, because the decorators are technically decorator factories (meaning they're a function that return the thing)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.