title: OpenCsv - set custom validation on field
body: <p>I am using OpenCsv to convert a csv file into Java Bean.</p>
  
  <p>My file looks like this:</p>
  
  <pre><code>USER_NAME, USER_ID, FREQUENCY
  aaa,111,DAILY
  bbb,222,WEEKLY,
  ccc,333,MONTHLY
  ddd,444,SOME_RANDOM_VALUE
  eee,555,YEARLY
  </code></pre>
  
  <p>I need to enforce the below validation on the columns:</p>
  
  <pre><code>USER_NAME -&gt; not null, alphabets only
  USER_ID -&gt; not null, digits only
  FREQUENCY -&gt; not null, allowed values - DAILY, WEEKLY, MONTHLY, YEARLY
  </code></pre>
  
  <p>For any other value of FREQUENCY, log the USER_NAME, column as given below:
  dddd, FREQUENCY</p>
  
  <p>I only need the line to be logged and the flow to continue as usual, i.e. 'eeee' should be mapped to the java bean.</p>
  
  <p>Based on <a href="https://stackoverflow.com/questions/48691164/opencsv-custom-logic-in-parcing">Opencsv custom logic in parcing</a>, I created the following logic in the java bean.</p>
  
  <p>The trouble is that the flow stops with 'dddd' and 'eeee' is not mapped.</p>
  
  <pre><code>class UserBean {
  
      private String userName;
      private String userID;
      private String frequency;
  
      //Getters and Setters for userName, userID
      public void setFrequency(String frequency) {
          if(frequency.isEmpty() &amp;&amp; !(frequency.equalsIgnoreCase("DAILY") &amp;&amp; frequency.equalsIgnoreCase("WEEKLY") &amp;&amp; frequency.equalsIgnoreCase("MONTHLY") &amp;&amp; frequency.equalsIgnoreCase("YEARLY"))) {
              throw new IllegalArgumentException("Invalid value for FREQUENCY"); 
          }
      }
  }
  
  public static void main() {
  
              CsvToBean&lt;UserBean&gt; csvToBean = new CsvToBean&lt;UserBean&gt;();
              Reader reader = new FileReader((File) INPUT_DIR);
              csvToBean = new CsvToBeanBuilder(reader).withSeparator(',')
                      .withType(User.class)
                      .withOrderedResults(true)
                      .withIgnoreLeadingWhiteSpace(true)
                      .build();
              Iterator&lt;PrimeAOBean&gt; csvUserIterator = csvToBean.iterator();
  
              List&lt;Exception&gt; exceptionList = new ArrayList&lt;Exception&gt;();
  
              while (csvUserIterator.hasNext()) {
                  try {
                  UserBean userBean = csvUserIterator.next();
                  userDataMap.put(userBean.getUserID(), userBean);
                  }
                  catch(Exception e) {
                      exceptionList.add(e);
                      continue;
                  }
                  finally {
                      FileWriter fileWriter = new FileWriter(ERROR_DIR);
                      PrintWriter printWriter = new PrintWriter(fileWriter);
                      for (Exception excep: exceptionList) {
                          printWriter.print(excep.getLocalizedMessage());
                          printWriter.close();
                      }
                      continue;
                  }
              }               
                  }
  </code></pre>