Files
ersatztv/ErsatzTV.Application/Validators/StringValidation.cs
T
Jason DoveandGitHub 1ab98578ab refactor namespaces and imports (#670)
* re-namespace

* optimize usings

* more usings

* more of the same

* more implicit/global usings

* cleanup all usings

* minor fixes
2022-03-03 15:36:07 -06:00

20 lines
822 B
C#

using System.Linq.Expressions;
using ErsatzTV.Core;
namespace ErsatzTV;
public static partial class Validators
{
public static Func<Expression<Func<T, string>>, Validation<BaseError, string>> NotLongerThan<T>(
this T input,
int maxLength) =>
expression => Optional(expression)
.Map(exp => exp.Compile()(input))
.Where(s => s.Length <= maxLength)
.ToValidation<BaseError>($"[{GetMemberName(expression)}] must not be longer than {maxLength}");
public static Validation<BaseError, string> NotEmpty<T>(this T input, Expression<Func<T, string>> expression) =>
Optional(expression.Compile()(input))
.Where(s => !string.IsNullOrWhiteSpace(s))
.ToValidation<BaseError>($"[{GetMemberName(expression)}] is an empty string");
}