Ejemplo n.º 1
0
        /// <summary>
        /// Validate the provided value in accordance with the attribute's
        /// policy.
        /// </summary>
        /// <param name="context">Context for validation.</param>
        /// <param name="value">The value to validate.</param>
        /// <param name="reason">On failure, receives a user-readable string
        /// message explaining why the value is not valid.</param>
        /// <returns>True if the value passes validation; false otherwise.
        /// </returns>
        public override bool TryValidate(ArgumentValidationContext context, object value, out string reason)
        {
            if (!string.IsNullOrEmpty(GetString(value)))
            {
                reason = null;
                return(true);
            }

            reason = Strings.StringIsEmpty;
            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Validate the provided value in accordance with the attribute's
        /// policy.
        /// </summary>
        /// <param name="context">Context for validation.</param>
        /// <param name="value">The value to validate.</param>
        /// <param name="reason">On failure, receives a user-readable string
        /// message explaining why the value is not valid.</param>
        /// <returns>True if the value passes validation; false otherwise.
        /// </returns>
        public override bool TryValidate(ArgumentValidationContext context, object value, out string reason)
        {
            if (GetArgumentType(value).IsGreaterThanOrEqualTo(value, Target))
            {
                reason = null;
                return(true);
            }

            reason = string.Format(CultureInfo.CurrentCulture, Strings.ValueIsNotGreaterThanOrEqualTo, Target);
            return(false);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Validate the provided value in accordance with the attribute's
        /// policy.
        /// </summary>
        /// <param name="context">Context for validation.</param>
        /// <param name="value">The value to validate.</param>
        /// <param name="reason">On failure, receives a user-readable string
        /// message explaining why the value is not valid.</param>
        /// <returns>True if the value passes validation; false otherwise.
        /// </returns>
        public override bool TryValidate(ArgumentValidationContext context, object value, out string reason)
        {
            if (!Value.Equals(value))
            {
                reason = null;
                return true;
            }

            reason = string.Format(CultureInfo.CurrentCulture, Strings.ValueMayNotBe, Value);
            return false;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Validate the provided value in accordance with the attribute's
        /// policy.
        /// </summary>
        /// <param name="context">Context for validation.</param>
        /// <param name="value">The value to validate.</param>
        /// <param name="reason">On failure, receives a user-readable string
        /// message explaining why the value is not valid.</param>
        /// <returns>True if the value passes validation; false otherwise.
        /// </returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="context"/>
        /// is null.</exception>
        public override bool TryValidate(ArgumentValidationContext context, object value, out string reason)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var path = (value as FileSystemPath) ?? (string)value;

            var exists = false;

            if (Exists.HasFlag(PathExists.AsFile))
            {
                exists = exists || context.FileSystemReader.FileExists(path);
            }

            if (Exists.HasFlag(PathExists.AsDirectory))
            {
                exists = exists || context.FileSystemReader.DirectoryExists(path);
            }

            if (exists)
            {
                reason = null;
                return(true);
            }

            string reasonMessage;

            switch (Exists)
            {
            case PathExists.AsFile:
                reasonMessage = Strings.FileDoesNotExist;
                break;

            case PathExists.AsDirectory:
                reasonMessage = Strings.DirectoryDoesNotExist;
                break;

            case PathExists.AsFileOrDirectory:
            default:
                reasonMessage = Strings.PathDoesNotExist;
                break;
            }

            reason = string.Format(CultureInfo.CurrentCulture, reasonMessage);
            return(false);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Validate the provided value in accordance with the attribute's
        /// policy.
        /// </summary>
        /// <param name="context">Context for validation.</param>
        /// <param name="value">The value to validate.</param>
        /// <param name="reason">On failure, receives a user-readable string
        /// message explaining why the value is not valid.</param>
        /// <returns>True if the value passes validation; false otherwise.
        /// </returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="context"/>
        /// is null.</exception>
        public override bool TryValidate(ArgumentValidationContext context, object value, out string reason)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var path = (FileSystemPath)value;

            if (!context.FileSystemReader.FileExists(path) && !context.FileSystemReader.DirectoryExists(path))
            {
                reason = null;
                return(true);
            }

            reason = string.Format(CultureInfo.CurrentCulture, Strings.PathExists);
            return(false);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Validate the provided value in accordance with the attribute's
 /// policy.
 /// </summary>
 /// <param name="context">Context for validation.</param>
 /// <param name="value">The value to validate.</param>
 /// <param name="reason">On failure, receives a user-readable string
 /// message explaining why the value is not valid.</param>
 /// <returns>True if the value passes validation; false otherwise.
 /// </returns>
 public abstract bool TryValidate(ArgumentValidationContext context, object value, out string reason);