thundr-contrib-validation

JSR-303 bean validation support for Thundr

License

License

GroupId

GroupId

com.threewks.thundr
ArtifactId

ArtifactId

thundr-contrib-validation
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

thundr-contrib-validation
JSR-303 bean validation support for Thundr
Project URL

Project URL

http://github.com/kuhnza/thundr-contrib-validation
Project Organization

Project Organization

3wks
Source Code Management

Source Code Management

https://github.com/kuhnza/thundr-contrib-validation

Download thundr-contrib-validation

How to add to project

<!-- https://jarcasting.com/artifacts/com.threewks.thundr/thundr-contrib-validation/ -->
<dependency>
    <groupId>com.threewks.thundr</groupId>
    <artifactId>thundr-contrib-validation</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.threewks.thundr/thundr-contrib-validation/
implementation 'com.threewks.thundr:thundr-contrib-validation:1.0.0'
// https://jarcasting.com/artifacts/com.threewks.thundr/thundr-contrib-validation/
implementation ("com.threewks.thundr:thundr-contrib-validation:1.0.0")
'com.threewks.thundr:thundr-contrib-validation:jar:1.0.0'
<dependency org="com.threewks.thundr" name="thundr-contrib-validation" rev="1.0.0">
  <artifact name="thundr-contrib-validation" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.threewks.thundr', module='thundr-contrib-validation', version='1.0.0')
)
libraryDependencies += "com.threewks.thundr" % "thundr-contrib-validation" % "1.0.0"
[com.threewks.thundr/thundr-contrib-validation "1.0.0"]

Dependencies

compile (2)

Group / Artifact Type Version
com.threewks.thundr : thundr jar 1.1.2
org.hibernate : hibernate-validator jar 4.3.2.Final

provided (1)

Group / Artifact Type Version
javax.el : javax.el-api jar 2.2.4

test (3)

Group / Artifact Type Version
junit : junit jar 4.11
org.hamcrest : hamcrest-all jar 1.3
org.mockito : mockito-all jar 1.9.5

Project Modules

There are no modules declared in this project.

thundr-contrib-validation

JSR-303 bean validation support for thundr.

The hibernate-validation implementation is used under the covers which provides all the standard JSR annotations plus a few really handy ones such as @NotBlank.

Build Status

Usage

  1. Declare the dependency in your POM:
<dependency>
	<groupId>com.threewks.thundr</groupId>
	<artifactId>thundr-contrib-validation</artifactId>
	<version>1.0.0</version>
	<scope>compile</scope>
</dependency>
  1. Register the module with the dependency registry:
public class ApplicationModule extends BaseModule {

	@Override
	public void requires(DependencyRegistry dependencyRegistry) {
		super.requires(dependencyRegistry);
		dependencyRegistry.addDependency(ValidationModule.class);
		
		// ... other dependencies
	}
}
  1. Inject away!
public class MyController {
	private final Validator validator;
	
	public MyController(Validator validator) {
		this.validator = validator;
	}

	public JspView myView(MyModel model) {
		Set<ConstraintViolation<MyModel>> validationErrors = validator.validate(model);
		
		Map<String, Object> viewModel = new HashMap<>();
		viewModel.put("validationErrors", validationErrors);
		return new JspView("myview", viewModel);
	}
}

Bonus

I just love freebies, don't you?

You might have noticed that Set<ConstraintViolation<T>> doesn't serialise particularly well. That makes it pretty bad for doing AJAX validation. Don't worry though I've created this handy little class: ConstraintViolationSetToMapTransformer for just such an occasion.

You use it like so:

ConstraintViolationSetToMapTransformer<MyModel> toMap = new ConstraintViolationSetToMapTransformer<>();
Set<ConstraintViolation<MyModel>> validationErrors = validator.validate(model);
Map<String, Object> errorMap = toMap.from(validationErrors);

This produces a nice little graph of maps keyed by the field names of the beans with the values set to error messages. You can then check for errors in your JavaScript like this:

// POST some data to the server that will get validated
$.post('/validate', myFormData)
	.success(function(data) { // do something successy })
	.error(function(jqxhr) { 
		var errors = JSON.parse(jqxhr.responseText);
		
		if (errors.someProperty) {
			console.log("Validation error: " + error.someProperty);
		}
		
		if (errors.nestedObject && errors.nestedObject.nestedProperty) {
			console.log("Nested field validation error: " + errors.nestedObject.nestedProperty);
		}
	});

Versions

Version
1.0.0