Esempio n. 1
0
        public static ValidationErrors Value(object target)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }

            // Self validation should be tried first; then the validator should be created
            ISelfValidation sv = target.Adapt <ISelfValidation>();

            if (sv != null)
            {
                return(sv.Validate());
            }

            Validator v = Validator.CreateSelfValidator(target.GetType());

            if (v != null)
            {
                return(v.Validate(target));
            }

            v = Validator.Create(target.GetType());
            if (v != null)
            {
                return(v.Validate(target));
            }

            throw ValidationFailure.ValidationNotDefined();
        }
Esempio n. 2
0
        public static Validator Create(KnownValidator knownValidator)
        {
            switch (knownValidator)
            {
            case KnownValidator.Required:
                return(new RequiredValidator());

            case KnownValidator.Length:
                return(new LengthValidator());

            case KnownValidator.Range:
                return(new RangeValidator());

            case KnownValidator.PropertyComparison:
                return(new PropertyComparisonValidator());

            case KnownValidator.Email:
                return(new EmailValidator());

            case KnownValidator.Regex:
                return(new RegexValidator());

            case KnownValidator.Negative:
                return(new NegativeValidator());

            case KnownValidator.NonNegative:
                return(new NonPositiveValidator());

            case KnownValidator.Positive:
                return(new PositiveValidator());

            case KnownValidator.NonPositive:
                return(new NonPositiveValidator());

            case KnownValidator.NonZero:
                return(new NonZeroValidator());

            case KnownValidator.Self:
                throw ValidationFailure.CannotCreateKnownValidatorThisWay("knownValidator");

            case KnownValidator.Unknown:
                throw new ArgumentOutOfRangeException("knownValidator", knownValidator, SR.CannotBeUnknownValidator());     // $NON-NLS-1

            default:
                throw Failure.NotDefinedEnum("knownValidator", knownValidator);     // $NON-NLS-1
            }
        }
        public static IEnumerable GetDictionaryKeys(object target)
        {
            var type = target.GetType();

            var iface = type.GetInterface("System.Collections.Generic.IDictionary`2");

            if (iface == null)
            {
                throw ValidationFailure.DictionaryTypeRequired();
            }

            var map = type.GetInterfaceMap(iface);

            for (int i = 0; i < map.InterfaceMethods.Length; i++)
            {
                if (map.InterfaceMethods[i].Name == "get_Keys")
                {
                    return((IEnumerable)map.TargetMethods[i].Invoke(target, null));
                }
            }

            return(null);
        }