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?