CsvFieldBadValue Delegate

Definition

Namespace: DataStandardizer.File.CSV

The delegate called when a bad CSV field value is encountered, so you can handle it instead of letting a CsvFileException be thrown.

Syntax

public delegate void CsvFieldBadValue<TModel>(CsvFieldContext<TModel> context)
    where TModel : class;

Remarks

Assigned to CsvFileOptions.BadValueHandler. The context carries the field’s identity, raw value, and the model. See CsvFieldContext.

TModel must be the record line type of the reader the options are passed to: a CsvFileReader<OrderLine> calls a CsvFieldBadValue<OrderLine>. The reader constructor throws ArgumentException for a handler of any other type, such as a CsvFieldBadValue<CsvFileRecordLine> given to a reader of a derived line type. A writer never calls the handler and doesn’t check it.

A reader calls it for a field it can’t read, such as an unquoted field that holds a quote or a line break, for a field a mapping’s validator rejects, and for a field it can’t convert to the mapped property’s type. See converting field values. An exception thrown by a CsvFieldConvertFromString delegate isn’t a bad value: it propagates unchanged, and the handler isn’t called.

For a field that breaks the quoting rules of RFC 4180 section 2, the handler is called once for each violation the reader finds, so one field can lead to several calls. The context’s RawFieldValue holds the field’s text read up to that violation, with quotes removed, not the whole field: for "a"bc the handler is called with ab and then abc. Once the handler returns, the reader uses an empty value for the field, wherever it is in the record, so the line, and a mapped property bound to the field, gets an empty value. If the property’s converter rejects an empty value, that is a second bad value, and the handler is called again for the field, with an empty RawFieldValue.

An escaped field is only a quoted value, so whitespace before its opening quote or after its closing quote is a violation, like any other text there. An unquoted field can’t hold a carriage return or line feed that isn’t part of the terminator, so each one is a violation too.

A quoted field that the file ends inside, with no closing quote, is one of these violations. Its RawFieldValue holds everything after the opening quote up to the end of the file, delimiters and line breaks included, so the records after it are never returned.

Applies to

Targets netstandard1.1, netstandard2.0, netstandard2.1, and net10.0.

See also


This site uses Just the Docs, a documentation theme for Jekyll.