title: How can I validate an array of enum values with Nestjs
body: <p>I feel like a combination of <a href="https://stackoverflow.com/questions/60505649/forbid-specific-enum-value-for-dto-in-nestjs">this thread</a> and <a href="https://stackoverflow.com/questions/60334856/class-validator-with-array-of-nested-objects-in-nestjs">this thread</a> is what I need to implement, I'm having trouble drawing them together.</p>
  <p>I have a DTO that contains an <code>enum</code>.</p>
  <p>Using Postman, I am sending a <code>PurchasableType</code> of <code>FOO</code> and expecting to get an error of some sort. Reading through the above links, it seems like the process is quite involved; which makes me thing I'm completely missing the point.</p>
  <p>How can I use the validation pipe(s) to make sure only the values in the <code>purchasable-type.enum.ts</code> are allowed?</p>
  <p>Thank you for any suggestions!</p>
  <pre class="lang-js prettyprint-override"><code>// create-order.dto.ts
  
  import { IsEmail, IsNotEmpty, IsEnum } from 'class-validator';
  import { PurchasableType } from '../enum/purchaseable-type.enum';
  
  export class CreateOrderDto {
    @IsNotEmpty()
    readonly userId: string;
  
    @IsNotEmpty()
    readonly locationId: string;
  
    @IsNotEmpty()
    @IsEnum(PurchasableType)
    readonly purchasableType: PurchasableType;
  
    @IsNotEmpty()
    @IsEmail()
    readonly email: string;
  }
  </code></pre>
  <pre class="lang-js prettyprint-override"><code>// purchasable-type.enum.ts
  
  export enum PurchasableType {
    CLINIC = 'CLINIC',
    EVENT = 'EVENT',
    LESSON = 'LESSON',
    RESERVATION = 'RESERVATION',
    TEAM = 'TEAM',
  }
  
  </code></pre>
  <p><strong>EDIT</strong></p>
  <p>It seems I was also not defining the entity correctly, and that may have been the main issue. I am still curious if my implementation good/bad.</p>
  <pre class="lang-js prettyprint-override"><code>// order.entity.ts
  
  ...
  import { PurchasableType } from '../enum/purchaseable-type.enum';
  
  @Entity()
  export class Order extends BaseEntity {
    @PrimaryGeneratedColumn()
    id: number;
  
  @Column({
      type: 'enum',
      enum: PurchasableType,
    })
  </code></pre>
  <p>Now when I send a <code>purchasableType</code> of <code>foo</code> I am getting a 500 error. If I send any valid value that is within the <code>enum</code> I am getting a 200/201.</p>
  <p><strong>EDIT 2</strong></p>
  <p>Sure - here is a bit wider view of what I've got. Everything <em>seems</em> to be working properly, I'd just like to have a better grasp of what was really happening.</p>
  <pre><code>// event.controller.ts
  
  @Post('/:id/orders')
    async purchaseEventTickets(@Body() createOrderDto: CreateOrderDto): 
      Promise&lt;Order&gt; {
      return await this.eventService.purchaseEventTickets(createOrderDto);
    }
  
  </code></pre>
  <pre><code>// create-order.dto.ts
  
  export class CreateOrderDto {
      @IsNotEmpty()
      @IsEnum(PurchasableType)
      readonly purchasableType: PurchasableType;
  }
  </code></pre>
  <pre><code>// event.service.ts
  
  async purchaseEventTickets(createOrderDto: CreateOrderDto): Promise&lt;Order&gt; {
      ...
      return await this.orderRepository.createOrder(createOrderDto);
  }
  
  </code></pre>
  <pre><code>// order.repository.ts
  
  async createOrder(createOrderDto: CreateOrderDto): Promise&lt;Order&gt; {
      const { purchasableType } = createOrderDto;
  
      const order = this.create();
  
      order.purchasableType = purchasableType;
  
      try {
          await order.save();
      } catch (error) {
          this.logger.error(`Failed to create the order: ${error.stack}`);
  
          throw new InternalServerErrorException();
      }
  
      return order;
  }
  
  </code></pre>
  <p>Using Postman, if I send an invalid value of &quot;Foo&quot; as a <code>PurchasableType</code> I get the expected error.</p>