示例#1
0
        public StringValidator(StringValidatorOperation operation, string value)
        {
            _operation  = operation;
            _checkValue = value;

            Value     = value;
            Operation = (int)operation;

            if (!CheckIntegerValueForLenthOperation(operation, _checkValue, out int intValue))
            {
                throw new ArgumentException($"Incorrect value for string length! Value: {value}");
            }

            _operationFunc = new Dictionary <StringValidatorOperation, Func <string, bool> >
            {
                [StringValidatorOperation.LengthGreater]        = new Func <string, bool>(x => x.Length > intValue),
                [StringValidatorOperation.LengthGreaterOrEqual] = new Func <string, bool>(x => x.Length >= intValue),
                [StringValidatorOperation.LengthLess]           = new Func <string, bool>(x => x.Length < intValue),
                [StringValidatorOperation.LengthLessOrEqual]    = new Func <string, bool>(x => x.Length <= intValue),
                [StringValidatorOperation.StartWith]            = new Func <string, bool>(x => x.StartsWith(_checkValue)),
                [StringValidatorOperation.Contains]             = new Func <string, bool>(x => x.Contains(_checkValue)),
                [StringValidatorOperation.EndWith]  = new Func <string, bool>(x => x.EndsWith(_checkValue)),
                [StringValidatorOperation.NotEmpty] = new Func <string, bool>(x => !string.IsNullOrWhiteSpace(x))
            };
        }
示例#2
0
 private bool CheckIntegerValueForLenthOperation(StringValidatorOperation operation, string value, out int result)
 {
     if (_lengthOperations.Contains(operation))
     {
         return(int.TryParse(value, out result));
     }
     result = default;
     return(true);
 }
示例#3
0
 public StringValidator(StringValidatorOperation operation, int value) : this(operation, value.ToString())
 {
 }