public EqualValidator(Func <object, object> comparisonProperty, MemberInfo member)
     : base(nameof(Messages.equal_error), typeof(Messages))
 {
     ErrorCodeSource = new StaticStringSource(ValidationErrors.Equal);
     func            = comparisonProperty;
     MemberToCompare = member;
 }
 public EqualValidator(object valueToCompare, IEqualityComparer comparer)
     : base(nameof(Messages.equal_error), typeof(Messages))
 {
     ErrorCodeSource = new StaticStringSource(ValidationErrors.Equal);
     ValueToCompare  = valueToCompare;
     this.comparer   = comparer;
 }
        public UserHasCorrectValidator(IEnumerable <string> role, IMediator mediator) : base(new StaticStringSource(""))
        {
            this.role     = role;
            this.mediator = mediator;

            ErrorCodeSource = new StaticStringSource(nameof(UserHasCorrectValidator));
        }
        protected override bool IsValid(PropertyValidatorContext context)
        {
            var establishmentId     = _establishmentId((T)context.Instance);
            var principalOrPersonId = context.PropertyValue;
            var principal           = principalOrPersonId as IPrincipal;
            var personId            = principal != null ? 0 : (int)principalOrPersonId;

            if (personId == 0)
            {
                personId = _queryProcessor.Execute(new MyPerson(principal)).RevisionId;
            }

            var entity = _queryProcessor.Execute(new AffiliationByPrimaryKey(personId, establishmentId));

            if (entity != null)
            {
                return(true);
            }

            if (principal != null)
            {
                ErrorMessageSource = new StaticStringSource(PrincipalFailMessageFormat);
                context.MessageFormatter.AppendArgument("PropertyValue", principal.Identity.Name);
            }
            else
            {
                ErrorMessageSource = new StaticStringSource(PersonFailMessageFormat);
                context.MessageFormatter.AppendArgument("PropertyValue", personId);
            }
            context.MessageFormatter.AppendArgument("EstablishmentId", establishmentId);
            return(false);
        }
Beispiel #5
0
 public NotEqualValidator(object comparisonValue, IEqualityComparer equalityComparer)
     : base(nameof(Messages.notequal_error), typeof(Messages))
 {
     ErrorCodeSource = new StaticStringSource(ValidationErrors.NotEqual);
     ValueToCompare  = comparisonValue;
     comparer        = equalityComparer;
 }
Beispiel #6
0
 public NotEqualValidator(Func <object, object> func, MemberInfo memberToCompare)
     : base(nameof(Messages.notequal_error), typeof(Messages))
 {
     ErrorCodeSource = new StaticStringSource(ValidationErrors.NotEqual);
     this.func       = func;
     MemberToCompare = memberToCompare;
 }
Beispiel #7
0
        public RegularExpressionValidator(string expression, RegexOptions options) : base(nameof(Messages.regex_error), typeof(Messages))
        {
            ErrorCodeSource = new StaticStringSource(ValidationErrors.RegularExpression);
            this.expression = expression;
            var regex = new Regex(expression, options);

            this.regexFunc = x => regex;
        }
Beispiel #8
0
 protected PropertyValidator(IStringSource errorMessageSource)
 {
     if (errorMessageSource == null)
     {
         errorMessageSource = new StaticStringSource("No default error message has been specified.");
     }
     errorSource = errorMessageSource;
 }
        public InclusiveBetweenValidator(IComparable from, IComparable to) : base(nameof(Messages.inclusivebetween_error), typeof(Messages))
        {
            ErrorCodeSource = new StaticStringSource(ValidationErrors.InclusiveBetween);
            To   = to;
            From = from;

            if (Comparer.GetComparisonResult(to, from) == -1)
            {
                throw new ArgumentOutOfRangeException("to", "To should be larger than from.");
            }
        }
        public LengthValidator(int min, int max, string resourceName, Type resourceType) : base(resourceName, resourceType)
        {
            ErrorCodeSource = new StaticStringSource(ValidationErrors.Length);
            Max             = max;
            Min             = min;

            if (max != -1 && max < min)
            {
                throw new ArgumentOutOfRangeException("max", "Max should be larger than min.");
            }
        }
Beispiel #11
0
 protected PropertyValidator(IStringSource errorMessageSource)
 {
     if (errorMessageSource == null)
     {
         errorMessageSource = new StaticStringSource("No default error message has been specified.");
     }
     else if (errorMessageSource is LanguageStringSource l)
     {
         errorMessageSource = new ErrorCodeLanguageStringSource(ctx => Options.ErrorCodeSource?.GetString(ctx), l);
     }
     Options.ErrorMessageSource = errorMessageSource;
 }
Beispiel #12
0
        protected PropertyValidator(IStringSource errorMessageSource)
        {
            if (errorMessageSource == null)
            {
                errorMessageSource = new StaticStringSource("No default error message has been specified.");
            }
            else if (errorMessageSource is LanguageStringSource l && l.ErrorCodeFunc == null)
            {
                l.ErrorCodeFunc = ctx => ErrorCodeSource?.GetString(ctx);
            }

            ErrorMessageSource = errorMessageSource;
        }
        /// <summary>
        /// Creates a new property rule.
        /// </summary>
        /// <param name="member">Property</param>
        /// <param name="propertyFunc">Function to get the property value</param>
        /// <param name="expression">Lambda expression used to create the rule</param>
        /// <param name="cascadeModeThunk">Function to get the cascade mode.</param>
        /// <param name="typeToValidate">Type to validate</param>
        /// <param name="containerType">Container type that owns the property</param>
        public PropertyRule(MemberInfo member, Func <object, object> propertyFunc, LambdaExpression expression, Func <CascadeMode> cascadeModeThunk, Type typeToValidate, Type containerType)
        {
            Member                = member;
            PropertyFunc          = propertyFunc;
            Expression            = expression;
            OnFailure             = x => { };
            TypeToValidate        = typeToValidate;
            this.cascadeModeThunk = cascadeModeThunk;

            PropertyName = ValidatorOptions.PropertyNameResolver(containerType, member, expression);
            string displayName = ValidatorOptions.DisplayNameResolver(containerType, member, expression);

            if (!string.IsNullOrEmpty(displayName))
            {
                DisplayName = new StaticStringSource(displayName);
            }
        }
        protected override bool IsValid(PropertyValidatorContext context)
        {
            var establishmentId     = _establishmentId((T)context.Instance);
            var principalOrPersonId = context.PropertyValue;
            var principal           = principalOrPersonId as IPrincipal;
            var personId            = principal != null ? 0 : (int)principalOrPersonId;

            var eagerLoad = new Expression <Func <Person, object> >[]
            {
                x => x.Affiliations.Select(y => y.Establishment.Offspring),
            };

            var person = principal != null
                ? _queryProcessor.Execute(new MyPerson(principal) { EagerLoad = eagerLoad, })
                : _queryProcessor.Execute(new PersonById(personId)
            {
                EagerLoad = eagerLoad,
            });

            var offspringOfDefault = person.DefaultAffiliation != null
                ? person.DefaultAffiliation.Establishment.Offspring.FirstOrDefault(x => x.OffspringId == establishmentId)
                : null;

            if (offspringOfDefault != null)
            {
                return(true);
            }

            if (principal != null)
            {
                ErrorMessageSource = new StaticStringSource(PrincipalFailMessageFormat);
                context.MessageFormatter.AppendArgument("PropertyValue", principal.Identity.Name);
            }
            else
            {
                ErrorMessageSource = new StaticStringSource(PersonFailMessageFormat);
                context.MessageFormatter.AppendArgument("PropertyValue", personId);
            }
            context.MessageFormatter.AppendArgument("EstablishmentId", establishmentId);
            return(false);
        }
Beispiel #15
0
        private void Init(int scale, int precision)
        {
            ErrorCodeSource = new StaticStringSource(ValidationErrors.ScalePrecision);
            Scale           = scale;
            Precision       = precision;

            if (Scale < 0)
            {
                throw new ArgumentOutOfRangeException(
                          "scale", string.Format("Scale must be a positive integer. [value:{0}].", Scale));
            }
            if (Precision < 0)
            {
                throw new ArgumentOutOfRangeException(
                          "precision", string.Format("Precision must be a positive integer. [value:{0}].", Precision));
            }
            if (Precision < Scale)
            {
                throw new ArgumentOutOfRangeException(
                          "scale",
                          string.Format("Scale must be less than precision. [scale:{0}, precision:{1}].", Scale, Precision));
            }
        }
Beispiel #16
0
 public NullValidator() : base(nameof(Messages.null_error), typeof(Messages))
 {
     ErrorCodeSource = new StaticStringSource(ValidationErrors.Null);
 }
 public LengthValidator(Func <object, int> min, Func <object, int> max, string resourceName, Type resourceType) : base(resourceName, resourceType)
 {
     ErrorCodeSource = new StaticStringSource(ValidationErrors.Length);
     MaxFunc         = max;
     MinFunc         = min;
 }
Beispiel #18
0
 public EmailValidator() : base(nameof(Messages.email_error), typeof(Messages))
 {
     ErrorCodeSource = new StaticStringSource(ValidationErrors.Email);
     regex           = new Regex(expression, RegexOptions.IgnoreCase);
 }
        public IsValidUserValidator(IMediator mediator) : base(new StaticStringSource(""))
        {
            this.mediator = mediator;

            ErrorCodeSource = new StaticStringSource(nameof(IsValidUserValidator));
        }
Beispiel #20
0
 public EmptyValidator(object defaultValueForType) : base(nameof(Messages.empty_error), typeof(Messages))
 {
     ErrorCodeSource          = new StaticStringSource(ValidationErrors.Empty);
     this.defaultValueForType = defaultValueForType;
 }
Beispiel #21
0
 public EnumValidator(Type enumType) : base(nameof(Messages.enum_error), typeof(Messages))
 {
     ErrorCodeSource = new StaticStringSource(ValidationErrors.Enum);
     this.enumType   = enumType;
 }
 public ExactLengthValidator(int length) : base(length, length, nameof(Messages.exact_length_error), typeof(Messages))
 {
     ErrorCodeSource = new StaticStringSource(ValidationErrors.Length);
 }
Beispiel #23
0
 public LessThanOrEqualValidator(IComparable value)
     : base(value, nameof(Messages.lessthanorequal_error), typeof(Messages))
 {
     ErrorCodeSource = new StaticStringSource(ValidationErrors.LessThanOrEqual);
 }
Beispiel #24
0
 public RegularExpressionValidator(Func <object, string> expression, RegexOptions options)
     : base(nameof(Messages.regex_error), typeof(Messages))
 {
     ErrorCodeSource = new StaticStringSource(ValidationErrors.RegularExpression);
     this.regexFunc  = x => new Regex(expression(x), options);
 }
Beispiel #25
0
 public GreaterThanValidator(Func <object, object> valueToCompareFunc, MemberInfo member)
     : base(valueToCompareFunc, member, nameof(Messages.greaterthan_error), typeof(Messages))
 {
     ErrorCodeSource = new StaticStringSource(ValidationErrors.GreaterThan);
 }
Beispiel #26
0
 public PredicateValidator(Predicate predicate) : base(nameof(Messages.predicate_error), typeof(Messages))
 {
     predicate.Guard("A predicate must be specified.");
     ErrorCodeSource = new StaticStringSource(ValidationErrors.Predicate);
     this.predicate  = predicate;
 }
Beispiel #27
0
        // This logic was taken from the CreditCardAttribute in the ASP.NET MVC3 source.

        public CreditCardValidator() : base(nameof(Messages.CreditCardError), typeof(Messages))
        {
            ErrorCodeSource = new StaticStringSource(ValidationErrors.CreditCard);
        }
Beispiel #28
0
 public LessThanOrEqualValidator(Func <object, object> valueToCompareFunc, MemberInfo member)
     : base(valueToCompareFunc, member, nameof(Messages.lessthanorequal_error), typeof(Messages))
 {
     ErrorCodeSource = new StaticStringSource(ValidationErrors.LessThanOrEqual);
 }
Beispiel #29
0
 public GreaterThanValidator(IComparable value) : base(value, nameof(Messages.greaterthan_error), typeof(Messages))
 {
     ErrorCodeSource = new StaticStringSource(ValidationErrors.GreaterThan);
 }
 public LengthValidator(int min, int max) : this(min, max, nameof(Messages.length_error), typeof(Messages))
 {
     ErrorCodeSource = new StaticStringSource(ValidationErrors.Length);
 }