@Override public boolean isValid(String value, ConstraintValidatorContext context) { if (value == null) { return true; } final boolean valid = Arrays.asList(strings).contains(value); if (!valid) { HibernateConstraintValidatorContext hibernateContext = context.unwrap(HibernateConstraintValidatorContext.class); hibernateContext.disableDefaultConstraintViolation(); hibernateContext.addExpressionVariable("validValues", Joiner.on(" ").join(strings)) .buildConstraintViolationWithTemplate(hibernateContext.getDefaultConstraintMessageTemplate()) .addConstraintViolation(); } return valid; }
public boolean isValidInSession(Object value, ConstraintValidatorContext context){ if(value == null){ return true; } TreeMap<String, Object> fieldMap = _countRows(value); if(fieldMap != null){ String message = _messageSource.getMessage(context.getDefaultConstraintMessageTemplate(), null, _defaultMesssage, LocaleContextHolder.getLocale()); Map.Entry<String, Object> field = fieldMap.entrySet().iterator().next(); context.unwrap(HibernateConstraintValidatorContext.class) .addExpressionVariable("name", value.getClass().getSimpleName()) .addExpressionVariable("fullName", value.getClass().getName()) .addExpressionVariable("field", field.getKey()) .addExpressionVariable("value", field.getValue()) .addExpressionVariable("allFields", StringUtils.join(fieldMap.keySet(), ", ")) .addExpressionVariable("values", StringUtils.join(fieldMap.values(), ", ")) .buildConstraintViolationWithTemplate(message) .addPropertyNode(field.getKey()) .addConstraintViolation() .disableDefaultConstraintViolation(); return false; } return true; }
@Override public boolean isValid(final CharSequence value, final ConstraintValidatorContext constraintValidatorContext) { if (value == null || this.pattern == null) { return true; } if (constraintValidatorContext instanceof HibernateConstraintValidatorContext) { constraintValidatorContext.unwrap(HibernateConstraintValidatorContext.class) .addMessageParameter("regexp", this.escapedRegexp); } final MatchResult match = this.pattern.exec(value.toString()); if (match == null) { return false; } // Must match the entire string return match.getGroup(0).length() == value.length(); }
@Override public boolean isValid(final WithId<?> value, final ConstraintValidatorContext context) { if (value == null) { return true; } final PropertyAccessor bean = new BeanWrapperImpl(value); final String propertyValue = String.valueOf(bean.getPropertyValue(property)); @SuppressWarnings({"rawtypes", "unchecked"}) final Class<WithId> modelClass = (Class) value.getKind().modelClass; @SuppressWarnings("unchecked") final Set<String> ids = dataManager.fetchIdsByPropertyValue(modelClass, property, propertyValue); final boolean isUnique = ids.isEmpty() || value.getId().map(id -> ids.contains(id)).orElse(false); if (!isUnique) { if (ids.stream().allMatch(id -> consideredValidByException(modelClass, id))) { return true; } context.disableDefaultConstraintViolation(); context.unwrap(HibernateConstraintValidatorContext.class).addExpressionVariable("nonUnique", propertyValue) .buildConstraintViolationWithTemplate(context.getDefaultConstraintMessageTemplate()) .addPropertyNode(property).addConstraintViolation(); } return isUnique; }
@Test public void shouldAscertainPropertyUniqueness() { final HibernateConstraintValidatorContext context = mock(HibernateConstraintValidatorContext.class); when(context.unwrap(HibernateConstraintValidatorContext.class)).thenReturn(context); when(context.addExpressionVariable(eq("nonUnique"), anyString())).thenReturn(context); when(context.getDefaultConstraintMessageTemplate()).thenReturn("template"); final ConstraintViolationBuilder builder = mock(ConstraintViolationBuilder.class); when(context.buildConstraintViolationWithTemplate("template")).thenReturn(builder); when(builder.addPropertyNode(anyString())).thenReturn(mock(NodeBuilderCustomizableContext.class)); assertThat(validator.isValid(connection, context)).isEqualTo(validity); }
@Override public boolean isValid(LocalDate value, ConstraintValidatorContext context) { // null values are valid if (value == null) { return true; } TimeProvider timeProvider = context.unwrap(HibernateConstraintValidatorContext.class) .getTimeProvider(); long now = timeProvider.getCurrentTime(); final LocalDate localDate = Instant.ofEpochMilli(now) .atZone(ZoneId.systemDefault()) .toLocalDate(); return localDate .isBefore(value) || localDate.isEqual(value); }
@Override public boolean isValid(LocalDate value, ConstraintValidatorContext context) { // null values are valid if (value == null) { return true; } TimeProvider timeProvider = context.unwrap(HibernateConstraintValidatorContext.class) .getTimeProvider(); long now = timeProvider.getCurrentTime(); return Instant.ofEpochMilli(now) .atZone(ZoneId.systemDefault()) .toLocalDate() .isBefore(value); }
@Override public boolean isValid(LocalDateTime value, ConstraintValidatorContext constraintValidatorContext) { if(value ==null){ return true; } TimeProvider timeProvider=constraintValidatorContext.unwrap(HibernateConstraintValidatorContext.class).getTimeProvider(); long now=timeProvider.getCurrentTime(); final LocalDateTime localDateTime = Instant.ofEpochMilli(now) .atZone(ZoneId.systemDefault()).toLocalDateTime(); return localDateTime.isBefore(value) || localDateTime.isEqual(value); }
@Override public boolean isValid(LocalDateTime value, ConstraintValidatorContext constraintValidatorContext) { if (value == null) { return true; } TimeProvider timeProvider = constraintValidatorContext.unwrap(HibernateConstraintValidatorContext.class).getTimeProvider(); long now = timeProvider.getCurrentTime(); final LocalDate localDate = Instant.ofEpochMilli(now) .atZone(ZoneId.systemDefault()) .toLocalDate(); final LocalDateTime localDateTime = LocalDateTime.of(localDate, LocalTime.MIN); return localDateTime.isBefore(value) || localDateTime.isEqual(value); }
@Override public boolean isValid(LocalDateTime value, ConstraintValidatorContext constraintValidatorContext) { if (value == null) { return true; } TimeProvider timeProvider = constraintValidatorContext.unwrap(HibernateConstraintValidatorContext.class).getTimeProvider(); long now = timeProvider.getCurrentTime(); return Instant.ofEpochMilli(now).atZone(ZoneId.systemDefault()).toLocalDateTime().isBefore(value); }
@Override public boolean isValid(LocalDate value, ConstraintValidatorContext context) { // null values are valid if (value == null) { return true; } TimeProvider timeProvider = context.unwrap(HibernateConstraintValidatorContext.class) .getTimeProvider(); long now = timeProvider.getCurrentTime(); final LocalDate localDate = Instant.ofEpochMilli(now) .atZone(ZoneId.systemDefault()) .toLocalDate(); return localDate .isAfter(value) || localDate.isEqual(value); }
@Override public boolean isValid(LocalDateTime value, ConstraintValidatorContext constraintValidatorContext) { if (value == null) { return true; } TimeProvider timeProvider = constraintValidatorContext.unwrap(HibernateConstraintValidatorContext.class).getTimeProvider(); long now = timeProvider.getCurrentTime(); final LocalDateTime localDateTime = Instant.ofEpochMilli(now).atZone(ZoneId.systemDefault()).toLocalDateTime(); return localDateTime.isAfter(value) || localDateTime.isEqual(value); }
@Override public boolean isValid(LocalDate value, ConstraintValidatorContext context) { // null values are valid if (value == null) { return true; } TimeProvider timeProvider = context.unwrap(HibernateConstraintValidatorContext.class) .getTimeProvider(); long now = timeProvider.getCurrentTime(); return Instant.ofEpochMilli(now) .atZone(ZoneId.systemDefault()) .toLocalDate() .isAfter(value); }
@SuppressWarnings("unchecked") protected void assertNoLat(ConstraintValidatorContext context) { ConstraintViolationCreationContext x = ((ConstraintValidatorContextImpl) context.unwrap(HibernateConstraintValidatorContext.class)).getConstraintViolationCreationContexts().get(0); Map<String, Object> ev = x.getExpressionVariables(); assertEquals(2, ev.size()); Set<String> contains = (Set<String>) ev.get("containsSet"); assertThat(contains, containsInAnyOrder("long", "within")); Set<String> missing = (Set<String>) ev.get("missingSet"); assertThat(missing, containsInAnyOrder("lat")); assertEquals("{message}", x.getMessage()); }
@SuppressWarnings("unchecked") protected void assertNoLong(ConstraintValidatorContext context) { ConstraintViolationCreationContext x = ((ConstraintValidatorContextImpl) context.unwrap(HibernateConstraintValidatorContext.class)).getConstraintViolationCreationContexts().get(0); Map<String, Object> ev = x.getExpressionVariables(); assertEquals(2, ev.size()); Set<String> contains = (Set<String>) ev.get("containsSet"); assertThat(contains, containsInAnyOrder("lat", "within")); Set<String> missing = (Set<String>) ev.get("missingSet"); assertThat(missing, containsInAnyOrder("long")); assertEquals("{message}", x.getMessage()); }
@SuppressWarnings("unchecked") protected void assertNoWithin(ConstraintValidatorContext context) { ConstraintViolationCreationContext x = ((ConstraintValidatorContextImpl) context.unwrap(HibernateConstraintValidatorContext.class)).getConstraintViolationCreationContexts().get(0); Map<String, Object> ev = x.getExpressionVariables(); assertEquals(2, ev.size()); Set<String> contains = (Set<String>) ev.get("containsSet"); assertThat(contains, containsInAnyOrder("lat", "long")); Set<String> missing = (Set<String>) ev.get("missingSet"); assertThat(missing, containsInAnyOrder("within")); assertEquals("{message}", x.getMessage()); }
private void setMessageTemplate(ConstraintValidatorContext context, String messageTemplate, String value) { HibernateConstraintValidatorContext hibernateContext = context.unwrap(HibernateConstraintValidatorContext.class); hibernateContext.disableDefaultConstraintViolation(); hibernateContext.addExpressionVariable("value", value).buildConstraintViolationWithTemplate(messageTemplate).addConstraintViolation(); }
@Override public boolean isValid(LocalDateTime value, ConstraintValidatorContext constraintValidatorContext) { TimeProvider timeProvider= constraintValidatorContext.unwrap(HibernateConstraintValidatorContext.class).getTimeProvider(); long now=timeProvider.getCurrentTime(); return Instant.ofEpochMilli(now).atZone(ZoneId.systemDefault()).toLocalDateTime().isAfter(value); }
@Override public boolean isValid(FilterParameters value, ConstraintValidatorContext context) { if (value == null) { return true; } SortedSet<String> containsSet = new TreeSet<String>(); SortedSet<String> missingSet = new TreeSet<String>(); if (StringUtils.isBlank(value.getLat())) { missingSet.add(Parameters.LATITUDE.toString()); } else { containsSet.add(Parameters.LATITUDE.toString()); } if (StringUtils.isBlank(value.getLong())) { missingSet.add(Parameters.LONGITUDE.toString()); } else { containsSet.add(Parameters.LONGITUDE.toString()); } if (StringUtils.isBlank(value.getWithin())) { missingSet.add(Parameters.WITHIN.toString()); } else { containsSet.add(Parameters.WITHIN.toString()); } if (containsSet.size() > 0 && missingSet.size() > 0) { HibernateConstraintValidatorContext hibernateContext = context.unwrap(HibernateConstraintValidatorContext.class); hibernateContext.disableDefaultConstraintViolation(); hibernateContext .addExpressionVariable("containsSet", containsSet) .addExpressionVariable("missingSet", missingSet) .buildConstraintViolationWithTemplate("{message}") .addConstraintViolation(); return false; } else { return true; } }