title: How to create decorator in nest.js?
body: <p>Im having troubles with this quite a while. My decorator supposed to give information for checking field for unique value. It looks like so:</p>
  
  <pre><code>export const IsUnique = (
    metadata: {
      entity: any,
      field: string,
    },
  ): PropertyDecorator =&gt; {
    return createPropertyDecorator(constants.custom_decorators.is_unique, metadata);
  };
  </code></pre>
  
  <p>And my validation looks like so:</p>
  
  <pre><code>import { HttpException } from '@nestjs/common';
  import { PipeTransform, Pipe, ArgumentMetadata, HttpStatus } from '@nestjs/common';
  import {validate, ValidationError} from 'class-validator';
  import { plainToClass } from 'class-transformer';
  import constants from '../../../constants';
  
  @Pipe()
  export class ValidationPipe implements PipeTransform&lt;any&gt; {
    async transform(value, metadata: ArgumentMetadata) {
      console.log(arguments);
      const { metatype } = metadata;
      if (!metatype || !this.toValidate(metatype)) {
        return value;
      }
  
      const object = plainToClass(metatype, value);
      const errors = await validate(object);
      const myErrors = await this.uniqueValidation(object);
      if (errors.length &gt; 0) {
        throw new HttpException(errors, HttpStatus.BAD_REQUEST);
      }
      return value;
    }
  
    private toValidate(metatype): boolean {
      const types = [String, Boolean, Number, Array, Object];
      return !types.find((type) =&gt; metatype === type);
    }
  
    private async uniqueValidation(object): Promise&lt;ValidationError[]|null&gt;{
      const md = Reflect.getMetadata('swagger/apiModelPropertiesArray', object);
      console.log(md);
  
      return null;
    }
  }
  </code></pre>
  
  <p>After executing code, looks like md=undefined. So how i can retrieve my metadata? Maybe, im using createPropertyDecorator in a wrong way?</p>
  
  <p>EDIT:
  After cuple of hours i realized that nestjs doesnt have "createPropertyDecorator", ive imported it from swagger module (BIG mistake). So i need to create my own function. Now im doing it like so:</p>
  
  <pre><code>export const IsUnique = (
    metadata: {
      entity: any,
      field: string,
    },
  ): PropertyDecorator =&gt; {
    return (target: object, propertyKey: string) =&gt; {
      const args = Reflect.getMetadata(constants.custom_decorators.is_unique, target, propertyKey) || {};
      const modifiedArgs = Object.assign(args, { IsUnique: metadata.field });
      Reflect.defineMetadata(constants.custom_decorators.is_unique, modifiedArgs, target);
    };
  };
  </code></pre>
  
  <p>So, my question is the same - how to properly define metadata, so it never interference with other's?</p>