Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
In the nest.js application on controller level I have to validate DTO.
I've faced with difficulty to check if item is not null (request should be rejected if any list item is
null
or
undefined
)
Code bellow demonstrates my configured verifications.
import { ArrayMinSize, IsArray } from 'class-validator'
export class ReminderPayload {
// ...
@IsArray()
@ArrayMinSize(1)
recipients: string[]
Question
I'm looking for help to reject requests with body data like
"recipients": [
How to validate if array items are string
only (it should reject handling if object is in the array item position)?
'class-validator'
injected successfully, and it produces some validation results for my API.
You need to tell class-validator to run the validations on each
item of the array. Change your payload DTO to the following:
import { ArrayMinSize, IsArray, IsString } from 'class-validator';
export class ReminderPayloadDto {
// ...
@IsArray()
// "each" tells class-validator to run the validation on each item of the array
@IsString({ each: true })
@ArrayMinSize(1)
recipients: string[];
Link to the docs on this.
–
–
For someone who wants to validate specific strings in array:
class MyDto {
@IsIn(['monday', 'tuesday', 'wednesday', 'thursday', 'friday'], { each: true })
weekdays: string[];
// regex mask validation
@Matches('^[a-zA-Z\\s]+$', undefined, { each: true })
words: string[];
@Contains('hello', { each: true })
greetings: string[];
For custom validation:
import {
ArrayNotEmpty,
IsArray,
Validate,
ValidateNested,
ValidatorConstraint,
ValidatorConstraintInterface
} from 'class-validator'
@ValidatorConstraint({ name: 'arrayPrefixValidator' })
export class ArrayPrefixValidator implements ValidatorConstraintInterface {
validate(values: string[] = []): boolean {
if (values.length) {
return values.every((value) => value.startsWith('user-'))
return false
class MyDto {
// Each item contains a prefix str-
@Validate(ArrayPrefixValidator, { message: 'No user- prefix' })
accounts: string[];
For more information go to official docs
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.