private bool IsEmptyString(PropertyValidatorContext context)
        {
            if (context.PropertyValue is string)
                return string.IsNullOrWhiteSpace(context.PropertyValue as string);

            return false;
        }
        /// <summary>
        /// Returns all the validation errors for the specified context
        /// </summary>
        /// <param name="context">The context to validate</param>
        /// <returns>Lit of all validation errors</returns>
        public IEnumerable<ValidationError> Validate(
            PropertyValidatorContext context)
        {
            if (!IsValid(context))
                return new[] { CreateValidationError(context) };

            return Enumerable.Empty<ValidationError>();
        }
        /// <summary>
        /// Returns whether or not the property value is valid comparing somehow
        /// with valueToCompare
        /// </summary>
        /// <param name="context">The context</param>
        /// <returns>True if property value is valid; false, otherwise</returns>
        protected override bool IsValid(PropertyValidatorContext context)
        {
            // If we're working with a nullable type then this rule should not be applied.
            if (context.PropertyValue.IsNull())
                return true;

            return IsValid((IComparable)context.PropertyValue, ValueToCompare);
        }
        /// <summary>
        /// Returns whether or not the property value is not empty
        /// </summary>
        /// <param name="context">The context</param>
        /// <returns>True when property value is not empty; false, otherwise</returns>
        protected override bool IsValid(PropertyValidatorContext context)
        {
            if (context.PropertyValue.IsNull() ||
                IsEmptyString(context) ||
                IsEmptyCollection(context))
                return false;

            return true;
        }
        /// <summary>
        /// Returns whether or not the property value length is between
        /// minimumLength and maximumLength
        /// </summary>
        /// <param name="context">The context</param>
        /// <returns>
        /// True when property value length is between minimum length and maximum length;
        /// false, otherwise</returns>
        protected override bool IsValid(PropertyValidatorContext context)
        {
            if (context.PropertyValue.IsNull() || !(context.PropertyValue is string))
                return true;

            var length = context.PropertyValue.ToString().Length;

            return length >= MinimunLength &&
                (MaximumLength == -1 || length <= MaximumLength);
        }
Beispiel #6
0
 /// <summary>
 /// Returns whether or not the property vaue is equal to valueToCompare
 /// </summary>
 /// <param name="context">The context</param>
 /// <returns>True if property value is equal to valueToCompare; false, otherwise</returns>
 protected override bool IsValid(PropertyValidatorContext context)
 {
     return Compare(context.PropertyValue, ValueToCompare);
 }
 /// <summary>
 /// Returns an instance of ValidationError
 /// </summary>
 /// <param name="context">The context</param>
 /// <returns>An instance of ValidationError</returns>
 protected virtual ValidationError CreateValidationError(
     PropertyValidatorContext context)
 {
     return new ValidationError(context.PropertyName, context.PropertyValue, ErrorMessage);
 }
 /// <summary>
 /// Returns whether or not the property validator context is valid
 /// </summary>
 /// <param name="context">The context</param>
 /// <returns>True if context is valid; false, otherwise</returns>
 protected abstract bool IsValid(PropertyValidatorContext context);
        private bool IsEmptyCollection(PropertyValidatorContext context)
        {
            var collection = context.PropertyValue as IEnumerable;

            return collection != null && !collection.Cast<object>().Any();
        }
 /// <summary>
 /// Returns whether or not the property value mathes the regular expression
 /// </summary>
 /// <param name="context"></param>
 /// <returns></returns>
 protected override bool IsValid(PropertyValidatorContext context)
 {
     return context.PropertyValue.IsNull() ||
         (context.PropertyValue is string && _regex.IsMatch(context.PropertyValue.ToString()));
 }
 /// <summary>
 /// Returns whether or not the property is valid based on nullability condition
 /// </summary>
 /// <param name="context">The property context</param>
 /// <returns></returns>
 protected override bool IsValid(PropertyValidatorContext context)
 {
     return context.PropertyValue != null;
 }