/// <summary> /// Validates that the value passed is a positive number /// </summary> public static Validation <TestError, int> PositiveNumber(int value) => value > 0 ? Success <TestError, int>(value) : Fail <TestError, int>(TestError.New($"must be positive"));
/// <summary> /// Validates that the value passed is a month /// </summary> public static Validation <TestError, int> ValidMonth(int month) => month >= 1 && month <= 12 ? Success <TestError, int>(month) : Fail <TestError, int>(TestError.New($"invalid month"));
/// <summary> /// Uses parseInt which returns an Option and converts it to a Validation /// value with a default Error if the parse fails /// </summary> public static Validation <TestError, int> ToInt(string str) => parseInt(str).ToValidation(TestError.New("must be a number"));
/// <summary> /// Validates that the string passed contains only digits /// </summary> public static Validation <TestError, string> DigitsOnly(string str) => str.ForAll(Char.IsDigit) ? Success <TestError, string>(str) : Fail <TestError, string>(TestError.New($"only numbers are allowed"));
/// <summary> /// Creates a delegate that when passed a string will validate that it's below /// a specific length /// </summary> public static Func <string, Validation <TestError, string> > MaxStrLength(int max) => str => str.Length <= max ? Success <TestError, string>(str) : Fail <TestError, string>(TestError.New($"can not exceed {max} characters"));
/// <summary> /// Validates the string has only ASCII characters /// </summary> public static Validation <TestError, string> AsciiOnly(string str) => str.ForAll(c => c <= 0x7f) ? Success <TestError, string>(str) : Fail <TestError, string>(TestError.New("only ascii characters are allowed"));