コード例 #1
0
		private static IEnumerable<ValidationFailure> Validate(object value)
		{
			var parentContext = new ValidationContext(null);
			var rule = new PropertyRule(null, x => value, null, null, typeof(string), null) { PropertyName = "Name" };
			var context = new PropertyValidatorContext(parentContext, rule, null);
			var validator = new OnlyCharactersValidator();
			var result = validator.Validate(context);
			return result;
		}
コード例 #2
0
		public void Should_validate_property_value_without_instance_different_types() {
			var validator = new EqualValidator(100M); // decimal
			var parentContext = new ValidationContext(null);
			var rule = new PropertyRule(null, x => 100D /* double */, null, null, typeof(string), null) {
				PropertyName = "Surname"
			};
			var context = new PropertyValidatorContext(parentContext, rule, null);
			var result = validator.Validate(context); // would fail saying that decimal is not double
			result.Count().ShouldEqual(0);
		}
コード例 #3
0
		public void Should_validate_property_value_without_instance() {
			var validator = new NotNullValidator();
			var parentContext = new ValidationContext(null);
			var rule = new PropertyRule(null, x => null, null, null, typeof(string), null) {
				PropertyName = "Surname"
			};
			var context = new PropertyValidatorContext(parentContext, rule, null);
			var result = validator.Validate(context);
			result.Single().ShouldNotBeNull();
		}
コード例 #4
0
        public override IEnumerable<ModelValidationResult> Validate(object container)
        {
            if (ShouldValidate) {
                var context = new PropertyValidatorContext(Metadata.PropertyName, container, Metadata.Model, Metadata.PropertyName);
                var result = validator.Validate(context);

                foreach (var failure in result) {
                    yield return new ModelValidationResult { Message = failure.ErrorMessage };
                }
            }
        }
		public override IEnumerable<ModelValidationResult> Validate(object container) {
			if (ShouldValidate) {
				var fakeRule = new PropertyRule(null, x => Metadata.Model, null, null, Metadata.ModelType, null) {
					PropertyName = Metadata.PropertyName,
					DisplayName = Rule == null ? null : Rule.DisplayName,
				};

				var fakeParentContext = new ValidationContext(container);
				var context = new PropertyValidatorContext(fakeParentContext, fakeRule, Metadata.PropertyName);
				var result = Validator.Validate(context);

				foreach (var failure in result) {
					yield return new ModelValidationResult { Message = failure.ErrorMessage };
				}
			}
		}
 private bool DoNotHaveRelationWithItself(EditCharacterCommand command, int[] friendIds,
                                          PropertyValidatorContext ctx)
 {
     return(friendIds.All(x => x != command.Id));
 }
コード例 #7
0
 public void Should_validate_property_value_without_instance()
 {
     var validator = new NotNullValidator();
     var context = new PropertyValidatorContext("Surname", null, null as object, null);
     validator.Validate(context).Single().ShouldNotBeNull();
 }
コード例 #8
0
        /// <summary>
        /// Invokes a property validator using the specified validation context.
        /// </summary>
        protected virtual IEnumerable <ValidationFailure> InvokePropertyValidator(ValidationContext context, IPropertyValidator validator, string propertyName)
        {
            var propertyContext = new PropertyValidatorContext(context, this, propertyName);

            return(validator.Validate(propertyContext));
        }
コード例 #9
0
 protected override bool IsValid(PropertyValidatorContext context)
 {
     return(true);
 }
 public MessageBuilderContext(PropertyValidatorContext innerContext, IStringSource errorSource, IPropertyValidator propertyValidator)
 {
     _innerContext     = innerContext;
     ErrorSource       = errorSource;
     PropertyValidator = propertyValidator;
 }
コード例 #11
0
 private void OnError(PersonPutDto obj, PropertyValidatorContext ctx, string message)
 {
     Log.Error("error: {0} - {1} :- {2}", ctx.PropertyName, ctx.PropertyValue, message);
 }
 public Task <IEnumerable <ValidationFailure> > ValidateAsync(PropertyValidatorContext context, CancellationToken cancellation) => Task.FromResult(Enumerable.Empty <ValidationFailure> ( ));
コード例 #13
0
 public IEnumerable <ValidationFailure> Validate(PropertyValidatorContext context)
 {
     return(Enumerable.Empty <ValidationFailure>());
 }
コード例 #14
0
        protected override bool IsValid(PropertyValidatorContext context)
        {
            var ageGenderViewModel = context.Instance as AgeGenderViewModel;

            return(IsAValidAge(ageGenderViewModel.Age));
        }
 public IEnumerable <ValidationFailure> Validate(PropertyValidatorContext context) => Enumerable.Empty <ValidationFailure> ( );
コード例 #16
0
 protected override bool IsValid(PropertyValidatorContext context)
 {
     return(context.PropertyValue is IList <InvoiceItemVm> list && list.Any());
 }
コード例 #17
0
 /// <summary>
 /// Is valid?
 /// </summary>
 /// <param name="context">Validation context</param>
 /// <returns>Result</returns>
 protected override bool IsValid(PropertyValidatorContext context)
 {
     return(IsValid(context.PropertyValue as string, _customerSettings));
 }
コード例 #18
0
        protected override bool IsValid(PropertyValidatorContext context)
        {
            var personalInfoAddressViewModel = context.Instance as FindServicesAddressViewModel;

            return(IsAValidPostcode(personalInfoAddressViewModel.Postcode));
        }
コード例 #19
0
ファイル: Validators.cs プロジェクト: developeramarish/vabank
        private bool GreaterThanOrEqualToMinimumAmount(TCommand command, decimal amount, PropertyValidatorContext context)
        {
            var userCard = _userCardRepository.SurelyFind(command.FromCardId);

            if (userCard.Account == null)
            {
                throw new InvalidOperationException("User card is not bound to the account.");
            }
            var minimal = _settings.MinimalAmounts.ContainsKey(userCard.Account.Currency.ISOName)
                ? _settings.MinimalAmounts[userCard.Account.Currency.ISOName]
                : 0;

            if (amount < minimal)
            {
                context.MessageFormatter.AppendArgument("MinimalAmount", minimal);
                context.MessageFormatter.AppendArgument("CurrencyISOName", userCard.Account.Currency.ISOName);
                return(false);
            }
            return(true);
        }
コード例 #20
0
 public Task <IEnumerable <ValidationFailure> > ValidateAsync(PropertyValidatorContext context, CancellationToken cancellation)
 {
     return(Validate(context).AsTaskResult());
 }
コード例 #21
0
 protected override bool IsValid(GreenEnergyHub.Messaging.MessageTypes.Common.MarketParticipant propertyValue, PropertyValidatorContext context)
 {
     return(!string.IsNullOrEmpty(propertyValue?.Qualifier) &&
            (IsValidCvr(propertyValue) || IsValidCpr(propertyValue)));
 }
コード例 #22
0
 protected override bool IsValid(PropertyValidatorContext context)
 {
     return(context.PropertyValue is string value &&
            Uri.IsWellFormedUriString(value, UriKind.RelativeOrAbsolute));
 }
コード例 #23
0
        protected override bool IsValid(PropertyValidatorContext context)
        {
            var designatedSKU = context.PropertyValue as string;

            return(string.IsNullOrEmpty(designatedSKU) || _itemService.GetItemBySKU(designatedSKU) != null);
        }
コード例 #24
0
 private static bool HaveQueenslandLandLine(UpdateCustomerCommand model, string phoneValue, PropertyValidatorContext ctx)
 {
     return(model.Phone.StartsWith("07") || model.Fax.StartsWith("07"));
 }
コード例 #25
0
        protected override bool IsValid(PropertyValidatorContext context)
        {
            var    cpf = context.PropertyValue as string;
            string tempCpf, digit;

            int[] multiplicador1 = new int[9] {
                10, 9, 8, 7, 6, 5, 4, 3, 2
            };
            int[] multiplicador2 = new int[10] {
                11, 10, 9, 8, 7, 6, 5, 4, 3, 2
            };
            int sum, rest;

            if (string.IsNullOrWhiteSpace(cpf))
            {
                return(false);
            }

            cpf = cpf.Trim();
            cpf = cpf.Replace(".", "").Replace("-", "");

            if (cpf.Length != 11)
            {
                return(false);
            }

            try
            {
                long.Parse(cpf);
            }
            catch
            {
                return(false);
            }

            tempCpf = cpf.Substring(0, 9);
            sum     = 0;

            for (int i = 0; i < 9; i++)
            {
                sum += int.Parse(tempCpf[i].ToString()) * multiplicador1[i];
            }

            rest = sum % 11;

            if (rest < 2)
            {
                rest = 0;
            }
            else
            {
                rest = 11 - rest;
            }

            digit   = rest.ToString();
            tempCpf = tempCpf + digit;
            sum     = 0;

            for (int i = 0; i < 10; i++)
            {
                sum += int.Parse(tempCpf[i].ToString()) * multiplicador2[i];
            }

            rest = sum % 11;
            if (rest < 2)
            {
                rest = 0;
            }
            else
            {
                rest = 11 - rest;
            }

            digit = digit + rest.ToString();
            return(cpf.EndsWith(digit));
        }
コード例 #26
0
 private async Task <bool> CheckCodeUnique(UpdateProductInputModel model, string value, PropertyValidatorContext context, CancellationToken cancellationToken)
 {
     return(await _catalogContext.Products
            .AllAsync(
                x => x.Id == model.Id || x.Code != value,
                cancellationToken));
 }
コード例 #27
0
 protected override bool IsValid(PropertyValidatorContext context)
 {
     return(CpfCnpjUtils.IsCpf(context.PropertyValue.ToString()));
 }
 private bool BeUnique(EditCharacterCommand command, string name, PropertyValidatorContext ctx)
 {
     return(_context.Characters.Where(x => x.Id != command.Id).All(x => x.Name != command.Name));
 }
コード例 #29
0
        protected override bool IsValid(PropertyValidatorContext context)
        {
            var date = context.PropertyValue;

            return(!date.Equals(default(DateTime)));
        }
コード例 #30
0
		protected override bool IsValid(PropertyValidatorContext context)
		{
			return
				context.PropertyValue == null || ((string)context.PropertyValue).All(x => char.IsLetter(x) || x == ' ');
		}
コード例 #31
0
        protected override bool IsValid(PropertyValidatorContext context)
        {
            var state = context.PropertyValue.ChangeType <bool?>();

            return(state.HasValue && state.Value);
        }
コード例 #32
0
 protected override bool IsValid(PropertyValidatorContext context)
 {
     return true;
 }
コード例 #33
0
        protected override bool IsValid(PropertyValidatorContext context)
        {
            var value = context.PropertyValue as IFormFile;

            return(_fileSize <= value.Length);
        }
 private bool HaveAtLeastOneEpisode(EditCharacterCommand command, int[] episodeIds, PropertyValidatorContext ctx)
 {
     return(_context.Episodes.Any(x => episodeIds.Contains(x.Id)));
 }
コード例 #35
0
 public IEnumerable<ValidationFailure> Validate(PropertyValidatorContext context)
 {
     return Enumerable.Empty<ValidationFailure>();
 }
コード例 #36
0
        protected override bool IsValid(PropertyValidatorContext context)
        {
            var value = Convert.ToInt32(context.PropertyValue);

            return(Grade.IsValid(value));
        }
コード例 #37
0
 // Server side validation
 protected override bool IsValid(PropertyValidatorContext context)
 {
     // Instance of the class which contains property which must be validated 
     var phone = context.ParentContext.InstanceToValidate as PhoneDetail;
コード例 #38
0
        protected virtual bool IsUnique <TProperty>(T instance, TProperty propertyValue, PropertyValidatorContext context)
        {
            if (propertyValue == null)
            {
                return(true);
            }
            bool isUnique = !Repository.Any(x => {
                if (!x.Equals(instance))
                {
                    object value = x.GetType().GetProperty(context.PropertyName, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public).GetValue(x);
                    if (value is TProperty val)
                    {
                        bool isEqual = val.Equals(propertyValue);
                        return(isEqual);
                    }
                }
                return(false);
            });

            return(isUnique);
        }
コード例 #39
0
        /// <summary>
        /// Validates the specified property on the instance.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns>The validation errors.</returns>
        public IEnumerable<IError> Validate(object instance, string propertyName)
        {
            var instanceValidator = CreateValidatorForInstance(instance);
            if(instanceValidator == null)
                yield break;

            var descriptor = instanceValidator.CreateDescriptor();
            var propertyValue = instance.GetType().GetProperty(propertyName).GetValue(instance, null);
            var validators = descriptor.GetValidatorsForMember(propertyName);
            var context = new PropertyValidatorContext(propertyName, instance, propertyValue, propertyName);

            foreach(var validator in validators)
            {
                var failures = validator.Validate(context);

                foreach(var failure in failures)
                {
                    yield return new ErrorAdapter(instance, failure);
                }
            }
        }
コード例 #40
0
        protected override bool IsValid(PropertyValidatorContext context)
        {
            var postcode = context.PropertyValue as string;

            return(postcode?.Length == 6);
        }