title: NestJS transform a property using ValidationPipe before validation execution during DTO creation
body: <p>I'm using the built in NestJS ValidationPipe along with class-validator and class-transformer to validate and sanitize inbound JSON body payloads.  One scenario I'm facing is a mixture of upper and lower case property names in the inbound JSON objects.  I'd like to rectify and map these properties to standard camel-cased models in our new TypeScript NestJS API so that I don't couple mismatched patterns in a legacy system to our new API and new standards, essentially using the @Transform in the DTOs as an isolation mechanism for the rest of the application. For example, properties on the inbound JSON object:</p>
  <pre><code>&quot;propertyone&quot;,
  &quot;PROPERTYTWO&quot;,
  &quot;PropertyThree&quot;
  </code></pre>
  <p>should map to</p>
  <pre><code>&quot;propertyOne&quot;,
  &quot;propertyTwo&quot;,
  &quot;propertyThree&quot;
  </code></pre>
  <p>I'd like to use @Transform to accomplish this, but I don't think my approach is correct.  I'm wondering if I need to write a custom ValidationPipe.  Here is my current approach.</p>
  <p>Controller:</p>
  <pre class="lang-js prettyprint-override"><code>import { Body, Controller, Post, UsePipes, ValidationPipe } from '@nestjs/common';
  import { TestMeRequestDto } from './testmerequest.dto';
  
  @Controller('test')
  export class TestController {
    constructor() {}
  
    @Post()
    @UsePipes(new ValidationPipe({ transform: true }))
    async get(@Body() testMeRequestDto: TestMeRequestDto): Promise&lt;TestMeResponseDto&gt; {
      const response = do something useful here... ;
      return response;
    }
  }
  </code></pre>
  <p>TestMeModel:</p>
  <pre class="lang-js prettyprint-override"><code>import { IsNotEmpty } from 'class-validator';
  
  export class TestMeModel {
    @IsNotEmpty()
    someTestProperty!: string;
  }
  </code></pre>
  <p>TestMeRequestDto:</p>
  <pre class="lang-js prettyprint-override"><code>import { IsNotEmpty, ValidateNested } from 'class-validator';
  import { Transform, Type } from 'class-transformer';
  import { TestMeModel } from './testme.model';
  
  export class TestMeRequestDto {
    @IsNotEmpty()
    @Transform((propertyone) =&gt; propertyone.valueOf())
    propertyOne!: string;
  
    @IsNotEmpty()
    @Transform((PROPERTYTWO) =&gt; PROPERTYTWO.valueOf())
    propertyTwo!: string;
  
    @IsNotEmpty()
    @Transform((PropertyThree) =&gt; PropertyThree.valueOf())
    propertyThree!: string;
  
    @ValidateNested({ each: true })
    @Type(() =&gt; TestMeModel)
    simpleModel!: TestMeModel
  
  }
  </code></pre>
  <p>Sample payload used to POST to the controller:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;propertyone&quot;: &quot;test1&quot;,
    &quot;PROPERTYTWO&quot;: &quot;test2&quot;,
    &quot;PropertyThree&quot;: &quot;test3&quot;,
    &quot;simpleModel&quot;: { &quot;sometestproperty&quot;: &quot;test4&quot; }
  }
  </code></pre>
  <p>The issues I'm having:</p>
  <ol>
  <li>The transforms seem to have no effect.  Class validator tells me that each of those properties cannot be empty.  If for example I change &quot;propertyone&quot; to &quot;propertyOne&quot; then the class validator validation is fine for that property, e.g. it sees the value. The same for the other two properties.  If I camelcase them, then class validator is happy.  Is this a symptom of the transform not running before the validation occurs?</li>
  <li>This one is very weird.  When I debug and evaluate the TestMeRequestDto object, I can see that the simpleModel property contains an object containing a property name &quot;sometestproperty&quot;, even though the Class definition for TestMeModel has a camelcase &quot;someTestProperty&quot;. Why doesn't the @Type(() =&gt; TestMeModel) respect the proper casing of that property name?  The value of &quot;test4&quot; is present in this property, so it knows how to understand that value and assign it.</li>
  <li>Very weird still, the @IsNotEmpty() validation for the &quot;someTestProperty&quot; property on the TestMeModel is not failing, e.g. it sees the &quot;test4&quot; value and is satisfied, even though the inbound property name in the sample JSON payload is &quot;sometestproperty&quot;, which is all lower case.</li>
  </ol>
  <p>Any insight and direction from the community would be greatly appreciated.  Thanks!</p>