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; }
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); }
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(); }
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)); }
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(); }
/// <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)); }
protected override bool IsValid(PropertyValidatorContext context) { return(true); }
public MessageBuilderContext(PropertyValidatorContext innerContext, IStringSource errorSource, IPropertyValidator propertyValidator) { _innerContext = innerContext; ErrorSource = errorSource; PropertyValidator = propertyValidator; }
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> ( ));
public IEnumerable <ValidationFailure> Validate(PropertyValidatorContext context) { return(Enumerable.Empty <ValidationFailure>()); }
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> ( );
protected override bool IsValid(PropertyValidatorContext context) { return(context.PropertyValue is IList <InvoiceItemVm> list && list.Any()); }
/// <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)); }
protected override bool IsValid(PropertyValidatorContext context) { var personalInfoAddressViewModel = context.Instance as FindServicesAddressViewModel; return(IsAValidPostcode(personalInfoAddressViewModel.Postcode)); }
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); }
public Task <IEnumerable <ValidationFailure> > ValidateAsync(PropertyValidatorContext context, CancellationToken cancellation) { return(Validate(context).AsTaskResult()); }
protected override bool IsValid(GreenEnergyHub.Messaging.MessageTypes.Common.MarketParticipant propertyValue, PropertyValidatorContext context) { return(!string.IsNullOrEmpty(propertyValue?.Qualifier) && (IsValidCvr(propertyValue) || IsValidCpr(propertyValue))); }
protected override bool IsValid(PropertyValidatorContext context) { return(context.PropertyValue is string value && Uri.IsWellFormedUriString(value, UriKind.RelativeOrAbsolute)); }
protected override bool IsValid(PropertyValidatorContext context) { var designatedSKU = context.PropertyValue as string; return(string.IsNullOrEmpty(designatedSKU) || _itemService.GetItemBySKU(designatedSKU) != null); }
private static bool HaveQueenslandLandLine(UpdateCustomerCommand model, string phoneValue, PropertyValidatorContext ctx) { return(model.Phone.StartsWith("07") || model.Fax.StartsWith("07")); }
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)); }
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)); }
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)); }
protected override bool IsValid(PropertyValidatorContext context) { var date = context.PropertyValue; return(!date.Equals(default(DateTime))); }
protected override bool IsValid(PropertyValidatorContext context) { return context.PropertyValue == null || ((string)context.PropertyValue).All(x => char.IsLetter(x) || x == ' '); }
protected override bool IsValid(PropertyValidatorContext context) { var state = context.PropertyValue.ChangeType <bool?>(); return(state.HasValue && state.Value); }
protected override bool IsValid(PropertyValidatorContext context) { return true; }
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))); }
public IEnumerable<ValidationFailure> Validate(PropertyValidatorContext context) { return Enumerable.Empty<ValidationFailure>(); }
protected override bool IsValid(PropertyValidatorContext context) { var value = Convert.ToInt32(context.PropertyValue); return(Grade.IsValid(value)); }
// 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;
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); }
/// <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); } } }
protected override bool IsValid(PropertyValidatorContext context) { var postcode = context.PropertyValue as string; return(postcode?.Length == 6); }