Validating strings with Java and NodeJS



vo.getResultFun() and cod returns 'G'

Java validation

if ( genericValidator.isBlankOrNull(vo.getResultFun()) || 
          !("G".equalsIgnoreCase(vo.getResultFun()) || "B".equalsIgnoreCase(vo.getResultFun()))) {
            throw new UCNaoCadastradaGerBenException();
        }

NodeJS

if (Validator.isNullUndefinedEmpty(cod) ||
            !(Validator.isEqual(cod, 'B', true) || Validator.isEqual(cod, 'G', true))) {
            callback(Translate.__('K1.CH1', lang), null);

isEqual

  static isEqual(str1: string, str2: string, ignoreCase: boolean = false): boolean {
    let ret = false;
    if (ignoreCase) {
      ret =
        (str1 === undefined && str2 === undefined) ||
        (str1 === null && str2 === null) ||
        (str1 != null && str2 != null && typeof str1 === 'string' && typeof str2 === 'string' && str1.toUpperCase() === str2.toUpperCase());
    } else {
      ret =
        (str1 === undefined && str2 === undefined) ||
        (str1 === null && str2 === null) ||
        (str1 != null && str2 != null && typeof str1 === 'string' && typeof str2 === 'string' && str1 === str2);
    }
    return ret;
  }

Why NodeJS return the callback and Java don’t throws the exception?

Answer

The result of this js part :

!(Validator.isEqual(cod, 'B', true) || Validator.isEqual(cod, 'G', true))

is false as the result of this java part:

!("G".equalsIgnoreCase(vo.getResultFun()) || "B".equalsIgnoreCase(vo.getResultFun()))

So there are several options :

  • Validator.isNullUndefinedEmpty doesn’t works
  • cod is not strictly equals to ‘G’
  • The callback function is not called


Source: stackoverflow