How to configure file servers with a required rather than an empty column

I was looking at the documentation of file files, but there seems to be nothing to handle empty values ​​in columns. I need to be able to set a non-empty string attribute in all columns.

Can someone point me in the right direction?

+3
source share
4 answers

You can perform any validation required in the event AfterReadRecord. If you want to continue processing the rest of the file, if there is an error, you also need to set ErrorModeto SaveAndContinue. The following is a working example.

[DelimitedRecord("|")]
public class MyClass
{
    public string Field1;
    public string Field2;
    public string Field3;
}

class Program
{
    static void Main(string[] args)
    {
        var engine = new FileHelperEngine<MyClass>();
        engine.AfterReadRecord += new FileHelpers.Events.AfterReadHandler<MyClass>(engine_AfterReadRecord);
        engine.ErrorMode = ErrorMode.SaveAndContinue;
        // import a record with an invalid Email
        MyClass[] validRecords = engine.ReadString("Hello||World");
        ErrorInfo[] errors = engine.ErrorManager.Errors;
        Assert.AreEqual(1, engine.TotalRecords); // 1 record was processed
        Assert.AreEqual(0, validRecords.Length); // 0 records were valid
        Assert.AreEqual(1, engine.ErrorManager.ErrorCount); // 1 error was found
        Assert.That(errors[0].ExceptionInfo.Message == "Field2 is invalid");
    }

    static void engine_AfterReadRecord(EngineBase engine, FileHelpers.Events.AfterReadEventArgs<MyClass> e)
    {
        if (String.IsNullOrWhiteSpace(e.Record.Field1))
            throw new Exception("Field1 is invalid");
        if (String.IsNullOrWhiteSpace(e.Record.Field2))
            throw new Exception("Field2 is invalid");
        if (String.IsNullOrWhiteSpace(e.Record.Field3))
            throw new Exception("Field3 is invalid");
    }
}
+4
source

String.Empty FileHelpers, :

public class EmptyStringConverter : ConverterBase
{
    public override object StringToField(string sourceString)
    {
        if (String.IsNullOrWhiteSpace(sourceString))
            return null;
        return sourceString;
    }
}

,

[FieldConverter(typeof(EmptyStringConverter))]
public string Field1;

, Field1, , null.

+3

, FileHelpers FieldNotEmptyAttribute,

[DelimitedRecord("|")]
public class MyClass
{
    [FieldNotEmpty()]
    public string Field1;
    public string Field2;
    [FieldNotEmpty()]
    public string Field3;
}

, Field1 Field3 , ConvertException.

+2

, FileHelpers.FieldBase Null .

public static FileHelperEngine GetEngine() AfterReadRecord.

[DelimitedRecord(",")]
public class RequiredField
{
    public string Required;

    public static FileHelperEngine GetEngine()
    {
        var result = new FileHelperEngine(typeof(RequiredField));
        result.AfterReadRecord += AfterReadValidation;

        return result;
    }

    private static void AfterReadValidation(EngineBase sender, AfterReadRecordEventArgs args)
    {
        if (String.IsNullOrWhiteSpace(((RequiredField)args.Record).Required))
        {
            throw new ConvertException("RequiredField is Null or WhiteSpace", typeof(String));
        }
    }
}
+1

All Articles