spring-projects / spring-projects/spring-data-rest

Document how to enable validating entities with JSR-303 bean validation annotations [DATAREST-372]

Open
#706 1 comment 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

type: documentation type: enhancement
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

Marcel Overdijk opened DATAREST-372 and commented

The Spring Data REST homepage states:

  • Validate entities with JSR-303 or Spring Validator beans

However in the documentation I cannot find back how this should be enabled.
By default entities with JSR-303 annotation are not validated.

What I did to make it working is:

@Configuration
protected static class CustomRepositoryRestMvcConfiguration extends RepositoryRestMvcConfiguration {

    @Autowired
    private Validator validator;

    @Override
    protected void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
        validatingListener.addValidator("beforeCreate", validator);
        validatingListener.addValidator("beforeSave", validator);
    }
}

I don't know if this the best way to enable JSR-303 bean validation but either way I think it's good to add to the documentation how to enable it


Affects: 2.1.2 (Dijkstra SR2)

6 votes, 8 watchers

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.