@Override public boolean isValid(PageRangeSelection value, ConstraintValidatorContext context) { if (value != null) { List<PageRange> ranges = new ArrayList<PageRange>(value.getPageSelection()); for (int i = 0; i < ranges.size(); i++) { PageRange range = ranges.get(i); for (int j = i + 1; j < ranges.size(); j++) { PageRange current = ranges.get(j); if (range.intersects(current)) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("Invalid page ranges, found an intersection between %s and %s", range, current)).addNode("page ranges").addConstraintViolation(); return false; } } } } return true; }
@Override public boolean isValid(PageRangeSelection value, ConstraintValidatorContext context) { if (value != null) { List<PageRange> ranges = new ArrayList<PageRange>(value.getPageSelection()); for (int i = 0; i < ranges.size(); i++) { PageRange range = ranges.get(i); for (int j = i + 1; j < ranges.size(); j++) { PageRange current = ranges.get(j); if (range.intersects(current)) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("Invalid page ranges, found an intersection between %s and %s", range, current)).addNode("page ranges").addConstraintViolation(); return false; } } } } return true; }
public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) { if ( disableDefaultError ) { constraintValidatorContext.disableDefaultConstraintViolation(); } if ( errorMessages != null ) { for ( Map.Entry<String, String> entry : errorMessages.entrySet() ) { if ( entry.getKey() == null ) { constraintValidatorContext.buildConstraintViolationWithTemplate( entry.getValue() ).addConstraintViolation(); } else { constraintValidatorContext.buildConstraintViolationWithTemplate( entry.getValue() ) .addNode( entry.getKey() ) .addConstraintViolation(); } } } return false; }
@SuppressWarnings("deprecation") @Override public boolean isValid(Group group, ConstraintValidatorContext constraintValidatorContext) { if ( group == null ) { return true; } constraintValidatorContext.disableDefaultConstraintViolation(); for ( int index = 0; index < group.persons.size(); index++ ) { Person person = group.persons.get( index ); if ( !group.gender.equals( person.gender ) ) { constraintValidatorContext .buildConstraintViolationWithTemplate( "constraints.CompatiblePersons.gender.message" ) .addNode( "persons" ) .addNode( null ).inIterable().atIndex( index ) .addConstraintViolation(); return false; } } return true; } }
@Override public boolean isValid(java.lang.Object[] value, ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( context.getDefaultConstraintMessageTemplate() ) .addParameterNode( 0 ) .addConstraintViolation(); context.buildConstraintViolationWithTemplate( context.getDefaultConstraintMessageTemplate() ) .addParameterNode( 0 ) .addBeanNode() .addConstraintViolation(); context.buildConstraintViolationWithTemplate( context.getDefaultConstraintMessageTemplate() ) .addParameterNode( 0 ) .addPropertyNode( "myNode1" ) .inIterable().atIndex( 23 ) .addBeanNode() .addConstraintViolation(); return false; } }
@Override @SuppressWarnings("deprecation") public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) { if ( disableDefaultError ) { constraintValidatorContext.disableDefaultConstraintViolation(); } if ( errorMessages != null ) { for ( Map.Entry<String, String> entry : errorMessages.entrySet() ) { if ( entry.getKey() == null ) { constraintValidatorContext.buildConstraintViolationWithTemplate( entry.getValue() ) .addConstraintViolation(); } else { constraintValidatorContext.buildConstraintViolationWithTemplate( entry.getValue() ) .addNode( entry.getKey() ) .addConstraintViolation(); } } } return false; }
public boolean isValid(Group group, ConstraintValidatorContext constraintValidatorContext) { if ( group == null ) { return true; } constraintValidatorContext.disableDefaultConstraintViolation(); for ( int index = 0; index < group.persons.size(); index++ ) { Person person = group.persons.get( index ); if ( !group.gender.equals( person.gender ) ) { constraintValidatorContext .buildConstraintViolationWithTemplate( "constraints.CompatiblePersons.gender.message" ) .addNode( "persons" ) .addNode( null ).inIterable().atIndex( index ) .addConstraintViolation(); return false; } } return true; } }
@Override public boolean isValid(SingleOutputTaskParameters value, ConstraintValidatorContext context) { if (nonNull(value) && nonNull(value.getOutput()) && ArrayUtils.isNotEmpty(extensions)) { String fileName = value.getOutput().getDestination().getName(); if (hasAllowedExtension(fileName)) { return true; } context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("The output '%s' is not one of the expected types: %s", fileName, ArrayUtils.toString(extensions))).addNode("taskOutput").addConstraintViolation(); return false; } return true; }
@Override public boolean isValid(SingleOutputTaskParameters value, ConstraintValidatorContext context) { if (nonNull(value) && nonNull(value.getOutput()) && ArrayUtils.isNotEmpty(extensions)) { String fileName = value.getOutput().getDestination().getName(); if (hasAllowedExtension(fileName)) { return true; } context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("The output '%s' is not one of the expected types: %s", fileName, ArrayUtils.toString(extensions))).addNode("taskOutput").addConstraintViolation(); return false; } return true; }
/** * We must show error for the title of validated poll, so * we must change the node name in error message to do it. * * @param context to add error message for the title fields */ private void addConstraintViolatedErrorMessage(ConstraintValidatorContext context) { String message = context.getDefaultConstraintMessageTemplate(); context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate(message) .addNode(titleFieldName) .addConstraintViolation(); } }
@SuppressWarnings("deprecation") @Override public boolean isValid(MyObject value, ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( context.getDefaultConstraintMessageTemplate() ) .addNode( "myNode1" ) .addConstraintViolation(); context.buildConstraintViolationWithTemplate( context.getDefaultConstraintMessageTemplate() ) .addNode( "myNode2" ) .addNode( "myNode3" ).inIterable().atKey( "key" ) .addConstraintViolation(); return false; } }
/** * We must show error for the list of items of validated poll, so * we must change the node name in error message to do it. * * @param context to add error message for the title fields */ private void addConstraintViolatedErrorMessage(ConstraintValidatorContext context) { String message = context.getDefaultConstraintMessageTemplate(); context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate(message) .addNode(pollItemsFieldName) .addConstraintViolation(); } }
/** * Put constraint violation into context. * * @param context validator context */ private void constraintViolated(ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate(failMessage) .addNode(secondPropertyName) .addConstraintViolation(); }
private void runVerify(String testMask, String errorMessage) { assertFalse(underTest.isValid(testMask, contextMock)); verify(contextMock).disableDefaultConstraintViolation(); verify(contextMock).buildConstraintViolationWithTemplate(errorMessage); verify(mockConstraintViolationBuilder).addNode("mask"); verify(mockNodeBuilderDefinedContext).addConstraintViolation(); }
private boolean failWith(ConstraintValidatorContext context, String message, String nodeName) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate(message) .addNode(nodeName) .addConstraintViolation(); return false; }
private boolean failWith(ConstraintValidatorContext context, String message) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate(message) .addNode("cidr") .addConstraintViolation(); return false; }
@Override public boolean isValid(AbstractPdfOutputParameters value, ConstraintValidatorContext context) { boolean isValid = value == null || value.getVersion() == null || value.getVersion().compareTo(value.getMinRequiredPdfVersion()) >= 0; if (!isValid) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("Invalid version %s. Minimum version required is %s.", value.getVersion(), value.getMinRequiredPdfVersion())).addNode("parameters").addConstraintViolation(); } return isValid; }
private void runVerify(String testCidr, String errorMessage) { assertFalse(underTest.isValid(testCidr, contextMock)); verify(contextMock).disableDefaultConstraintViolation(); verify(contextMock).buildConstraintViolationWithTemplate(errorMessage); verify(mockConstraintViolationBuilder).addNode("cidr"); verify(mockNodeBuilderDefinedContext).addConstraintViolation(); }
@Override public boolean isValid(String value, ConstraintValidatorContext context) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( context.getDefaultConstraintMessageTemplate() ) .addParameterNode( 0 ) .addConstraintViolation(); return false; } }
@Override public boolean isValid(AbstractPdfOutputParameters value, ConstraintValidatorContext context) { boolean isValid = value == null || value.getVersion() == null || value.getVersion().compareTo(value.getMinRequiredPdfVersion()) >= 0; if (!isValid) { context.disableDefaultConstraintViolation(); context.buildConstraintViolationWithTemplate( String.format("Invalid version %s. Minimum version required is %s.", value.getVersion(), value.getMinRequiredPdfVersion())).addNode("parameters").addConstraintViolation(); } return isValid; }