Skip to content

Commit 364e258

Browse files
committed
spring-projectsgh-4901: introduce ValidatingEntityCallback
Signed-off-by: felgentraeger <[email protected]>
1 parent ceac2b8 commit 364e258

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.mapping.event;
17+
18+
import jakarta.validation.ConstraintViolationException;
19+
import jakarta.validation.Validator;
20+
import java.util.Set;
21+
import org.apache.commons.logging.Log;
22+
import org.apache.commons.logging.LogFactory;
23+
import org.bson.Document;
24+
import org.springframework.core.Ordered;
25+
import org.springframework.util.Assert;
26+
27+
/**
28+
* javax.validation dependant entities validator.
29+
* <p>
30+
* When it is registered as Spring component its automatically invoked
31+
* after any {@link AbstractMongoEventListener} and before entities are saved in database.
32+
*
33+
* @author original authors of {@link ValidatingMongoEventListener}
34+
* @author Rene Felgenträger
35+
*
36+
* @see {@link ValidatingMongoEventListener}
37+
*/
38+
public class ValidatingEntityCallback implements BeforeSaveCallback<Object>, Ordered {
39+
40+
private static final Log LOG = LogFactory.getLog(ValidatingEntityCallback.class);
41+
42+
// TODO: discuss with spring team, if a handler encapsulating validation logic makes more sense (similar to "AuditingHandler")
43+
private final Validator validator;
44+
45+
/**
46+
* Creates a new {@link ValidatingEntityCallback} using the given {@link Validator}.
47+
*
48+
* @param validator must not be {@literal null}.
49+
*/
50+
public ValidatingEntityCallback(Validator validator) {
51+
52+
Assert.notNull(validator, "Validator must not be null");
53+
this.validator = validator;
54+
}
55+
56+
// TODO: alternatively implement the "BeforeConvertCallback" interface and set the order to highest value ?
57+
@Override
58+
public Object onBeforeSave(Object entity, Document document, String collection) {
59+
60+
if (LOG.isDebugEnabled()) {
61+
LOG.debug(String.format("Validating object: %s", entity));
62+
}
63+
Set violations = validator.validate(entity);
64+
65+
if (!violations.isEmpty()) {
66+
67+
if (LOG.isDebugEnabled()) {
68+
LOG.info(String.format("During object: %s validation violations found: %s", event.getSource(), violations));
69+
}
70+
throw new ConstraintViolationException(violations);
71+
}
72+
return entity;
73+
}
74+
75+
@Override
76+
public int getOrder() {
77+
return 100;
78+
}
79+
}

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/ValidatingMongoEventListener.java

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
* @author Maciej Walkowiak
3333
* @author Oliver Gierke
3434
* @author Christoph Strobl
35+
*
36+
* @see {@link ValidatingEntityCallback}
3537
*/
3638
public class ValidatingMongoEventListener extends AbstractMongoEventListener<Object> {
3739

0 commit comments

Comments
 (0)