protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) return true; var min = Min; var max = Max; if (MaxFunc != null && MinFunc != null) { max = MaxFunc(context.Instance); min = MinFunc(context.Instance); } int length = context.PropertyValue.ToString().Length; if (length < min || (length > max && max != -1)) { context.MessageFormatter .AppendArgument("MinLength", min) .AppendArgument("MaxLength", max) .AppendArgument("TotalLength", length); return false; } return true; }
protected ValidationContext CreateNewValidationContextForChildValidator(object instanceToValidate, PropertyValidatorContext propertyValidatorContext, IValidatorSelector validatorSelector) { var propertyChain = new PropertyChain(propertyValidatorContext.PropertyChain); propertyChain.Add(propertyValidatorContext.Member); return new ValidationContext(instanceToValidate, propertyChain, validatorSelector); }
private IComparable GetComparisonValue(PropertyValidatorContext context) { if(valueToCompareFunc != null) { return (IComparable)valueToCompareFunc(context.Instance); } return (IComparable)ValueToCompare; }
protected override bool IsValid(PropertyValidatorContext context) { var value = context.PropertyValue as string; if (value == null) { return true; } value = value.Replace("-", "").Replace(" ", ""); int checksum = 0; bool evenDigit = false; // http://www.beachnet.com/~hstiles/cardtype.html foreach (char digit in value.ToCharArray().Reverse()) { if (!char.IsDigit(digit)) { return false; } int digitValue = (digit - '0') * (evenDigit ? 2 : 1); evenDigit = !evenDigit; while (digitValue > 0) { checksum += digitValue % 10; digitValue /= 10; } } return (checksum % 10) == 0; }
protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue != null) return (bool)context.PropertyValue; return false; }
protected override bool IsValid(PropertyValidatorContext context) { var value = context.PropertyValue as string; if (String.IsNullOrEmpty(value)) return false; return value.Length == 24; }
protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) { return false; } return true; }
protected override bool IsValid(PropertyValidatorContext context) { var ccValue = context.PropertyValue as string; if (String.IsNullOrWhiteSpace(ccValue)) return false; ccValue = ccValue.Replace(" ", ""); ccValue = ccValue.Replace("-", ""); int checksum = 0; bool evenDigit = false; //http://www.beachnet.com/~hstiles/cardtype.html foreach (char digit in ccValue.Reverse()) { if (!Char.IsDigit(digit)) return false; int digitValue = (digit - '0') * (evenDigit ? 2 : 1); evenDigit = !evenDigit; while (digitValue > 0) { checksum += digitValue % 10; digitValue /= 10; } } return (checksum % 10) == 0; }
protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue != null && !regex.IsMatch((string)context.PropertyValue)) { return false; } return true; }
protected override bool IsValid(PropertyValidatorContext context) { #if PORTABLE40 return IsValidAsync(context, new CancellationToken()).Result; #else return Task.Run(() => IsValidAsync(context, new CancellationToken())).GetAwaiter().GetResult(); #endif }
protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) return false; if (OsInfo.IsNotWindows) return true; if (!_runtimeInfo.IsWindowsService) return true; var path = context.PropertyValue.ToString(); if (!DriveRegex.IsMatch(path)) return true; var drives = _diskProvider.GetDrives(); foreach (var drive in drives) { if (path.StartsWith(drive.Name, StringComparison.InvariantCultureIgnoreCase)) { if (drive.DriveType == DriveType.Network) { return false; } } } return true; }
private object GetComparisonValue(PropertyValidatorContext context) { if(func != null) { return func(context.Instance); } return ValueToCompare; }
protected override bool IsValid(PropertyValidatorContext context) { if (!predicate(context.Instance, context.PropertyValue, context)) { return false; } return true; }
public override IEnumerable<ValidationFailure> Validate(PropertyValidatorContext context) { if (context.Rule.Member == null) { throw new InvalidOperationException(string.Format("Nested validators can only be used with Member Expressions.")); } var collection = context.PropertyValue as IEnumerable; if (collection == null) { yield break; } int count = 0; foreach (var element in collection) { if(element == null) { // If an element in the validator is null then we want to skip it to prevent NullReferenceExceptions in the child validator. // We still need to update the counter to ensure the indexes are correct. count++; continue; } var newContext = context.ParentContext.CloneForChildValidator(element); newContext.PropertyChain.Add(context.Rule.Member); newContext.PropertyChain.AddIndexer(count++); var results = childValidator.Validate(newContext).Errors; foreach (var result in results) { yield return result; } } }
protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) return true; bool fail = false; string stringTarget = Convert.ToString(context.PropertyValue); switch (typeName) { case ValidatableTypes.number: re = new Regex(@"^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$"); break; case ValidatableTypes.email: re = new Regex(@"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$"); break; case ValidatableTypes.date: DateTime d; fail = !DateTime.TryParse(stringTarget, out d); break; case ValidatableTypes.dateISO: re = new Regex(@"^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$"); break; case ValidatableTypes.digits: re = new Regex(@"^\d+$"); break; case ValidatableTypes.url: re = new Regex(@"^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$"); break; } if (re != null) fail = !re.IsMatch(stringTarget); return !fail; }
protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) return true; var tvdbId = Convert.ToInt32(context.PropertyValue.ToString()); return (!_seriesService.GetAllSeries().Exists(s => s.TvdbId == tvdbId)); }
public virtual IEnumerable<ValidationFailure> Validate(PropertyValidatorContext context) { if (!IsValid(context)) { PrepareMessageFormatterForValidationError(context); return new[] { CreateValidationError(context) }; } return Enumerable.Empty<ValidationFailure>(); }
protected override bool IsValid(PropertyValidatorContext context) { var value = context.PropertyValue as string; if (String.IsNullOrEmpty(value)) return false; ObjectId id; return ObjectId.TryParse(context.PropertyValue as string, out id); }
protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) return false; if (!context.PropertyValue.ToString().Contains("@")) return false; return true; }
protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) return false; var email = context.PropertyValue.ToString(); context.MessageFormatter.AppendArgument("EmailDomain", _emailDomain); return _regex.IsMatch(email); }
protected override bool IsValid(PropertyValidatorContext context) { IApplicationDbContext dbContext = dbContextFunction(); string emailAddress = context.PropertyValue as string; ApplicationUser user = dbContext.Users.FirstOrDefault(u => u.Email == emailAddress); return user == null; }
protected override bool IsValid(PropertyValidatorContext context) { var regex = regexFunc(context.Instance); if (regex != null && context.PropertyValue != null && !regex.IsMatch((string) context.PropertyValue)) { context.MessageFormatter.AppendArgument("RegularExpression", regex.ToString()); return false; } return true; }
protected override bool IsValid(PropertyValidatorContext context) { var password = context.PropertyValue as string; return !string.IsNullOrWhiteSpace(password) && password.Length >= 3 && password.Length <= 25; }
protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) return true; dynamic instance = context.ParentContext.InstanceToValidate; var instanceId = (int)instance.Id; return (!_seriesService.GetAllSeries().Exists(s => s.Path.PathEquals(context.PropertyValue.ToString()) && s.Id != instanceId)); }
protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null || context.PropertyValue.Equals(string.Empty) || Equals(context.PropertyValue, defaultValueForType)) { return false; } return true; }
protected override bool IsValid(PropertyValidatorContext context) { string iban = context.PropertyValue as string; var status = Iban.CheckIban(iban, true); bool result = status.IsValid; if(!result) ErrorMessageSource = (IStringSource)new StaticStringSource(status.Message); return result; }
public virtual IEnumerable<ValidationFailure> Validate(PropertyValidatorContext context) { context.MessageFormatter.AppendPropertyName(context.PropertyDescription); context.MessageFormatter.AppendArgument("PropertyValue", context.PropertyValue); if (!IsValid(context)) { return new[] { CreateValidationError(context) }; } return Enumerable.Empty<ValidationFailure>(); }
protected override bool IsValid(PropertyValidatorContext context) { var dt = (DateTime)context.PropertyValue; var age = dt.AgeAt(); context.MessageFormatter.AppendArgument("ExpectedAge", _expectedAge); context.MessageFormatter.AppendArgument("Age", age); context.MessageFormatter.AppendArgument("ComparisonDate", DateTime.UtcNow.Date); return age >= _expectedAge; }
protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null || IsInvalidString(context.PropertyValue) || IsEmptyCollection(context.PropertyValue) || Equals(context.PropertyValue, defaultValueForType)) { return false; } return true; }
protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) return false; var droneFactory = _configService.DownloadedEpisodesFolder; if (string.IsNullOrWhiteSpace(droneFactory)) return true; return !droneFactory.PathEquals(context.PropertyValue.ToString()); }
public virtual IValidator <TProperty> GetValidator(PropertyValidatorContext context) { context.Guard("Cannot pass a null context to GetValidator", nameof(context)); return(_validatorProvider != null?_validatorProvider(context) : _validator); }
/// <summary> /// Creates a new CustomContext /// </summary> /// <param name="context">The parent PropertyValidatorContext that represents this execution</param> public CustomContext(PropertyValidatorContext context) { _context = context; }
protected override Task <bool> IsValidAsync(PropertyValidatorContext context, CancellationToken cancellation) { return(predicate(context.Instance, context.PropertyValue, context, cancellation)); }
public abstract IEnumerable <ValidationFailure> Validate(PropertyValidatorContext context);
public virtual async Task <IEnumerable <ValidationFailure> > ValidateAsync(PropertyValidatorContext context, CancellationToken cancellation) { return(Validate(context)); }
protected override bool IsValid(PropertyValidatorContext context) { //TODO: For FV 9, throw an exception by default if async validator is being executed synchronously. return(Task.Run(() => IsValidAsync(context, new CancellationToken())).GetAwaiter().GetResult()); }
#pragma warning disable 1998 protected virtual async Task <bool> IsValidAsync(PropertyValidatorContext context, CancellationToken cancellation) { return(IsValid(context)); }
protected abstract bool IsValid(PropertyValidatorContext context);
protected override bool IsValid(PropertyValidatorContext context) { throw new NotImplementedException(); }
protected ValidationContext CreateNewValidationContextForChildValidator(object instanceToValidate, PropertyValidatorContext context) { var newContext = context.ParentContext.CloneForChildValidator(instanceToValidate); if (!context.ParentContext.IsChildCollectionContext) { newContext.PropertyChain.Add(context.Rule.PropertyName); } return(newContext); }
protected IValidationContext CreateNewValidationContextForChildValidator(object instanceToValidate, PropertyValidatorContext context) { var selector = RuleSets?.Length > 0 ? new RulesetValidatorSelector(RuleSets) : null; var parentContext = ValidationContext <T> .GetFromNonGenericContext(context.ParentContext); var newContext = parentContext.CloneForChildValidator((TProperty)instanceToValidate, PassThroughParentContext, selector); if (!parentContext.IsChildCollectionContext) { newContext.PropertyChain.Add(context.Rule.PropertyName); } return(newContext); }
public virtual IValidator GetValidator(PropertyValidatorContext context) { context.Guard("Cannot pass a null context to GetValidator", nameof(context)); return(validatorProvider(context.Instance)); }
/// <summary> /// Verify that the received property represents a valid document. /// </summary> /// <param name="context">rules context.</param> /// <param name="cancellation">cancelation task.</param> /// <returns>execution task.</returns> protected override Task <bool> IsValidAsync(PropertyValidatorContext context, CancellationToken cancellation) { return(new Task <bool>(() => { return IsValid(context); }, cancellation)); }
protected abstract override Task <bool> IsValidAsync(PropertyValidatorContext context, CancellationToken cancellation);
#pragma warning restore 1998 /// <summary> /// Prepares the <see cref="MessageFormatter"/> of <paramref name="context"/> for an upcoming <see cref="ValidationFailure"/>. /// </summary> /// <param name="context">The validator context</param> protected virtual void PrepareMessageFormatterForValidationError(PropertyValidatorContext context) { context.MessageFormatter.AppendPropertyName(context.DisplayName); context.MessageFormatter.AppendPropertyValue(context.PropertyValue); }
protected override bool IsValid(PropertyValidatorContext context) { return(IsValidAsync(context, new CancellationToken()).Result); }