PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 30, 2022

[FIXED] How to pass class-validator @isArray when single item comes from query param

 July 30, 2022     class-validator, nestjs, typescript, validation     No comments   

Issue

I'm trying to validate request with class-validator if it's array.

inputs comes from query param with /api/items?someTypes=this

my request dto looks like this.

    (...)
    @IsArray()
    @IsEnum(SOMETHING, {each: true})
    readonly someTypes: keyof typeof SOMETHING[];
    (...)

when I give only one item, @IsArray gives validation error, saying it is not an array.

I want to make it array too when one item comes from query param, but I don't know how.

I know using /api/items?someTypes[]=this will pass validation.

but I want to know if there is another way to solve this.


Solution

If your field is an array and you want to perform validation of each item in the array you must pass a special each: true option to the @IsEnum() decorator.

import { IsArray, IsEnum } from 'class-validator';

enum SomeType {
  A,
  B,
  C,
}

class SearchQuery {
  @IsArray()
  @IsEnum(SomeType, { each: true })
  types: SomeType[];
}


@Controller()
export class AppController {
  @Get()
  async search(@Query() searchQuery: SearchQuery): Promise<void> 
  { ... }
}

You can then perform a GET request to your endpoint using the following syntax.

?types[]=A&types[]=B&types[]=C

That works perfectly fine when you pass just one item.

?types[]=A

If you don't like this syntax and prefer types=A,B,C then you can combine class-validator with class-tranformer.

class SearchQuery {
  @IsArray()
  @IsEnum(SomeType, { each: true })
  @Transform(({ value }) =>
    value
      .trim()
      .split(',')
      .map((type) => SomeType[type]),
  )
  types: SomeType[];
}


Answered By - Christophe Geers
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing