Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing MessagerFormatter on ConstraintViolation #281

Open
ffroliva opened this issue Sep 26, 2022 · 1 comment
Open

Missing MessagerFormatter on ConstraintViolation #281

ffroliva opened this issue Sep 26, 2022 · 1 comment

Comments

@ffroliva
Copy link
Contributor

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.

@making
Copy link
Owner

making commented Nov 17, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants