I am updating an old validator which does not use FluentValidation to use FluentValidation.
I have a two classes CsvRow which is a List<string> and CsvFile which is a List<CsvRow>
I have a CsvRowValidator
/// <summary>
/// Validates a csv file's row.
/// </summary>
public class CsvRowValidator : AbstractValidator<CsvRow>
{
/// <summary>
/// Initializes a new instance of the <see cref="CsvRowValidator"/> class.
/// </summary>
/// <param name="expectedNumberOfRows">The expected number of rows.</param>
/// <exception cref="System.ArgumentException">The number of expected rows must be greater than or equal to zero.</exception>
public CsvRowValidator(int expectedNumberOfRows)
{
if (expectedNumberOfRows < 0)
throw new ArgumentException("The number of expected rows must be greater than or equal to zero.");
RuleFor(row => row.Count).Equal(expectedNumberOfRows);
}
}
I want a CsvFileValidator
public class CsvFileValidator : AbstractValidator<CsvFile>
{
public CsvFileValidator(int expectedNumberOfRows)
{
if (expectedNumberOfRows < 0)
throw new ArgumentException("The number of expected rows must be greater than or equal to zero.");
}
}
I want this validator to generate an error message for each row in the file:
"Row 23 has 5 columns but expected 6. Knowing which row I'm on is the part I'm having trouble determining. My old validator worked like this:
public bool Validate(CsvFile file)
{
bool valid = true;
bool validRow;
_errors.Clear();
int index = 0;
file.ForEach(row => {
index++;
validRow = row.Count == _expectedNumberOfColumns;
valid &= validRow;
if (!validRow)
_errors.Add(string.Format(ESNCommonTypesResources.RowErrorMessageForFile, index.ToString(), row.Count.ToString(), _expectedNumberOfColumns.ToString()));
});
return valid;
}
Can someone point me in the right direction?