Menu

#113 How to have a required column but a nullable content

v1.0 (example)
closed
None
1
2023-07-10
2023-03-08
No

Hello,
Using a CsvToBeanBuilder, I wish to know if there is a way to have a required column (named column must present in the source CSV) but with a nullable content.
Apparently, CsvBindByName "required" attribute forces both.

Regards

Alberto

Discussion

  • Scott Conway

    Scott Conway - 2023-04-01

    Hello Alberto

    Sorry it took so long to get back to you but life has been busy :)

    So you only care that the column is present but not about the data.

    Honestly I would just create a CSVReader and read the header line and check the array for the value and then close that reader. If the header array has the value then create your StatefulCsvToBean with any mapping strategy other than the HeaderColumnNameMappingStrategy - because that is the one that is checking the required annotation.

    Or you can use the mapping strategy. I have created and committed a SR113Test that you can look at for an example. I used the HeaderColumnNameMappingStrategy and give it two tests: one with a string with a required field and the other that is missing the field. And then I use the strategy to give me the header and check if it contains the field I want.

      @Test
        @DisplayName("Using mapping strategy to enforce a header missing state")
        public void enforceColumns() throws CsvRequiredFieldEmptyException {
            MappingStrategy<Feature> strategy = createTranslateMappingStrategy();
            CsvToBean<Feature> csvToBean = new CsvToBeanBuilder<Feature>(new StringReader(TEST_EMPTY_STRING_NO_STATE))
                    .withMappingStrategy(strategy)
                    .build();
            List<Feature> list = csvToBean.parse();
            String[] header = strategy.generateHeader(list.get(0));
            assertFalse(Arrays.asList(header).contains("STATE"));
        }
    
        @Test
        @DisplayName("Using mapping strategy to enforce a header with state")
        public void enforceColumnsWithColumn() throws CsvRequiredFieldEmptyException {
            MappingStrategy<Feature> strategy = createTranslateMappingStrategy();
            CsvToBean<Feature> csvToBean = new CsvToBeanBuilder<Feature>(new StringReader(TEST_EMPTY_STRING))
                    .withMappingStrategy(strategy)
                    .build();
            List<Feature> list = csvToBean.parse();
            String[] header = strategy.generateHeader(list.get(0));
            assertTrue(Arrays.asList(header).contains("STATE"));
        }
    

    Hope that helps

     
  • Scott Conway

    Scott Conway - 2023-07-10
    • status: open --> closed
    • assigned_to: Scott Conway
     
  • Scott Conway

    Scott Conway - 2023-07-10

    closed for lack of response.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.