Skip to content

Is it possible to enforce a max length on a string with a mapping? #2110

Answered by JoshClose
R1Fo asked this question in Q&A
Discussion options

You must be logged in to vote

No. You could write a custom converter to do it though.

public class TruncateStringConverter : StringConverter
{
	public override object ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData)
	{
		var s = base.ConvertFromString(text, row, memberMapData) as string;
		return s == null ? s : s.Substring(0, 50);
	}
}

You can apply that via a mapping like

Map(m => m.StringProperty).TypeConverter<TruncateStringConverter>();

Or globally for all strings like

csv.Context.TypeConverterCache.AddConverter<string>(new TruncateStringConverter());

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@R1Fo
Comment options

Answer selected by R1Fo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants