CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

Field Validate - Passing information about why validation failed

Open simplifirepyroneil opened this issue 4 years ago • 0 comments

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");
  }

simplifirepyroneil avatar Dec 02 '21 19:12 simplifirepyroneil