验证字符串 – Java 和 Node.js

huangapple go评论80阅读模式
英文:

Validating strings with Java and NodeJS

问题

`vo.getResultFun()` 和 `cod` 返回 `'G'`

**Java 验证**

    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;
      }

为什么 NodeJS 返回回调而 Java 不抛出异常?
英文:

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?

答案1

得分: 1

以下是翻译好的内容:

这段 JavaScript 代码的结果:

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

与这段 Java 代码的结果相同:

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

因此有几个可能的选项:

  • Validator.isNullUndefinedEmpty 不起作用
  • cod 不严格等于 'G'
  • 回调函数未被调用
英文:

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

huangapple
  • 本文由 发表于 2020年10月2日 22:40:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64173483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定