-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DATAMONGO-36 - Added support for JSR-303 validation
Added event listener that uses a java.validation.Validator to trigger validation on an entity before persisting it. This validation is enabled by default if the javx.validation API is present on the classpath. Added namespace attribute 'disable-validation' to allow disabling that auto enabling behavior.
- Loading branch information
Showing
15 changed files
with
851 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...ava/org/springframework/data/mongodb/core/mapping/event/ValidatingMongoEventListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright 2012 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.mongodb.core.mapping.event; | ||
|
||
import java.util.Set; | ||
|
||
import javax.validation.ConstraintViolationException; | ||
import javax.validation.Validator; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.util.Assert; | ||
|
||
import com.mongodb.DBObject; | ||
|
||
/** | ||
* javax.validation dependant entities validator. When it is registered as Spring component its automatically invoked | ||
* before entities are saved in database. | ||
* | ||
* @author Maciej Walkowiak | ||
*/ | ||
public class ValidatingMongoEventListener extends AbstractMongoEventListener<Object> { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ValidatingMongoEventListener.class); | ||
|
||
private final Validator validator; | ||
|
||
/** | ||
* Creates a new {@link ValidatingMongoEventListener} using the given {@link Validator}. | ||
* | ||
* @param validator must not be {@literal null}. | ||
*/ | ||
public ValidatingMongoEventListener(Validator validator) { | ||
Assert.notNull(validator); | ||
this.validator = validator; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener#onBeforeSave(java.lang.Object, com.mongodb.DBObject) | ||
*/ | ||
@Override | ||
@SuppressWarnings({ "rawtypes", "unchecked" }) | ||
public void onBeforeSave(Object source, DBObject dbo) { | ||
|
||
LOG.debug("Validating object: {}", source); | ||
Set violations = validator.validate(source); | ||
|
||
if (!violations.isEmpty()) { | ||
|
||
LOG.info("During object: {} validation violations found: {}", source, violations); | ||
throw new ConstraintViolationException(violations); | ||
} | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
spring-data-mongodb/src/main/resources/META-INF/spring.schemas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.1.xsd=org/springframework/data/mongodb/config/spring-mongo-1.1.xsd | ||
http\://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd=org/springframework/data/mongodb/config/spring-mongo-1.0.xsd | ||
http\://www.springframework.org/schema/data/mongo/spring-mongo.xsd=org/springframework/data/mongodb/config/spring-mongo-1.0.xsd | ||
http\://www.springframework.org/schema/data/mongo/spring-mongo.xsd=org/springframework/data/mongodb/config/spring-mongo-1.1.xsd |
Oops, something went wrong.