help with custom validator

hey guys. can smb help me out with writing my won validation annotation? i have entityPayment and DTO PaymentDTO and they have private BigDecimal amount field. i want to validate it bc i want this field's length to be 7 positions at maximum. so far i have this:
@Documented
@Constraint(validatedBy = AmountValidator.class)
@Target( { ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface AmountConstraint {
    String message() default "Invalid amount";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

public class AmountValidator implements ConstraintValidator<AmountConstraint, ???> {
    @Override
    public void initialize(AmountConstraint amountConstraint){}

    @Override
    public boolean isValid(BigDecimal amountField, ConstraintValidatorContext constraintValidatorContext) {
        boolean result= amountField!=null&& fields max length is less than 7 positions
        return result;
    }
}

  1. what to write instead of ??? in my AmountValidator? should it be data type of my field? i.e. BigDecimal?
  2. how to check if my field's max length is less than 7 positions in isValid method in the AmountValidator?
    thanks in advance.
Was this page helpful?