FluentValidation / FluentValidation

A popular .NET validation library for building strongly-typed validation rules.
https://fluentvalidation.net
Apache License 2.0
9k stars 1.19k forks source link

Group Grapheme Clusters together when calculating the string length #1923

Closed CooperWolfe closed 2 years ago

CooperWolfe commented 2 years ago

Is your feature request related to a problem? Please describe.

Grapheme clusters (i.e. emojis) may be stored in a C# (unicode) string, but their lengths will be counted as two characters, the grapheme base and additional marks.

Describe the solution you'd like

For intuitive use, it would be nice if FluentValidation took grapheme clusters into account, counting each exactly once.

Describe alternatives you've considered

Getting string length by value.Length (in LengthValidator.cs) could be changed to the value's StringInfo.LengthInTextElements:

var stringInfo = new System.Globalization.StringInfo(value);
int length = stringInfo.LengthInTextElements;

Additional Context

N/A

JeremySkinner commented 2 years ago

Thanks for the suggestion, I think this seems like a reasonable change. I'll give your PR a review.

CooperWolfe commented 2 years ago

Ultimately it was decided that we not implement this in the suggested way, but instead to update the documentation to instruct users on how to use .Must() to count grapheme clusters once.