5

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": []
}
2
  • You can use @IsNotEmpty().
    – Rajan
    Apr 17, 2020 at 6:24
  • 1
    @IsNotEmpty() doesn't catch an empty array, because [] IS a non-empty value. As the selected answer show,s you must use @ArrayNotEmpty() Jul 22, 2022 at 13:57

1 Answer 1

18

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

1
  • Really close in name to @IsNotEmpty(). Could see that catching people Feb 3, 2022 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.