Top Description Methods
com.opencsv.bean.validators

public Interface StringValidator

Known Direct Implementers
com.opencsv.bean.validators.MustMatchRegexExpression
Imports
com.opencsv.bean.BeanField, com.opencsv.exceptions.CsvValidationException

This is the interface for validators for a single java.lang.String value.

Currently this is used by the PreAssignmentValidator to check the value of a string before time is taken to convert it.

For post-conversion validation there are already a plethora of third party libraries that can be incorporated into the bean, or you can just modify the setter to validate inputs.

Author
Scott Conway
Since
5.0

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

true if the value is valid, false otherwise
isValid
(String
String to be validated
value
)

Performs the validation check on the string and returns the result.

public void
setParameterString(String
Information used by the validator to validate the string
value
)

This allows the validator extending StringValidator to be used by multiple fields by allowing you to pass in data for the validator to be used.

public void
validate(String
String to be validated
value
,
BeanField<T, I>
Name of the field in the bean. This will be used in the CsvValidationException if the value is not valid.
field
)

Performs the validation check on the string and throws an exception if invalid.

Method Detail

isValidback to summary
public boolean isValid(String value)

Performs the validation check on the string and returns the result.

Parameters
value:String

String to be validated

Returns:boolean

true if the value is valid, false otherwise

setParameterStringback to summary
public void setParameterString(String value)

This allows the validator extending StringValidator to be used by multiple fields by allowing you to pass in data for the validator to be used.

Those data might be forbidden characters or regular expressions, to name two possibilities.

If the validator needs multiple parameters, then you will need to combine them into a single string using some sort of delimiter, say a comma, and parse them out using some library that allows you to parse such strings 😁.

If the validator does not need a value then just create an empty method like the MustStartWithACapitalLetter validator used by the BeanFieldValidatorTest.

Parameters
value:String

Information used by the validator to validate the string

validateback to summary
public void validate(String value, BeanField<T, I> field) throws CsvValidationException

Performs the validation check on the string and throws an exception if invalid.

Parameters
value:String

String to be validated

field:BeanField<T, I>

Name of the field in the bean. This will be used in the CsvValidationException if the value is not valid.

Exceptions
CsvValidationException:
If the input is invalid. Should contain a message describing the error.