title: ParseObjectIdPipe for MongoDB's ObjectID
body: <p>I want to create a NestJs API with TypeORM and MongoDB. My entity id fields are of type <code>ObjectID</code>. The controller routes should validate the incoming ids before passing them to the services. I know that Nest ships with the ParseIntPipe and ParseUUIDPipe but as far as I know there is nothing I can use for MongoDBs <code>ObjectID</code>.</p>
  
  <p>So I created my own pipe for those fields as described here <a href="https://docs.nestjs.com/pipes#transformation-use-case" rel="nofollow noreferrer">https://docs.nestjs.com/pipes#transformation-use-case</a></p>
  
  <pre><code>import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common';
  import { ObjectID } from 'typeorm';
  
  @Injectable()
  export class ParseObjectIdPipe implements PipeTransform&lt;any, ObjectID&gt; {
    transform(value: any): ObjectID {
      const validObjectId: boolean = ObjectID.isValid(value);
  
      if (validObjectId) {
        throw new BadRequestException('Invalid ObjectId');
      }
  
      const objectId: ObjectID = ObjectID.createFromHexString(value);
      return objectId;
    }
  }
  </code></pre>
  
  <p>and hope this will do the trick, even for edge cases. I can use it for my route params like</p>
  
  <pre><code>  @Get(':id')
    public getUserById(@Param('id', ParseObjectIdPipe) id: ObjectID): Promise&lt;User&gt; {
      return this.usersService.getUserById(id);
    }
  </code></pre>
  
  <p>The problem I have is that some routes need a <code>Body</code> validation. I use the class-validator package as described here </p>
  
  <p><a href="https://docs.nestjs.com/techniques/validation" rel="nofollow noreferrer">https://docs.nestjs.com/techniques/validation</a></p>
  
  <p>It seems that I have to create my own class-validator decorator for those <code>ObjectID</code> fields but that should be fine. Maybe I'll find something here on how to do it <a href="https://github.com/typestack/class-validator#custom-validation-classes" rel="nofollow noreferrer">https://github.com/typestack/class-validator#custom-validation-classes</a>. But how can I transform those fields to the type <code>ObjectID</code>? Can I use the custom pipe for that later on too?</p>
  
  <hr>
  
  <p><strong>Update:</strong></p>
  
  <p>I also tried to transform the value via class-transformer package. So the code for this is</p>
  
  <pre><code>import { ObjectID } from 'typeorm';
  import { Type, Transform } from 'class-transformer';
  import { BadRequestException } from '@nestjs/common';
  
  export class FooDTO {
    @Type(() =&gt; ObjectID)
    @Transform(bar =&gt; {
      if (ObjectID.isValid(bar)) {
        throw new BadRequestException('Invalid ObjectId');
      }
  
      return ObjectID.createFromHexString(bar);
    })
    public bar: ObjectID;
  }
  </code></pre>
  
  <p>Unfortunately the value <code>bar</code> is always <code>undefined</code>. But maybe this code might help for validation and transformation purposes...</p>