Search in sources :

Example 1 with PathImpl

use of org.hibernate.validator.internal.engine.path.PathImpl in project hibernate-validator by hibernate.

the class ValidatorImpl method validateValue.

@Override
public final <T> Set<ConstraintViolation<T>> validateValue(Class<T> beanType, String propertyName, Object value, Class<?>... groups) {
    Contracts.assertNotNull(beanType, MESSAGES.beanTypeCannotBeNull());
    sanityCheckPropertyPath(propertyName);
    sanityCheckGroups(groups);
    BeanMetaData<T> rootBeanMetaData = beanMetaDataManager.getBeanMetaData(beanType);
    if (!rootBeanMetaData.hasConstraints()) {
        return Collections.emptySet();
    }
    PathImpl propertyPath = PathImpl.createPathFromString(propertyName);
    BaseBeanValidationContext<T> validationContext = getValidationContextBuilder().forValidateValue(beanType, rootBeanMetaData, propertyPath);
    ValidationOrder validationOrder = determineGroupValidationOrder(groups);
    return validateValueInContext(validationContext, value, propertyPath, validationOrder);
}
Also used : ValidationOrder(org.hibernate.validator.internal.engine.groups.ValidationOrder) PathImpl(org.hibernate.validator.internal.engine.path.PathImpl)

Example 2 with PathImpl

use of org.hibernate.validator.internal.engine.path.PathImpl in project hibernate-validator by hibernate.

the class ConstraintValidatorContextImplTest method createEmptyConstraintValidatorContextImpl.

private ConstraintValidatorContextImpl createEmptyConstraintValidatorContextImpl() {
    PathImpl path = PathImpl.createRootPath();
    path.addBeanNode();
    ConstraintValidatorContextImpl context = new ConstraintValidatorContextImpl(null, path, null, null, ExpressionLanguageFeatureLevel.BEAN_PROPERTIES, ExpressionLanguageFeatureLevel.NONE);
    context.disableDefaultConstraintViolation();
    return context;
}
Also used : ConstraintValidatorContextImpl(org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl) PathImpl(org.hibernate.validator.internal.engine.path.PathImpl)

Example 3 with PathImpl

use of org.hibernate.validator.internal.engine.path.PathImpl in project hibernate-validator by hibernate.

the class PathImplTest method testCreationOfExecutablePath.

@Test
public void testCreationOfExecutablePath() throws Exception {
    Method executable = Container.class.getMethod("addItem", Key.class, Item.class);
    BeanMetaDataManager beanMetaDataManager = new BeanMetaDataManagerImpl(getDummyConstraintCreationContext(), new ExecutableHelper(new TypeResolutionHelper()), new ExecutableParameterNameProvider(new DefaultParameterNameProvider()), new JavaBeanHelper(new DefaultGetterPropertySelectionStrategy(), new DefaultPropertyNodeNameProvider()), new DefaultBeanMetaDataClassNormalizer(), new ValidationOrderGenerator(), Collections.<MetaDataProvider>emptyList(), new MethodValidationConfiguration.Builder().build());
    ExecutableMetaData executableMetaData = beanMetaDataManager.getBeanMetaData(Container.class).getMetaDataFor(executable).get();
    PathImpl methodParameterPath = PathImpl.createPathForExecutable(executableMetaData);
    assertEquals(methodParameterPath.toString(), "addItem");
}
Also used : TypeResolutionHelper(org.hibernate.validator.internal.util.TypeResolutionHelper) ExecutableParameterNameProvider(org.hibernate.validator.internal.util.ExecutableParameterNameProvider) ValidationOrderGenerator(org.hibernate.validator.internal.engine.groups.ValidationOrderGenerator) Method(java.lang.reflect.Method) PathImpl(org.hibernate.validator.internal.engine.path.PathImpl) DefaultBeanMetaDataClassNormalizer(org.hibernate.validator.internal.metadata.DefaultBeanMetaDataClassNormalizer) DefaultGetterPropertySelectionStrategy(org.hibernate.validator.internal.properties.DefaultGetterPropertySelectionStrategy) BeanMetaDataManager(org.hibernate.validator.internal.metadata.BeanMetaDataManager) DefaultParameterNameProvider(org.hibernate.validator.internal.engine.DefaultParameterNameProvider) ExecutableHelper(org.hibernate.validator.internal.util.ExecutableHelper) JavaBeanHelper(org.hibernate.validator.internal.properties.javabean.JavaBeanHelper) BeanMetaDataManagerImpl(org.hibernate.validator.internal.metadata.BeanMetaDataManagerImpl) DefaultPropertyNodeNameProvider(org.hibernate.validator.internal.engine.DefaultPropertyNodeNameProvider) ExecutableMetaData(org.hibernate.validator.internal.metadata.aggregated.ExecutableMetaData) Test(org.testng.annotations.Test)

Example 4 with PathImpl

use of org.hibernate.validator.internal.engine.path.PathImpl in project hibernate-validator by hibernate.

the class PathImplTest method testParsingPropertyWithGermanCharacter.

@Test
public void testParsingPropertyWithGermanCharacter() {
    PathImpl path = PathImpl.createPathFromString("höchstBetrag");
    Iterator<Path.Node> it = path.iterator();
    assertEquals(it.next().getName(), "höchstBetrag");
}
Also used : PathImpl(org.hibernate.validator.internal.engine.path.PathImpl) Test(org.testng.annotations.Test)

Example 5 with PathImpl

use of org.hibernate.validator.internal.engine.path.PathImpl in project litemall by linlinjava.

the class GlobalExceptionHandler method badArgumentHandler.

@ExceptionHandler(ValidationException.class)
@ResponseBody
public Object badArgumentHandler(ValidationException e) {
    logger.error(e.getMessage(), e);
    if (e instanceof ConstraintViolationException) {
        ConstraintViolationException exs = (ConstraintViolationException) e;
        Set<ConstraintViolation<?>> violations = exs.getConstraintViolations();
        for (ConstraintViolation<?> item : violations) {
            String message = ((PathImpl) item.getPropertyPath()).getLeafNode().getName() + item.getMessage();
            return ResponseUtil.fail(402, message);
        }
    }
    return ResponseUtil.badArgumentValue();
}
Also used : ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) PathImpl(org.hibernate.validator.internal.engine.path.PathImpl) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

PathImpl (org.hibernate.validator.internal.engine.path.PathImpl)15 ConstraintViolation (javax.validation.ConstraintViolation)4 ConstraintViolationException (javax.validation.ConstraintViolationException)4 Test (org.testng.annotations.Test)4 LinkedHashSet (java.util.LinkedHashSet)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Path (javax.validation.Path)3 Validator (javax.validation.Validator)3 ValidationOrder (org.hibernate.validator.internal.engine.groups.ValidationOrder)2 MessageInterpolator (jakarta.validation.MessageInterpolator)1 NotNull (jakarta.validation.constraints.NotNull)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 DefaultParameterNameProvider (org.hibernate.validator.internal.engine.DefaultParameterNameProvider)1 DefaultPropertyNodeNameProvider (org.hibernate.validator.internal.engine.DefaultPropertyNodeNameProvider)1 MessageInterpolatorContext (org.hibernate.validator.internal.engine.MessageInterpolatorContext)1 ConstraintValidatorContextImpl (org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl)1 ValidationOrderGenerator (org.hibernate.validator.internal.engine.groups.ValidationOrderGenerator)1 BeanMetaDataManager (org.hibernate.validator.internal.metadata.BeanMetaDataManager)1