thundr-contrib-validation

JSR-303 bean validation support for Thundr

Лицензия

Лицензия

Группа

Группа

com.threewks.thundr
Идентификатор

Идентификатор

thundr-contrib-validation
Последняя версия

Последняя версия

1.0.0
Дата

Дата

Тип

Тип

jar
Описание

Описание

thundr-contrib-validation
JSR-303 bean validation support for Thundr
Ссылка на сайт

Ссылка на сайт

http://github.com/kuhnza/thundr-contrib-validation
Организация-разработчик

Организация-разработчик

3wks
Система контроля версий

Система контроля версий

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

Скачать thundr-contrib-validation

Как подключить последнюю версию

<!-- 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"]

Зависимости

compile (2)

Идентификатор библиотеки Тип Версия
com.threewks.thundr : thundr jar 1.1.2
org.hibernate : hibernate-validator jar 4.3.2.Final

provided (1)

Идентификатор библиотеки Тип Версия
javax.el : javax.el-api jar 2.2.4

test (3)

Идентификатор библиотеки Тип Версия
junit : junit jar 4.11
org.hamcrest : hamcrest-all jar 1.3
org.mockito : mockito-all jar 1.9.5

Модули Проекта

Данный проект не имеет модулей.

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);
		}
	});

Версии библиотеки

Версия
1.0.0