use of jakarta.validation.constraints.Size in project hibernate-validator by hibernate.
the class SizeValidatorTest method getValidatorMin1Max2.
private <T> ConstraintValidator<Size, T> getValidatorMin1Max2(Class<?> validatorClass) throws Exception {
ConstraintAnnotationDescriptor.Builder<Size> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(Size.class);
descriptorBuilder.setAttribute("min", 1);
descriptorBuilder.setAttribute("max", 2);
descriptorBuilder.setMessage("{validator.max}");
Size m = descriptorBuilder.build().getAnnotation();
@SuppressWarnings("unchecked") ConstraintValidator<Size, T> validator = (ConstraintValidator<Size, T>) validatorClass.getConstructor().newInstance();
validator.initialize(m);
return validator;
}
use of jakarta.validation.constraints.Size in project hibernate-validator by hibernate.
the class ProgrammaticContainerElementConstraintsForParameterTest method canDeclareContainerElementConstraintsForListContainingArrayTypeParameterProgrammatically.
// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForListContainingArrayTypeParameterProgrammatically() {
ConstraintMapping newMapping = config.createConstraintMapping();
newMapping.type(IFishTank.class).method("test6", List.class).parameter(0).containerElementType(0, 0).constraint(new SizeDef().max(5));
config.addMapping(newMapping);
Validator validator = config.buildValidatorFactory().getValidator();
IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
try {
List<String[]> fishNamesByMonth = new ArrayList<>();
fishNamesByMonth.add(new String[] { "Too Long" });
fishTank.test6(fishNamesByMonth);
fail("Expected exception wasn't raised");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"));
}
}
use of jakarta.validation.constraints.Size in project hibernate-validator by hibernate.
the class ProgrammaticContainerElementConstraintsForParameterTest method canDeclareContainerElementConstraintsForMultiDimensionalArrayTypeParameterProgrammatically.
// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForMultiDimensionalArrayTypeParameterProgrammatically() {
ConstraintMapping newMapping = config.createConstraintMapping();
newMapping.type(IFishTank.class).method("test7", String[][].class).parameter(0).containerElementType(0, 0).constraint(new SizeDef().max(5));
config.addMapping(newMapping);
Validator validator = config.buildValidatorFactory().getValidator();
IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
try {
String[][] fishNamesByMonthAsArray = new String[][] { new String[] { "Too Long" } };
fishTank.test7(fishNamesByMonthAsArray);
fail("Expected exception wasn't raised");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"));
}
}
use of jakarta.validation.constraints.Size in project hibernate-validator by hibernate.
the class ProgrammaticContainerElementConstraintsForParameterTest method canDeclareContainerElementConstraintsForArrayTypedParameterProgrammatically.
// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForArrayTypedParameterProgrammatically() {
ConstraintMapping newMapping = config.createConstraintMapping();
newMapping.type(IFishTank.class).method("test5", String[].class).parameter(0).containerElementType().constraint(new SizeDef().max(5));
config.addMapping(newMapping);
Validator validator = config.buildValidatorFactory().getValidator();
IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
try {
fishTank.test5(new String[] { "Too Long" });
fail("Expected exception wasn't raised");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"));
}
}
use of jakarta.validation.constraints.Size in project hibernate-validator by hibernate.
the class ProgrammaticContainerElementConstraintsForReturnValueTest method canDeclareContainerElementConstraintsForListContainingArrayTypeReturnValueProgrammatically.
// HV-1428 Container element support is disabled for arrays
@Test(expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "HV000226:.*")
@TestForIssue(jiraKey = "HV-1239")
public void canDeclareContainerElementConstraintsForListContainingArrayTypeReturnValueProgrammatically() {
ConstraintMapping newMapping = config.createConstraintMapping();
newMapping.type(IFishTank.class).method("test6").returnValue().containerElementType(0, 0).constraint(new SizeDef().max(5));
config.addMapping(newMapping);
Validator validator = config.buildValidatorFactory().getValidator();
IFishTank fishTank = ValidatorUtil.getValidatingProxy(new FishTank(), validator);
try {
fishTank.test6();
fail("Expected exception wasn't raised");
} catch (ConstraintViolationException e) {
assertThat(e.getConstraintViolations()).containsOnlyViolations(violationOf(Size.class).withMessage("size must be between 0 and 5"));
}
}
Aggregations