Package com.opencsv.validators
Class RowFunctionValidator
- java.lang.Object
-
- com.opencsv.validators.RowFunctionValidator
-
- All Implemented Interfaces:
RowValidator
public class RowFunctionValidator extends java.lang.Object implements RowValidator
This validator is best used to validate a specific property of the row - either about a specific element or information about the array itself.An empty or null first row is considered invalid.
As with all row validators the assumption is you have control of the data and have skipped any initial empty lines AND that your validator checks the size or handles the IndexOutOfBoundsException.
There are several examples coded in the RowFunctionValidatorTest but here are a couple to give you the idea of the flexibility this validator offers.
private static final String[] GOOD_ROW = {"8675309", "Firstname", "M", "Lastname", "Dec 06, 1951"}; private static final String[] BAD_ROW = {"not a number", "not capitialized", "not an initial", "Not Single word", "12/06/51"}; private static final String[] LONG_ROW = {"8675309", "Firstname", "M", "Lastname", "Dec 06, 1951", "More data"}; private static final String[] SHORT_ROW = {"8675309", "Firstname", "Lastname", "Dec 06, 1951"}; private static final Function<String[], Boolean> THIRD_ELEMENT_IS_MIDDLE_INITIAL = (x) -> { return x.length > 2 && x[2].matches("^[A-Z]$"); }; private static final Function<String[], Boolean> ROW_MUST_HAVE_FIVE_ELEMENTS = (x) -> { return (x.length == 5); };
@Test
@DisplayName("Simple test to show checking an middle initial")
public void thirdElementIsMiddleInitial() { validator = new RowFunctionValidator(THIRD_ELEMENT_IS_MIDDLE_INITIAL, "The third element must be the middle initial."); assertTrue(validator.isValid(GOOD_ROW)); assertFalse(validator.isValid(BAD_ROW)); }
@Test
@DisplayName("The row must have a specific number of elements in order to be valid.")
public void numberOfElementsInARow() { validator = new RowFunctionValidator(ROW_MUST_HAVE_FIVE_ELEMENTS, "A Row can have only five elements."); assertTrue(validator.isValid(GOOD_ROW)); assertFalse(validator.isValid(LONG_ROW)); assertFalse(validator.isValid(SHORT_ROW)); }
- Since:
- 5.0
- Author:
- Scott Conway
-
-
Constructor Summary
Constructors Constructor Description RowFunctionValidator(java.util.function.Function<java.lang.String[],java.lang.Boolean> testFunction, java.lang.String failureMessage)
Default Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
isValid(java.lang.String[] row)
Performs the validation check on the string and returns the result.void
validate(java.lang.String[] row)
Performs the validation check on the row (an array ofString
s) and throws an exception if invalid.
-
-
-
Constructor Detail
-
RowFunctionValidator
public RowFunctionValidator(java.util.function.Function<java.lang.String[],java.lang.Boolean> testFunction, java.lang.String failureMessage)
Default Constructor.- Parameters:
testFunction
- - function to run against the Array of Strings.failureMessage
- - message to be included in the CsvValidationException error message.
-
-
Method Detail
-
isValid
public boolean isValid(java.lang.String[] row)
Description copied from interface:RowValidator
Performs the validation check on the string and returns the result. While not called directly in opencsv it is in the interface to provide an easy way to test if the validator is functioning properly.- Specified by:
isValid
in interfaceRowValidator
- Parameters:
row
- Array of strings to be validated- Returns:
true
if the row is valid,false
otherwise
-
validate
public void validate(java.lang.String[] row) throws CsvValidationException
Description copied from interface:RowValidator
Performs the validation check on the row (an array ofString
s) and throws an exception if invalid.- Specified by:
validate
in interfaceRowValidator
- Parameters:
row
- Array ofString
s to be validated.- Throws:
CsvValidationException
- Thrown if invalid. Should contain a message describing the error.
-
-