Top Description Fields Constructors Methods
com.opencsv.bean.validators

public Class MustMatchRegexExpression

extends Object
implements StringValidator
Class Inheritance
All Implemented Interfaces
com.opencsv.bean.validators.StringValidator
Imports
com.opencsv.bean.BeanField, com.opencsv.exceptions.CsvValidationException, java.util.ResourceBundle

This is a validator that, due to the addition of the parameter, allows the validation of multiple different types of input. The paramString must be a valid regular expression. The MustMatchRegularExpression validator will the String#matches(String) method on the string to be converted and the regular expression string and if the two do not match then a CsvValidationException will be thrown.

Because this is validating the string before it is parsed/converted, the capture settings of the string must be taken into account.

Examples:

    // The String that becomes id must be a number with three to six digits.
    @PreAssignmentValidator(validator = MustMatchRegexExpression.class, paramString = "^[0-9]{3,6}$")
    @CsvBindByName(column = "id")
    private int beanId;

    // The String that becomes bigNumber must be a number with seven to ten digits.
    // The String for this field is after the word "value: " in the field.
    @PreAssignmentValidator(validator = MustMatchRegexExpression.class, paramString = "^[A-Za-z ]*value: [0-9]{7,10}$")
    @CsvBindByName(column = "big number", capture = "^[A-Za-z ]*value: (.*)$", format = "value: %s")
    private long bigNumber;

Field Summary

Modifier and TypeField and Description
private String

Constructor Summary

AccessConstructor and Description
public
MustMatchRegexExpression()

Default constructor.

Method Summary

Modifier and TypeMethod and Description
public boolean
isValid(String value)

Implements com.opencsv.bean.validators.StringValidator.isValid.

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

public void
setParameterString(String value)

Implements com.opencsv.bean.validators.StringValidator.setParameterString.

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 value, BeanField<T, I> field)

Implements com.opencsv.bean.validators.StringValidator.validate.

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

regexback to summary
private String regex

Constructor Detail

MustMatchRegexExpressionback to summary
public MustMatchRegexExpression()

Default constructor.

Method Detail

isValidback to summary
public boolean isValid(String value)

Implements com.opencsv.bean.validators.StringValidator.isValid.

Doc from com.opencsv.bean.validators.StringValidator.isValid.

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

Annotations
@Override
setParameterStringback to summary
public void setParameterString(String value)

Implements com.opencsv.bean.validators.StringValidator.setParameterString.

Doc from com.opencsv.bean.validators.StringValidator.setParameterString.

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.

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

Implements com.opencsv.bean.validators.StringValidator.validate.

Doc from com.opencsv.bean.validators.StringValidator.validate.

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

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