making/yavi

Missing MessagerFormatter on ConstraintViolation

ffroliva opened this issue · 1 comments

I have the following class:

/**
 * A custom violation that includes a prefix which is shown with the violation message.
 */
@Getter
@ToString
public class PrefixedViolation extends ConstraintViolation {

    /**
     * The wrapped violation.
     */
    private final ConstraintViolation violation;

    /**
     * The message prefix.
     */
    private final String prefix;

    private PrefixedViolation(ConstraintViolation constraintViolation, String prefix) {
        super(constraintViolation.name(),
                constraintViolation.messageKey(),
                constraintViolation.defaultMessageFormat(),
                constraintViolation.args(),
                new SimpleMessageFormatter(),
                constraintViolation.locale());
        this.violation = constraintViolation;
        this.prefix = prefix;
    }


    /**
     * Returns a prefixed violation with no prefix.
     */
    public static PrefixedViolation withoutPrefix(ConstraintViolation violation) {
        return new PrefixedViolation(violation, "");
    }

    /**
     * Returns a prefixed violation with the specified prefix.
     */
    public static PrefixedViolation withPrefix(ConstraintViolation violation, String prefix) {
        return new PrefixedViolation(violation, prefix);
    }

    /**
     * The prefixed violation message.
     */
    public String message() {
        return prefix + super.message();
    }

}

I would like to use the MessageFormatter messageFormatter; from the ConstraintViolation but it doesn't expose it.

Could it be added?

Currently, I am adding the new SimpleMessageFormatter()

Solution, add:

MessageFormatter messageFormatter() {
    return messageFormatter;
}

I think that adding it would not be a harm.

Thank you.

Actually extending ConstraintViolation was not expected.
Why did you need PrefixedViolation ?