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)); }
| Modifier and Type | Field and Description |
|---|---|
| private String | |
| private Function |
| Access | Constructor and Description |
|---|---|
| public | RowFunctionValidator(Function<String[], Boolean>
- function to run against the Array of Strings. testFunction, String - message to be included in the CsvValidationException error message. failureMessage)Default Constructor. |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | isValid(String[] row)
Implements com. Performs the validation check on the string and returns the result. |
| public void | validate(String[] row)
Implements com. Performs the validation check on the row (an array of |
| failureMessage | back to summary |
|---|---|
| private String failureMessage | |
| testFunction | back to summary |
|---|---|
| private Function<String[], Boolean> testFunction | |
| RowFunctionValidator | back to summary |
|---|---|
| public RowFunctionValidator(Function<String[], Boolean> testFunction, String failureMessage) Default Constructor. | |
| isValid | back to summary |
|---|---|
| public boolean isValid(String[] row) Implements com. Doc from com. 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.
| |
| validate | back to summary |
|---|---|
| public void validate(String[] row) throws CsvValidationException Implements com. Doc from com. Performs the validation check on the row (an array of
| |