Exemplo n.º 1
0
 /// <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"));
Exemplo n.º 2
0
 /// <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"));
Exemplo n.º 3
0
 /// <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"));
Exemplo n.º 4
0
 /// <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"));
Exemplo n.º 5
0
 /// <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"));
Exemplo n.º 6
0
 /// <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"));