spring-projects / spring-projects/spring-framework

Allow to override a validation message using bean validation message code [SPR-15967]

Open
#20,519 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: core type: enhancement
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

Kazuki Shimizu opened SPR-15967 and commented

I propose to allow to override a validation message using bean validation message code(like {javax.validation.constraint.Size.message}) at Spring's MessageSource.

e.g) message.properties

javax.validation.constraints.Size.message={0}'s size is must be between {2} and {1}

I know it can be realised using a constraint validation simple class name like as:

Size={0}'s size is must be between {2} and {1}

But there is a case that can't use it.

For example, there is a case that uses composed annotation not work fine as follow:

@Documented
@Constraint(validatedBy = {})
@Target({ TYPE, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Compare(left = "", right = "", operator = Compare.Operator.EQUAL, requireBoth = true) // use composed annotation
public @interface Confirm {

    @OverridesAttribute(constraint = Compare.class, name = "message")
    String message() default "{com.example.sample.domain.validation.Confirm.message}";

    // ...

}

In this case, following definition does not work (can't override a default message generated by bean validation), because bean validation creates a ConstraintViolation of @Compare instead of @Confirm.

Confirm=must be same value with "{4}"
password=Password

I want to override a message corresponding @Confirm using Spring's MessageSource feature as follow:

e.x ) message.properties

com.example.sample.domain.validation.Confirm.message=must be same value with "{4}"
password=Password

I will submit a PR at later.

Thanks.


Affects: 4.3.11, 5.0 RC4

Referenced from: pull request https://github.com/spring-projects/spring-framework/pull/1528

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing Spring's MessageSource integration with Bean Validation message resolution, using the example message.properties entries and composed @Confirm annotation as the behavior to understand. Verify how a bean validation message code such as {javax.validation.constraints.Size.message} or {com.example.sample.domain.validation.Confirm.message} is resolved, and define done as allowing those codes to override the default validation message.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.