예제 #1
0
        public override bool Validate(string value)
        {
            var comparer = new ComparerFactory().GetComparer(Type);

            if (!comparer.TryConvert(value, false, out var convertedValue))
            {
                return(false);
            }

            if (Operator == ValidationCompareOperator.DataTypeCheck)
            {
                return(true);
            }

            if (!comparer.TryConvert(ValueToCompare, CultureInvariantValues, out var valueToCompare))
            {
                return(true);
            }

            var compareResult = comparer.CompareTo(convertedValue, valueToCompare);

            return(Operator switch
            {
                EqualCompareOperator e => compareResult == 0,
                NotEqualCompareOperator e => compareResult != 0,
                GreaterThanCompareOperator g => compareResult > 0,
                GreaterThanEqualCompareOperator g => compareResult >= 0,
                LessThanCompareOperator l => compareResult < 0,
                LessThanEqualCompareOperator l => compareResult <= 0,
                _ => true
            });
        protected bool Compare(string leftText, bool cultureInvariantLeftText, string rightText, bool cultureInvariantRightText, ValidationCompareOperator op, ValidationDataType type)
        {
            var comparer = new ComparerFactory().GetComparer(type);

            if (!comparer.TryConvert(leftText, cultureInvariantLeftText, out var leftValue))
            {
                return(false);
            }

            if (op == ValidationCompareOperator.DataTypeCheck)
            {
                return(true);
            }

            if (!comparer.TryConvert(rightText, cultureInvariantRightText, out var rightValue))
            {
                return(true);
            }

            var compareResult = comparer.CompareTo(leftValue, rightValue);

            return(op switch
            {
                EqualCompareOperator _ => compareResult == 0,
                NotEqualCompareOperator _ => compareResult != 0,
                GreaterThanCompareOperator _ => compareResult > 0,
                GreaterThanEqualCompareOperator _ => compareResult >= 0,
                LessThanCompareOperator _ => compareResult < 0,
                LessThanEqualCompareOperator _ => compareResult <= 0,
                _ => true
            });