use of jakarta.validation.metadata.ConstraintDescriptor in project wicket by apache.
the class PropertyValidator method onComponentTag.
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void onComponentTag(Component component, ComponentTag tag) {
super.onComponentTag(component, tag);
BeanValidationContext config = BeanValidationConfiguration.get();
Validator validator = config.getValidator();
Property property = getProperty();
// find any tag modifiers that apply to the constraints of the property
// being validated
// and allow them to modify the component tag
Iterator<ConstraintDescriptor<?>> it = new ConstraintIterator(validator, property, getGroups());
while (it.hasNext()) {
ConstraintDescriptor<?> desc = it.next();
ITagModifier modifier = config.getTagModifier(desc.getAnnotation().annotationType());
if (modifier != null) {
modifier.modify((FormComponent<?>) component, tag, desc.getAnnotation());
}
}
}
use of jakarta.validation.metadata.ConstraintDescriptor in project hibernate-orm by hibernate.
the class TypeSafeActivator method applySize.
private static void applySize(Property property, ConstraintDescriptor<?> descriptor, PropertyDescriptor propertyDescriptor) {
if (Size.class.equals(descriptor.getAnnotation().annotationType()) && String.class.equals(propertyDescriptor.getElementClass())) {
@SuppressWarnings("unchecked") ConstraintDescriptor<Size> sizeConstraint = (ConstraintDescriptor<Size>) descriptor;
int max = sizeConstraint.getAnnotation().max();
for (Column col : property.getColumns()) {
if (max < Integer.MAX_VALUE) {
col.setLength(max);
}
}
}
}
use of jakarta.validation.metadata.ConstraintDescriptor in project hibernate-orm by hibernate.
the class TypeSafeActivator method applyDigits.
private static void applyDigits(Property property, ConstraintDescriptor<?> descriptor) {
if (Digits.class.equals(descriptor.getAnnotation().annotationType())) {
@SuppressWarnings("unchecked") ConstraintDescriptor<Digits> digitsConstraint = (ConstraintDescriptor<Digits>) descriptor;
int integerDigits = digitsConstraint.getAnnotation().integer();
int fractionalDigits = digitsConstraint.getAnnotation().fraction();
for (Selectable selectable : property.getSelectables()) {
if (selectable instanceof Column) {
Column col = (Column) selectable;
col.setPrecision(integerDigits + fractionalDigits);
col.setScale(fractionalDigits);
}
}
}
}
use of jakarta.validation.metadata.ConstraintDescriptor in project hibernate-validator by hibernate.
the class MessageInterpolatorContextTest method testContextWithRightDescriptorAndValueAndRootBeanClassIsPassedToMessageInterpolator.
@Test
@TestForIssue(jiraKey = "HV-333")
public void testContextWithRightDescriptorAndValueAndRootBeanClassIsPassedToMessageInterpolator() {
// use a easy mock message interpolator for verifying that the right MessageInterpolatorContext
// will be passed
MessageInterpolator mock = createMock(MessageInterpolator.class);
Configuration<?> config = ValidatorUtil.getConfiguration().messageInterpolator(mock);
Validator validator = config.buildValidatorFactory().getValidator();
BeanDescriptor beanDescriptor = validator.getConstraintsForClass(TestBean.class);
PropertyDescriptor propertyDescriptor = beanDescriptor.getConstraintsForProperty("test");
Set<ConstraintDescriptor<?>> constraintDescriptors = propertyDescriptor.getConstraintDescriptors();
assertTrue(constraintDescriptors.size() == 1);
// prepare the mock interpolator to expect the right interpolate call
String validatedValue = "value";
expect(mock.interpolate(MESSAGE, new MessageInterpolatorContext(constraintDescriptors.iterator().next(), validatedValue, TestBean.class, null, Collections.<String, Object>emptyMap(), Collections.<String, Object>emptyMap(), ExpressionLanguageFeatureLevel.BEAN_METHODS, false))).andReturn("invalid");
replay(mock);
Set<ConstraintViolation<TestBean>> violations = validator.validate(new TestBean(validatedValue));
assertThat(violations).containsOnlyViolations(violationOf(Size.class).withMessage("invalid"));
// verify that the right context was passed
verify(mock);
}
use of jakarta.validation.metadata.ConstraintDescriptor in project hibernate-validator by hibernate.
the class ParameterTermResolverTest method createNotHibernateContextWithConstraintDescriptorAttr.
@SuppressWarnings({ "rawtypes", "unchecked" })
private static MessageInterpolator.Context createNotHibernateContextWithConstraintDescriptorAttr(String attrName, Object attrValue) {
final Map<String, Object> attrs = createConstraintDescriptorAttr(attrName, attrValue);
final MessageInterpolator.Context context = EasyMock.mock(MessageInterpolator.Context.class);
final ConstraintDescriptor constraintDescriptor = EasyMock.mock(ConstraintDescriptor.class);
EasyMock.expect(context.getConstraintDescriptor()).andStubReturn(constraintDescriptor);
EasyMock.expect(constraintDescriptor.getAttributes()).andStubReturn(attrs);
EasyMock.replay(context);
EasyMock.replay(constraintDescriptor);
return context;
}
Aggregations