View Javadoc
1   package com.opencsv.bean.validators;
2   
3   import com.opencsv.bean.BeanField;
4   import com.opencsv.exceptions.CsvValidationException;
5   
6   /**
7    * This is the interface for validators for a single
8    * {@link java.lang.String} value.
9    * <p>Currently this is used by the {@link PreAssignmentValidator} to check the
10   * value of a string before time is taken to convert it.</p>
11   * <p>For post-conversion validation there are already a plethora of third
12   * party libraries that can be incorporated into the bean, <em>or</em> you can
13   * just modify the setter to validate inputs.</p>
14   *
15   * @author Scott Conway
16   * @since 5.0
17   */
18  public interface StringValidator {
19      /**
20       * Performs the validation check on the string and returns the result.
21       *
22       * @param value String to be validated
23       * @return {@code true} if the value is valid, {@code false} otherwise
24       */
25      boolean isValid(String value);
26  
27      /**
28       * Performs the validation check on the string and throws an exception if
29       * invalid.
30       *
31       * @param value String to be validated
32       * @param field Name of the field in the bean.  This will be used in
33       *                  the {@link CsvValidationException} if the value is not
34       *                  valid.
35       * @throws CsvValidationException If the input is invalid. Should contain a
36       * message describing the error.
37       */
38      void validate(String value, BeanField field) throws CsvValidationException;
39  
40      /**
41       * This allows the validator extending {@link StringValidator} to be used
42       * by multiple fields by allowing you to pass in data for the validator to
43       * be used.
44       * <p>Those data might be forbidden characters or regular expressions, to
45       * name two possibilities.</p>
46       * <p>If the validator needs multiple parameters, then you will need to
47       * combine them into a single string using some sort of delimiter, say a
48       * comma, and parse them out using some library that allows you to parse
49       * such strings 😁.</p>
50       * <p>If the validator does not need a value then just create an empty
51       * method like the MustStartWithACapitalLetter validator used by the
52       * BeanFieldValidatorTest.</p>
53       *
54       * @param value Information used by the validator to validate the string
55       */
56      void setParameterString(String value);
57  }