CsvHelper
CsvHelper copied to clipboard
Field Validate - Passing information about why validation failed
when a Field Validate returns false, a FieldValidationException is thrown. However, If the validator throws an exception, the exception is wrapped and a generic CsvHelperException is thrown. I propose that if the Validator throws an ArgumentException, that should be wrapped in a FieldValidationException instead. The inner exception message can be used to provide context for logging when a FieldValidationException is caught.
public static bool ValidateModule(ValidateArgs args)
{
if(string.IsNullOrEmpty(args?.Field))
throw new ArgumentException("Module must not be null or empty.");
if(byte.TryParse(args.Field, out byte module))
{
if(module >= 1 && module <= 255)
return true;
}
throw new ArgumentException("Module must be between 1 and 255");
}