protected override bool IsValid(PropertyValidatorContext context) { var value = context.PropertyValue as string; if (value == null) { return true; } value = value.Replace("-", ""); int checksum = 0; bool evenDigit = false; // http://www.beachnet.com/~hstiles/cardtype.html foreach (char digit in value.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; }
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; var predicate = Predicate ?? (x => true); foreach (var element in collection) { if(element == null || !(predicate(element))) { // 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; } } }
private IComparable GetComparisonValue(PropertyValidatorContext context) { if(valueToCompareFunc != null) { return (IComparable)valueToCompareFunc(context.Instance); } return (IComparable)ValueToCompare; }
protected override bool IsValid(PropertyValidatorContext context) { if (context.PropertyValue == null) return true; if (!regex.IsMatch((string)context.PropertyValue)) { return false; } return true; }
public virtual IEnumerable<ValidationFailure> Validate(PropertyValidatorContext context) { context.MessageFormatter.AppendPropertyName(context.PropertyDescription); if (!IsValid(context)) { return new[] { CreateValidationError(context) }; } return Enumerable.Empty<ValidationFailure>(); }
protected override bool IsValid(PropertyValidatorContext context) { var comparisonValue = GetComparisonValue(context); bool success = !Compare(comparisonValue, context.PropertyValue); if (!success) { context.MessageFormatter.AppendArgument("PropertyValue", context.PropertyValue); return false; } return true; }
protected sealed override bool IsValid(PropertyValidatorContext context) { if(context.PropertyValue == null) { // If we're working with a nullable type then this rule should not be applied. // If you want to ensure that it's never null then a NotNull rule should also be applied. return true; } var value = GetComparisonValue(context); if (!IsValid((IComparable)context.PropertyValue, value)) { context.MessageFormatter.AppendArgument("ComparisonValue", value); return false; } return true; }
protected override bool IsValid(PropertyValidatorContext context) { var propertyValue = (IComparable)context.PropertyValue; // If the value is null then we abort and assume success. // This should not be a failure condition - only a NotNull/NotEmpty should cause a null to fail. if (propertyValue == null) return true; if (propertyValue.CompareTo(From) < 0 || propertyValue.CompareTo(To) > 0) { context.MessageFormatter .AppendArgument("From", From) .AppendArgument("To", To) .AppendArgument("Value", context.PropertyValue); 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 instanceToValidate = context.PropertyValue; if (instanceToValidate == null) { return Enumerable.Empty<ValidationFailure>(); } var validator = GetValidator(context); if(validator == null) { return Enumerable.Empty<ValidationFailure>(); } var newContext = CreateNewValidationContextForChildValidator(instanceToValidate, context); var results = validator.Validate(newContext).Errors; return results; }
public abstract IEnumerable<ValidationFailure> Validate(PropertyValidatorContext context);
protected ValidationContext CreateNewValidationContextForChildValidator(object instanceToValidate, PropertyValidatorContext context) { var newContext = context.ParentContext.CloneForChildValidator(instanceToValidate); newContext.PropertyChain.Add(context.Rule.Member); return newContext; }
protected abstract bool IsValid(PropertyValidatorContext context);
/// <summary> /// Creates an error validation result for this validator. /// </summary> /// <param name="context">The validator context</param> /// <returns>Returns an error validation result.</returns> protected virtual ValidationFailure CreateValidationError(PropertyValidatorContext context) { context.MessageFormatter.AppendAdditionalArguments( customFormatArgs.Select(func => func(context.Instance)).ToArray() ); string error = context.MessageFormatter.BuildMessage(errorSource.GetString()); var failure = new ValidationFailure(context.PropertyName, error, errorCode, context.PropertyValue); if (CustomStateProvider != null) { failure.CustomState = CustomStateProvider(context.Instance); } return failure; }
protected virtual IValidator GetValidator(PropertyValidatorContext context) { return Validator; }
public IEnumerable<ValidationFailure> Validate(PropertyValidatorContext context) { if (condition(context.Instance)) { return InnerValidator.Validate(context); } return Enumerable.Empty<ValidationFailure>(); }