Exemplo n.º 1
0
        protected override ValidationResult IsValidInternal(object value, ValidationContext validationContext)
        {
            if (value != null)
            {
                Compile(validationContext.ObjectType);
                if (!CachedValidationFuncs[validationContext.ObjectType](validationContext.ObjectInstance)) // check if the assertion condition is not satisfied
                {
                    return(new ValidationResult(                                                            // assertion not satisfied => notify
                               FormatErrorMessage(validationContext.DisplayName, Expression, validationContext.ObjectInstance),
                               new[] { validationContext.MemberName }));
                }
            }

            return(ValidationResult.Success);
        }
        protected override ValidationResult IsValidInternal(object value, ValidationContext validationContext)
        {
            var isEmpty = value is string && string.IsNullOrWhiteSpace((string)value);

            if (value == null || (isEmpty && !AllowEmptyStrings))
            {
                Compile(validationContext.ObjectType);
                if (CachedValidationFuncs[validationContext.ObjectType](validationContext.ObjectInstance)) // check if the requirement condition is satisfied
                {
                    return(new ValidationResult(                                                           // requirement confirmed => notify
                               FormatErrorMessage(validationContext.DisplayName, Expression, validationContext.ObjectInstance),
                               new[] { validationContext.MemberName }));
                }
            }

            return(ValidationResult.Success);
        }
        protected override ValidationResult IsValidInternal(object value, ValidationContext validationContext)
        {
            if ((PropertyType != null) && (PropertyType.Name == "String") && AllowEmptyStrings)
            {
                return(ValidationResult.Success);
            }
            else if (value == null)
            {
                Compile(validationContext.ObjectType);
                if (CachedValidationFuncs[validationContext.ObjectType](validationContext.ObjectInstance)) // check if the requirement condition is satisfied
                {
                    return(new ValidationResult(                                                           // requirement confirmed => notify
                               FormatErrorMessage(validationContext.DisplayName, Expression, validationContext.ObjectInstance),
                               new[] { validationContext.MemberName }));
                }
            }

            return(ValidationResult.Success);
        }
Exemplo n.º 4
0
 /// <summary>
 ///     Parses and compiles expression provided to the attribute. Compiled lambda is then cached and used for validation purposes.
 /// </summary>
 /// <param name="validationContextType">The type of the object to be validated.</param>
 /// <param name="force">Flag indicating whether parsing should be rerun despite the fact compiled lambda already exists.</param>
 /// <exception cref="ParseErrorException"></exception>
 public void Compile(Type validationContextType, bool force = false)
 {
     if (force)
     {
         CachedValidationFuncs[validationContextType] = Parser.Parse <bool>(validationContextType, Expression);
         return;
     }
     if (!CachedValidationFuncs.ContainsKey(validationContextType))
     {
         CachedValidationFuncs[validationContextType] = Parser.Parse <bool>(validationContextType, Expression);
     }
 }
Exemplo n.º 5
0
 /// <summary>
 ///     Parses and compiles expression provided to the attribute. Compiled lambda is then cached and used for validation purposes.
 /// </summary>
 /// <param name="validationContextType">The type of the object to be validated.</param>
 /// <param name="action">The action to be invoked after compilation is done. <see cref="Parser" /> object, containing all of the computed data, is given as an input parameter.</param>
 /// <param name="force">Flag indicating whether parsing should be invoked again despite the fact compiled lambda already exists.</param>
 /// <exception cref="ParseErrorException"></exception>
 public void Compile(Type validationContextType, Action <Parser> action = null, bool force = false)
 {
     if (force)
     {
         CachedValidationFuncs[validationContextType] = Parser.Parse <bool>(validationContextType, Expression);
         action?.Invoke(Parser);
         return;
     }
     if (!CachedValidationFuncs.ContainsKey(validationContextType))
     {
         CachedValidationFuncs[validationContextType] = Parser.Parse <bool>(validationContextType, Expression);
     }
     action?.Invoke(Parser);
 }