@Test public void testAssertNull() { Set<ConstraintViolation<ObjectWithValidation>> violations = validator.validate(obj, Null.class); assertNotNull(violations); assertEquals(violations.size(), 1); if (runPeformance) { long time = System.currentTimeMillis(); for (int index = 0; index < 10000; index++) { validator.validate(obj, Null.class); } long used = System.currentTimeMillis() - time; System.out.println("Hibernate Validator [Null] check used " + used + "ms, avg. " + ((double) used) / 10000 + "ms."); } }
/** * Take a stab at fixing validation problems ? * * @param object */ private void validate(Object object) { Set<ConstraintViolation<Object>> viols = validator.validate(object); for (ConstraintViolation<Object> constraintViolation : viols) { if (Null.class.isAssignableFrom(constraintViolation.getConstraintDescriptor().getAnnotation().getClass())) { Object o = constraintViolation.getLeafBean(); Iterator<Node> iterator = constraintViolation.getPropertyPath().iterator(); String propertyName = null; while (iterator.hasNext()) { propertyName = iterator.next().getName(); } if (propertyName != null) { try { PropertyDescriptor descriptor = BeanUtils.getPropertyDescriptor(o.getClass(), propertyName); descriptor.getWriteMethod().invoke(o, new Object[] { null }); } catch (Exception e) { e.printStackTrace(); } } } } }
/** * @return The list of constraints for fields that reference other beans. */ private List<MetaAnnotation> buildRefFieldAnnotations() { List<MetaAnnotation> anns = Lists.newArrayList(); HashMap<String, Object> annotParams; annotParams = Maps.newHashMap(); anns.add(new MetaAnnotation(codeModel, NotNull.class, AnnotationType.JSR_303, annotParams)); annotParams = Maps.newHashMap(); anns.add(new MetaAnnotation(codeModel, Null.class, AnnotationType.JSR_303, annotParams)); return anns; }
@Override public Object process(AnnotationInfo ctx, Object value) throws Exception { if (!ctx.isAnnotationPresent(Null.class)) { return value; } return null; }
public MessageList findMessages(@Null @Email String recipientEmail, Pageable pageRequest) { List<MessageEntity> entities; if (StringUtils.isBlank(recipientEmail)) { entities = messageRepository.findAllByOrderByReceivedDateDesc(pageRequest); } else { entities = messageRepository.findAllForRecipientOrderByReceivedDateDesc(recipientEmail, pageRequest); } return messageListMapper.toMessageList(entities); }
/** * @param column Name of column value to be negated. */ public NegateExpression(@Null String column, String alias) { super(column, alias); if (this.alias == null) { this.alias = "NEGATE(" + column + ")"; } }
@Null(message = "ValidationViolation must be null. ValidationMessage: ${validatedValue}") public String getValidationViolations() { List<StockTransactionStatus> sortedList = new ArrayList<>(getStatusHistory()); Collections.sort(sortedList); List<StockTransactionStatusType> sortedTypeList = new ArrayList<>(); for (StockTransactionStatus stockTransactionStatus : sortedList) { sortedTypeList.add(stockTransactionStatus.getType()); } if ( POSSIBLE_STATUS_TYPES.contains(sortedTypeList) ) return null; return "TransactionStatusHistory is in invalid. Statuses=" + sortedList + " AllowedTypes=" + POSSIBLE_STATUS_TYPES; }
/** * Returns null if the instance is valid, or a string representing the error. * <p> * @return null if the instance is valid, or a string representing the error. */ @Null(message = "ViolationMessage is not null, but '${validatedValue}'") public String getViolationMessage() { if ( tradeName == null ) return null; if ( !tradeName.isBrand() ) return tradeName + " is not a Brand"; if ( tradeName.getManufacturer().getPartNoSupport() == null ) return null; // No Support, so everything is ok. return tradeName.getManufacturer().getPartNoSupport().violationMessages(partNo); }
/** * Returns null if the instance is valid, or a string representing the error. * <p> * @return null if the instance is valid, or a string representing the error. */ @Null(message = "ViolationMessage is not null, but '${validatedValue}'") public String getViolationMessage() { if ( model == null || model.getFamily() == null || model.getFamily().getSeries() == null || model.getFamily().getSeries().getBrand() == null || model.getFamily().getSeries().getBrand().getManufacturer().getPartNoSupport() == null ) return null; return model.getFamily().getSeries().getBrand().getManufacturer().getPartNoSupport().violationMessages(partNo); }
private static void mapBeanValidationParameter(Annotation annotation, InstanceDescriptor element) { SimpleTypeDescriptor typeDescriptor = (SimpleTypeDescriptor) element.getLocalType(false); if (annotation instanceof AssertFalse) typeDescriptor.setTrueQuota(0.); else if (annotation instanceof AssertTrue) typeDescriptor.setTrueQuota(1.); else if (annotation instanceof DecimalMax) typeDescriptor.setMax(String.valueOf(DescriptorUtil.convertType(((DecimalMax) annotation).value(), typeDescriptor))); else if (annotation instanceof DecimalMin) typeDescriptor.setMin(String.valueOf(DescriptorUtil.convertType(((DecimalMin) annotation).value(), typeDescriptor))); else if (annotation instanceof Digits) { Digits digits = (Digits) annotation; typeDescriptor.setGranularity(String.valueOf(Math.pow(10, - digits.fraction()))); } else if (annotation instanceof Future) typeDescriptor.setMin(new SimpleDateFormat("yyyy-MM-dd").format(TimeUtil.tomorrow())); else if (annotation instanceof Max) typeDescriptor.setMax(String.valueOf(((Max) annotation).value())); else if (annotation instanceof Min) typeDescriptor.setMin(String.valueOf(((Min) annotation).value())); else if (annotation instanceof NotNull) { element.setNullable(false); element.setNullQuota(0.); } else if (annotation instanceof Null) { element.setNullable(true); element.setNullQuota(1.); } else if (annotation instanceof Past) typeDescriptor.setMax(new SimpleDateFormat("yyyy-MM-dd").format(TimeUtil.yesterday())); else if (annotation instanceof Pattern) typeDescriptor.setPattern(String.valueOf(((Pattern) annotation).regexp())); else if (annotation instanceof Size) { Size size = (Size) annotation; typeDescriptor.setMinLength(size.min()); typeDescriptor.setMaxLength(size.max()); } }
public static void checkNotEqual(@Null Object type, @NotNull Object target, ErrorCode errorCode) { if (!target.equals(type)) { throw new SedException(errorCode); } }
@Override public boolean incudeInValidation(Null nullAnnotation, RequestHandler requestHandler, ValidationContext validationCtx) { return true; }
@Override public void validate(Null nullAnnotation, String name, ValidationContext validationCtx, Errors e) { if (validationCtx.value(name) != null) e.add(name, nullAnnotation.message()); }
@Null(groups = { Create.class, Update.class }) public String getId() { return id; }
@Null(groups = { Create.class, Update.class }) public String getName() { return name; }
@Override public void initialize(Null constraintAnnotation) { }
/** * Instance Validation. Validates: value brand is a brand. * <p> * @return null if valid, or a error message */ @Null(message = "ViolationMessage is not null, but '${validatedValue}'") public String getViolationMessage() { if ( brand != null && !brand.isBrand() ) return brand.getName() + " is not a Brand"; return null; }
@Null(message = "ViolationMessage is not null, but '${validatedValue}'") public String getValidationViolations() { if ( resolution.ordinal() > size.getMaxResolution().ordinal() ) return "resolution > size.maxResolution"; return null; }
private void appendNullValidator(SourceWriter w, JField field) { Null nullAnnotation = field.getAnnotation(Null.class); if (nullAnnotation != null) { w.println(", new NullValidator(\"%s\")", nullAnnotation.message()); } }
@Override public void setSlotContents(final int slot, @Null final ItemStack itemStack) { this.slots[slot] = itemStack; }
@Null(groups = {PrePersist.class}) @NotNull(groups = {PostPersist.class}) @Nullable public Date getCreated() { return created; }
@Null(groups = {PrePersist.class}) @NotNull(groups = {PostPersist.class}) @Nullable public Date getModified() { return modified; }
@Null(groups = {PrePersist.class}) @NotNull(groups = {PostPersist.class}) public Integer getKey() { return key; }
@Null(groups = {PrePersist.class}) @NotNull(groups = {PostPersist.class}) @Min(1) public Integer getKey() { return key; }
@Null(groups = {PrePersist.class}) @NotNull(groups = {PostPersist.class}) public Date getCreated() { return created; }
@Null(groups = PrePersist.class) @NotNull(groups = PostPersist.class) @Min(1) public Integer getKey() { return key; }
@Null(groups = PrePersist.class) @NotNull(groups = PostPersist.class) public Date getCreated() { return created; }
@Null(groups = PrePersist.class) @NotNull(groups = PostPersist.class) public Date getModified() { return modified; }
@Null(groups = {PrePersist.class}) @NotNull(groups = {PostPersist.class}) public Date getModified() { return modified; }
@Nullable @Null(groups = {PrePersist.class}) @NotNull(groups = {PostPersist.class}) UUID getKey();
@Null(groups = {PrePersist.class}) @NotNull(groups = {PostPersist.class}) @Nullable Date getCreated();
@Null(groups = {PrePersist.class}) @NotNull(groups = {PostPersist.class}) @Nullable Date getModified();
@Null(groups = {PrePersist.class}) @NotNull(groups = {PostPersist.class}) @Override public UUID getKey() { return key; }
@Null(groups = {PrePersist.class}) @NotNull(groups = {PostPersist.class}) @Override public Date getCreated() { return created; }
@Null(groups = {PrePersist.class}) @NotNull(groups = {PostPersist.class}) @Override public Date getModified() { return modified; }