private static MinijaxConstraintDescriptor<NotEmpty> buildNotEmptyValidator(final NotEmpty notEmpty, final Class<?> valueClass) { if (valueClass.isArray()) { return new MinijaxConstraintDescriptor<>(notEmpty, NotEmptyValidatorForArray.INSTANCE); } if (CharSequence.class.isAssignableFrom(valueClass)) { return new MinijaxConstraintDescriptor<>(notEmpty, NotEmptyValidatorForCharSequence.INSTANCE); } if (Collection.class.isAssignableFrom(valueClass)) { return new MinijaxConstraintDescriptor<>(notEmpty, NotEmptyValidatorForCollection.INSTANCE); } if (Map.class.isAssignableFrom(valueClass)) { return new MinijaxConstraintDescriptor<>(notEmpty, NotEmptyValidatorForMap.INSTANCE); } throw new ValidationException("Unsupported type for @NotEmpty annotation: " + valueClass); }
/** * Check not valid parameter operation failed. */ @Test public void objectInvalid() { final SystemUser userDto = new SystemUser(); userDto.setLogin(""); try { validationInInterceptor.handleValidation(MESSAGE, INSTANCE, fromName("object"), Arrays.asList(userDto)); Assert.fail("Expected validation errors"); } catch (final ConstraintViolationException cve) { // Check all expected errors are there. final Set<ConstraintViolation<?>> constraintViolations = cve.getConstraintViolations(); Assert.assertNotNull(constraintViolations); Assert.assertEquals(1, constraintViolations.size()); // Check expected errors final ConstraintViolation<?> error1 = constraintViolations.iterator().next(); Assert.assertEquals(NotEmpty.class, error1.getConstraintDescriptor().getAnnotation().annotationType()); Assert.assertEquals("", error1.getInvalidValue()); Assert.assertEquals("login", error1.getPropertyPath().toString()); } }
@Test public void testConstraintViolationExceptionParameter() { final Wine bean = new Wine(); final Set<ConstraintViolation<?>> violations = new LinkedHashSet<>(); final ConstraintHelper helper = new ConstraintHelper(); final ConstraintDescriptor<NotEmpty> notEmptyNameDescriptor = new ConstraintDescriptorImpl<>(helper, (Member) null, getAnnotation("name", NotEmpty.class), ElementType.FIELD); PathImpl path = PathImpl.createPathFromString("name"); violations.add(ConstraintViolationImpl.<Wine> forParameterValidation("name-Empty", null, null, "interpolated", Wine.class, bean, new Object(), "value", path, notEmptyNameDescriptor, ElementType.PARAMETER, null, null)); path.addParameterNode("parameter1", 0); final ConstraintViolationException violationException = Mockito.mock(ConstraintViolationException.class); Mockito.when(violationException.getConstraintViolations()).thenReturn(violations); final ValidationJsonException validationJsonException = new ValidationJsonException(violationException); Assert.assertFalse(validationJsonException.getErrors().isEmpty()); Assert.assertEquals("{parameter1=[{rule=name-Empty}]}", validationJsonException.getErrors().toString()); }
@Test public void testConstraintViolationException() { final Wine bean = new Wine(); final Set<ConstraintViolation<?>> violations = new LinkedHashSet<>(); final ConstraintHelper helper = new ConstraintHelper(); final ConstraintDescriptor<NotEmpty> notEmptyNameDescriptor = new ConstraintDescriptorImpl<>(helper, (Member) null, getAnnotation("name", NotEmpty.class), ElementType.FIELD); final ConstraintDescriptor<NotEmpty> notEmptyGrapesDescriptor = new ConstraintDescriptorImpl<>(helper, (Member) null, getAnnotation("grapes", NotEmpty.class), ElementType.FIELD); final ConstraintDescriptor<Length> lengthNameDescriptor = new ConstraintDescriptorImpl<>(helper, (Member) null, getAnnotation("name", Length.class), ElementType.FIELD); violations.add(ConstraintViolationImpl.<Wine> forBeanValidation("name-Empty", null, null, "interpolated", Wine.class, bean, new Object(), "value", PathImpl.createPathFromString("name"), notEmptyNameDescriptor, ElementType.FIELD, null)); violations.add(ConstraintViolationImpl.<Wine> forBeanValidation("name-length", null, null, "interpolated", Wine.class, bean, new Object(), "value", PathImpl.createPathFromString("name"), lengthNameDescriptor, ElementType.FIELD, null)); violations.add(ConstraintViolationImpl.<Wine> forBeanValidation("grapes-Empty", null, null, "interpolated", Wine.class, bean, new Object(), "value", PathImpl.createPathFromString("grapes"), notEmptyGrapesDescriptor, ElementType.FIELD, null)); final ConstraintViolationException violationException = Mockito.mock(ConstraintViolationException.class); Mockito.when(violationException.getConstraintViolations()).thenReturn(violations); final ValidationJsonException validationJsonException = new ValidationJsonException(violationException); Assert.assertFalse(validationJsonException.getErrors().isEmpty()); Assert.assertEquals("{name=[{rule=name-Empty}, {rule=name-length, parameters={min=0, max=50}}], grapes=[{rule=grapes-Empty}]}", validationJsonException.getErrors().toString()); }
@SuppressWarnings("unchecked") public static <T extends Annotation> MinijaxConstraintDescriptor<T> build(final AnnotatedType annotatedType, final T annotation) { final Constraint constraint = annotation.annotationType().getAnnotation(Constraint.class); if (constraint == null) { return null; } final Class<?> valueClass = ReflectionUtils.getRawType(annotatedType); final Class<?> annotationClass = annotation.annotationType(); if (constraint.validatedBy().length > 0) { return buildDeclaredValidator(annotation, constraint.validatedBy()[0]); } else if (annotationClass == AssertFalse.class) { return (MinijaxConstraintDescriptor<T>) buildAssertFalseValidator((AssertFalse) annotation, valueClass); } else if (annotationClass == AssertTrue.class) { return (MinijaxConstraintDescriptor<T>) buildAssertTrueValidator((AssertTrue) annotation, valueClass); } else if (annotationClass == Max.class) { return (MinijaxConstraintDescriptor<T>) buildMaxValidator((Max) annotation, valueClass); } else if (annotationClass == Min.class) { return (MinijaxConstraintDescriptor<T>) buildMinValidator((Min) annotation, valueClass); } else if (annotationClass == NotBlank.class) { return (MinijaxConstraintDescriptor<T>) buildNotBlankValidator((NotBlank) annotation, valueClass); } else if (annotationClass == NotEmpty.class) { return (MinijaxConstraintDescriptor<T>) buildNotEmptyValidator((NotEmpty) annotation, valueClass); } else if (annotationClass == NotNull.class) { return (MinijaxConstraintDescriptor<T>) buildNotNullValidator((NotNull) annotation); } else if (annotationClass == Pattern.class) { return (MinijaxConstraintDescriptor<T>) buildPatternValidator((Pattern) annotation, valueClass); } else if (annotationClass == Size.class) { return (MinijaxConstraintDescriptor<T>) buildSizeValidator((Size) annotation, valueClass); } else { throw new ValidationException("Unrecognized constraint annotation: " + annotation); } }
/** * @param params Collection parameter with several constraints. */ public void jsr349Collection(final @NotEmpty @Size(max = 2) Collection<Object> params) { // }
protected void doCheckMismatchedValidatorAnnotation(Field field, Map<String, Class<?>> genericMap) { // recursive point pathDeque.push(field.getName()); checkedTypeSet.add(field.getDeclaringClass()); final Class<?> fieldType = deriveFieldType(field, genericMap); // *depends on JSON rule so difficult, check only physical mismatch here //if (isFormPropertyCannotNotNullType(fieldType)) { // final Class<NotNull> notNullType = NotNull.class; // if (field.getAnnotation(notNullType) != null) { // throwExecuteMethodFormPropertyValidationMismatchException(property, notNullType); // } //} if (isCannotNotEmptyType(fieldType)) { final Class<NotEmpty> notEmptyType = NotEmpty.class; if (field.getAnnotation(notEmptyType) != null) { throwExecuteMethodNotEmptyValidationMismatchException(field, notEmptyType); } } if (isCannotNotBlankType(fieldType)) { final Class<NotBlank> notBlankType = NotBlank.class; if (field.getAnnotation(notBlankType) != null) { throwExecuteMethodNotEmptyValidationMismatchException(field, notBlankType); } } if (isFormPropertyCannotRequiredPrimitiveType(fieldType)) { final Class<Required> requiredType = Required.class; if (field.getAnnotation(requiredType) != null) { throwExecuteMethodPrimitiveValidationMismatchException(field, requiredType); } final Class<NotNull> notNullType = NotNull.class; if (field.getAnnotation(notNullType) != null) { throwExecuteMethodPrimitiveValidationMismatchException(field, notNullType); } } if (Collection.class.isAssignableFrom(fieldType)) { // only collection, except array and map, simply doCheckGenericBeanValidationMismatch(field); } else if (mayBeNestedBeanType(fieldType)) { doCheckNestedValidationMismatch(fieldType); doCheckGenericBeanValidationMismatch(field); } pathDeque.pop(); }
@NotEmpty public abstract List<String> getSourceAttributes();
@NotEmpty public abstract List<String> getOntologyIds();
@NotEmpty public abstract String getOntologyTermIRI();
@NotEmpty public abstract List<String> getOntologyTermIRIs();
@Nullable // See #3897. If hashCode fails, the validation throws an exception @NotEmpty(message = "Please provide at least one entity in the entities property.") @Size(max = RestControllerV2.MAX_ENTITIES, message = "Number of entities cannot be more than {max}.") public abstract List<Map<String, Object>> getEntities();
@NotEmpty(message = "Please provide at least one entity in the entityIds property.") @Size(max = RestControllerV2.MAX_ENTITIES, message = "Number of entity identifiers cannot be more than {max}.") public abstract List<String> getEntityIds();
@NotEmpty @Email @Size(max = 128) public void setEmail(String email) { this.email = email; }
@NotEmpty @Email @Size(max = 128) String getEmail();