model/src/main/java/eu/internetofus/common/model/ValidateContext.java
/**
   * Verify a field value is {@code null} or a valid telephone.
   *
   * @param fieldName name of the checking field.
   * @param value     to verify.
   * @param locale    to use.
   * @param promise   to notify of any error.
   *
   * @return the normalized telephone value.
   */
Verify a field value is  null or a valid telephone.
@param fieldName name of the checking field.
@param value     to verify.
@param locale    to use.
@param promise   to notify of any error.

@return the normalized telephone value.
Verify a field value is null or a valid telephone . @param fieldName name of the checking field . @param value to verify . @param locale to use . @param promise to notify of any error . @return the normalized telephone value .
default String validateNullableTelephoneField(final String fieldName, final String value, final String locale,
      final Promise<Void> promise) {
    if (value != null) {
      final var trimmedValue = value.trim();
      if (trimmedValue.length() > 0) {
        try {
          final var phoneUtil = PhoneNumberUtil.getInstance();
          var defaultRegion = Locale.getDefault().getCountry();
          if (locale != null) {
            defaultRegion = new Locale(locale).getCountry();
          }
          final var number = phoneUtil.parse(trimmedValue, defaultRegion);
          if (!phoneUtil.isValidNumber(number)) {
            promise.tryFail(new ValidationErrorException(this.fieldErrorCode(fieldName),
                "The '" + trimmedValue + "' is not a valid telephone number"));
            return value;
          } else {
            return phoneUtil.format(number, PhoneNumberFormat.INTERNATIONAL);
          }
        } catch (final Throwable badTelephone) {
          promise.tryFail(new ValidationErrorException(this.fieldErrorCode(fieldName), badTelephone));
          return value;
        }
      }
    }
    return null;
  }
default String validateNullableTelephoneField ( final String fieldName , final String value , final String locale , final Promise < Void > promise ) { if ( value != null ) { final var trimmedValue = value . trim ( ) ; if ( trimmedValue . length ( ) > 0 ) { try { final var phoneUtil = PhoneNumberUtil . getInstance ( ) ; var defaultRegion = Locale . getDefault ( ) . getCountry ( ) ; if ( locale != null ) { defaultRegion = new Locale ( locale ) . getCountry ( ) ; } final var number = phoneUtil . parse ( trimmedValue , defaultRegion ) ; if ( ! phoneUtil . isValidNumber ( number ) ) { promise . tryFail ( new ValidationErrorException ( this . fieldErrorCode ( fieldName ) , "The '" + trimmedValue + "' is not a valid telephone number" ) ) ; return value ; } else { return phoneUtil . format ( number , PhoneNumberFormat . INTERNATIONAL ) ; } } catch ( final Throwable badTelephone ) { promise . tryFail ( new ValidationErrorException ( this . fieldErrorCode ( fieldName ) , badTelephone ) ) ; return value ; } } } return null ; }
Verify a field value is {@code null} or a valid telephone.
Verify a field value is { @code null } or a valid telephone .