class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

feature: add `isNotBlank` decorator

Open jingyuexing opened this issue 3 years ago • 1 comments

Description

we often need to check a string variable is not a empty or Map is not empty or Set is not empty

Proposed solution

function isBlank(val:string|Array<any>|Map<string,any>|Set<any>){
    if(val instanceof Array || val instanceof String){
      if(isTypes.isArray(val) || isTypes.isString(val)){
        return val.length == 0;
      }
    }else if(val instanceof Set || val instanceof Map){
      if(isTypes.isMap(val) || isTypes.isSet(val)){
        return val.size == 0;
      }
    }
  }

jingyuexing avatar Sep 13 '21 16:09 jingyuexing

We use javax.validation on the server side, and try to also validate on the client side. There we have https://javaee.github.io/javaee-spec/javadocs/javax/validation/constraints/NotEmpty.html and https://javaee.github.io/javaee-spec/javadocs/javax/validation/constraints/NotBlank.html.

Maybe the existing IsEmpty/IsNotEmpty decorators should also evaluate Arrays, Sets and Maps, and IsBlank/IsNotBlank should just evaluate strings and should trim() the string before checking.

tschulte avatar Jun 14 '22 10:06 tschulte

seems useful to me ;)

radvansky-tomas avatar Oct 21 '22 10:10 radvansky-tomas