Skip to content

5. Validation

Jos de Weger edited this page Feb 28, 2022 · 1 revision

Validation

The following validations are provided out of the box:

  • IsRequired (value can not be empty)
  • Matches (value must match regex pattern provided)
  • WithMinimum (value can not have value smaller than minimum provided)
  • WithMaximum (value can not have value bigger than maximum provided)
  • ShouldHaveUniqueValue (value has to be unique in column)

Besides these default validations, you can specify a custom validation with validation message:

new SheetMapper().AddConfigFor<TestModel>(cfg => cfg
    .MapColumn(column => column
        .WithHeader("Age")
        .WithCustomRule<int>(x => x >= 18 && x <= 67, "Age should be between 3 and 5")
        .MapTo(t => t.Age)));

If that still doesn't suit your needs, you can extend the library by implementing one of the following interfaces and writing extension methods:

  • IParsingRule (these rules are validated during parsing of the cell value, e.g. IsRequiredRule)
  • IComparableRule (these rules do some kind of comparison between values)
  • IGenericRule
  • IColumnRule (rules that relate to other values in the same column)
Clone this wiki locally