1

How I can specify the length constraint of 'ID' from 15-25 symbols in my spring boot rest application using Redis?

@RedisHash("Foo")
public class Foo implements Serializable {

  @Id
  private Long id;

  @Indexed
  private Status status;
}

UPDATE

I have tried to change id type to String and set Size for symbols length limitations but it doesn't work(it seems Size annotation ignored) in the scope of Redis db usage:

@RedisHash("Foo")
public class Foo implements Serializable {

  @Id
  @Size(min = 15, max = 25)
  private String id;

  @Indexed
  private Status status;
}

I have also tried to use custom validator also and the same behaviour - seems like it`s ignored when Redis db is used:

@Documented
@Constraint(validatedBy = DigitsLimitValidator.class)
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DigitsLimit {

  String message() default "Digits length is too short or long";

  Class<?>[] groups() default {};

  Class<? extends Payload>[] payload() default {};

  int min() default 5;

  int max() default 15;

}

Validator:

public class DigitsLimitValidator implements ConstraintValidator<DigitsLimit, BigInteger> {

  private int min;
  private int max;

    @Override
    public void initialize(DigitsLimit value) {
      min = value.min();
      max = value.max();
    }

    @Override
    public boolean isValid(BigInteger value, ConstraintValidatorContext cxt) {
      return value != null && BigIntegerMath.log10(value, RoundingMode.FLOOR) + 1 >= min && BigIntegerMath.log10(value, RoundingMode.FLOOR) + 1 <= max;
    }

  }

Model:

@RedisHash("Foo")
public class Foo implements Serializable {

  @Id
  @DigitsLimit(min = 15, max = 25)
  private BigInteger id;

  @Indexed
  private Status status;
}

Under debugging mode, I cannot catch any breakpoints inside my validator.

In both attempts I can save any length of digits. Limitations are ignored. Why it was ignored?

Viktor M.
  • 4,393
  • 9
  • 40
  • 71

1 Answers1

0

How I can specify the length constraint of 'ID' from 15-25 symbols

It makes no sense to use the Long type as Long.MAX_VALUE has "only" 19 digits.

9223372036854775807

To contain such a value, BigInteger is probably a better choice.

You could not add the @Min and @Max annotations to a BigInteger field as the values are limited to the Long range. So you could not represent 25 digits.

So you have two ways :

  • creating a custom validator that ensures that your constraint is respected.

  • replacing the numeric value by a String. You could so use the javax.validation.constraints.Size annotation that is not applicable to numeric fields but is for CharSequence fields such as String.

For example :

@Id
@Size(min=15, max=25)
private String id;
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • Thanks! But how I can limit BigInteger in the case of 15-25 symbols? – Viktor M. Feb 19 '18 at 13:18
  • I would like to generate id with 15 symbols and the max allowed symbols is 25 – Viktor M. Feb 19 '18 at 13:30
  • You are welcome. It could not work with `BigInteger`. I edited with two ways. – davidxxx Feb 19 '18 at 13:37
  • Ou, thanks. If I choose custom validator based on BigInteger field how I can count number of digits in the field? Maybe there is more elegant way to count digits of BigInteger value beside the solution which I have found - https://stackoverflow.com/questions/18828377/biginteger-count-the-number-of-decimal-digits-in-a-scalable-method/18828536#18828536 – Viktor M. Feb 19 '18 at 14:12