Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Js: Validate empty array using class validator

How to check whether the array is empty or not using class-validator.
Given below is my structure. I want to check if cartId is empty or not.

{
   "cartId": []
}
like image 514
Black Magic Avatar asked Mar 21 '26 10:03

Black Magic


2 Answers

What you want is @ArrayNotEmpty(). You can find all built-in validators here: https://github.com/typestack/class-validator#validation-decorators

If you don't find - you can write your own validator: https://github.com/typestack/class-validator#custom-validation-classes

like image 76
satanTime Avatar answered Mar 23 '26 02:03

satanTime


To check if array is empty, you can use @ArrayNotEmpty(), like this:

@ArrayNotEmpty({ each: true })
cartId: [];
like image 43
TiagoPaz Avatar answered Mar 23 '26 03:03

TiagoPaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!