Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.yammer.dropwizard.validation;
import static java.lang.String.format;

A simple façade for Hibernate Validator.
public class Validator {
    private final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();

Validates the given object, and returns a list of error messages, if any. If the returned list is empty, the object is valid.

o a potentially-valid object
<T> the type of object to validate
a list of error messages, if any, regarding o's validity
    public <T> ImmutableList<Stringvalidate(T o) {
        final Set<Stringerrors = Sets.newHashSet();
        final Set<ConstraintViolation<T>> violations = .getValidator().validate(o);
        for (ConstraintViolation<T> v : violations) {
            if (v.getConstraintDescriptor().getAnnotation() instanceof ValidationMethod) {
                final ImmutableList<Path.Nodenodes = ImmutableList.copyOf(v.getPropertyPath());
                final ImmutableList<Path.NodeusefulNodes = nodes.subList(0, nodes.size() - 1);
                final String msg = v.getMessage().startsWith(".") ? "%s%s" : "%s %s";
                errors.add(format(msg, Joiner.on('.').join(usefulNodes), v.getMessage()).trim());
            } else {
                errors.add(format("%s %s (was %s)",
        return ImmutableList.copyOf(Ordering.natural().sortedCopy(errors));
New to GrepCode? Check out our FAQ X