When parsing a csv file, how do i define that a specific field is mandatory. Essentially, I want to make sure that a given field is never empty, and if it is then I would like an exception thrown. Here is the mapping class:
public sealed class DataMapper : CsvClassMap<DataType>
{
public DataMapper()
{
Map(m => m.Field1).Name("FirstField");
Map(m => m.Field2).Name("SecondField");
Map(m => m.Field3).Name("ThirdField"); // this field should be mandatory
}
}
and the usage:
List<DataType> data;
using (var sr = new StreamReader(localFilePath))
{
var reader = new CsvReader(sr);
reader.Configuration.RegisterClassMap<DataMapper>();
data = reader.GetRecords<DataType>().ToList();
}
Currently I am just checking the results in the data list as follows:
var numberOfInvalidRecords = data.Count(data => string.IsNullOrEmpty(data.Field3));
if (nullAccountHolderRecords > 0)
{
//handle
}
I was unable to find a built-in feature in the CSVHelper documentation. Am I missing something?
I'd probably do this using the ConvertUsing
extension:
public sealed class DataMapper : CsvClassMap<DataType>
{
public DataMapper()
{
Map(m => m.Field1).Name("FirstField");
Map(m => m.Field2).Name("SecondField");
Map(m => m.Field3).ConvertUsing(row =>
{
if(string.IsNullOrEmpty(row.GetField<string>("ThirdField")))
throw new Exception("Oops, ThirdField is empty!");
return row.GetField<string>("ThirdField");
});
}
}
Here is a solution that extends the API:
public static class CsvHelperExtensions
{
public static CsvPropertyMap Required<T>(this CsvPropertyMap map, string columnName)
{
return map.Name(columnName).ConvertUsing(row =>
{
if (string.IsNullOrEmpty(row.GetField(columnName)))
throw new CsvParserException($"{columnName} is required, but missing from row {row.Row}");
return row.GetField<T>(columnName);
});
}
}
Usage:
public CsvPersonMap()
{
Map(m => m.FirstName).Required<string>("First");
Map(m => m.LastName).Name("Last");
Map(m => m.MiddleName).Required<string>("Middle");
}
The developer has now added a Validate method: https://joshclose.github.io/CsvHelper/examples/configuration/class-maps/validation
Using this to validate against a non-null or empty string:
Map(m => m.Id).Validate(field => !string.IsNullOrEmpty(field));
https://github.com/JoshClose/CsvHelper/issues/556
As suggested by the creator of CsvHelper here:
For the time being, I think you'll have to have WillThrowOnMissingField = false and run a loop and check your specific required fields. You can probably just check the header after the first read.
His sample code:
csv.WillThrowOnMissingField = false;
var list = new List<MyObject>();
var headerChecked = false;
while( csv.Read() )
{
if( !headerChecked )
{
// check for specific headers
if( !csv.FieldHeaders.Exists( "MyHeaderName" ) )
{
throw new Exception( "message" );
}
headerChecked = true;
}
list.Add( csv.GetRecord<MyObject>() );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With