How to reject a missing value in Filehelpers?

37 Views Asked by At

I am looking at using Filehelpers for parsing a CSV file. However, I need to reject entries that have missing values. This does not seem to be in the documentation which mentions only Nullable and FieldNullValue attributes which don't reject the entire row.

Is this possible with filehelpers?

1

There are 1 best solutions below

0
shamp00 On

You can use the [FieldNotEmpty()] attribute. Here is a working example:

[DelimitedRecord(",")]
public class Contact
{
    [FieldTrim(TrimMode.Both)]
    public string Name;
    [FieldTrim(TrimMode.Both)]
    public string Street;
    [FieldNotEmpty()]
    public string City;
    [FieldTrim(TrimMode.Both)]
    public string State;
    [FieldTrim(TrimMode.Both)]
    public string Zip;
}

internal class Program
{
    static void Main(string[] args)
    {
        var engine = new DelimitedFileEngine<Contact>();
        engine.ErrorMode = ErrorMode.SaveAndContinue;

        var records = engine.ReadString("Billy, Bello,, IL, 12342");

        Assert.AreEqual(1, engine.ErrorManager.Errors.Count());
        Assert.AreEqual("Line: 1. Column: 14. Field: City. Error Converting '' to type: 'String'. The value is empty and must be populated.", engine.ErrorManager.Errors[0].ExceptionInfo.Message);

        Console.WriteLine("All OK");
        Console.ReadKey();
    }
}