@@ -144,7 +144,7 @@ public static string IfLongerThan(
144144 /// </summary>
145145 /// <param name="_"></param>
146146 /// <param name="value">Value to validate</param>
147- /// <param name="minLength">Max length</param>
147+ /// <param name="minLength">Min length</param>
148148 /// <param name="parameterName">If not defined, the name of the variable passed by the <paramref name="value"/> parameter will be used</param>
149149 /// <exception cref="PropertyException">Exception thrown when the length of the value is less than</exception>
150150 [ System . Obsolete ( "This method is deprecated. It will be removed on 2022/09/30. Use the new method 'string.IfShorterThan'" ) ]
@@ -160,7 +160,7 @@ public static string IfLengthLessThan(
160160 /// </summary>
161161 /// <param name="_"></param>
162162 /// <param name="value">Value to validate</param>
163- /// <param name="minLength">Max length</param>
163+ /// <param name="minLength">Min length</param>
164164 /// <param name="parameterName">If not defined, the name of the variable passed by the <paramref name="value"/> parameter will be used</param>
165165 /// <exception cref="PropertyException">Exception thrown when the value is shorter than</exception>
166166 public static string IfShorterThan (
@@ -363,5 +363,40 @@ public static string IfDifferent(
363363
364364 return value ;
365365 }
366+
367+ /// <summary>
368+ /// Throws an <see cref="PropertyException" /> if <paramref name="value"/> has a length out of range. Error code 'MIN:{X}' or 'MAX:{X}'
369+ /// </summary>
370+ /// <param name="_"></param>
371+ /// <param name="value">Value to validate</param>
372+ /// <param name="minLength">Min length</param>
373+ /// <param name="maxLength">Max length</param>
374+ /// <param name="parameterName">If not defined, the name of the variable passed by the <paramref name="value"/> parameter will be used</param>
375+ /// <exception cref="PropertyException">Exception thrown when the length of the value is out of range</exception>
376+ public static string IfLengthOutOfRange (
377+ this IGuardValidationClause _ ,
378+ string value ,
379+ int minLength ,
380+ int maxLength ,
381+ [ CallerArgumentExpression ( "value" ) ] string parameterName = null
382+ )
383+ {
384+ if ( value == null )
385+ {
386+ return value ;
387+ }
388+
389+ if ( value . Length < minLength )
390+ {
391+ throw new PropertyException ( parameterName , ErrorCodes . GetMinFormatted ( minLength ) ) ;
392+ }
393+
394+ if ( value . Length > maxLength )
395+ {
396+ throw new PropertyException ( parameterName , ErrorCodes . GetMaxFormatted ( maxLength ) ) ;
397+ }
398+
399+ return value ;
400+ }
366401 }
367402}
0 commit comments