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;
    public Validator() {
        this(Validation.buildDefaultValidatorFactory());
    }
    public Validator(ValidatorFactory factory) {
        this. = factory;
    }

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

Parameters:
o a potentially-valid object
<T> the type of object to validate
Returns:
a list of error messages, if any, regarding o's validity
    public <T> ImmutableList<Stringvalidate(T o) {
        return validate(oDefault.class);
    }

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

Parameters:
o a potentially-valid object
groups group or list of groups targeted for validation (default to javax.validation.groups.Default)
<T> the type of object to validate
Returns:
a list of error messages, if any, regarding o's validity
    public <T> ImmutableList<Stringvalidate(T oClass<?>... groups) {
        final Set<Stringerrors = Sets.newHashSet();
        final Set<ConstraintViolation<T>> violations = .getValidator().validate(o,groups);
        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)",
                                  v.getPropertyPath(),
                                  v.getMessage(),
                                  v.getInvalidValue()));
            }
        }
        return ImmutableList.copyOf(Ordering.natural().sortedCopy(errors));
    }
New to GrepCode? Check out our FAQ X