protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null)
            {
                return(ValidationResult.Success);
            }

            IEnumerable list   = value as IEnumerable;
            var         result = new List <ValidationResult>();

            if (list != null)
            {
                foreach (var element in list)
                {
                    if (element != null)
                    {
                        var context = ValidationContextFactory.Create(element, null);
                        Validator.TryValidateObject(element, context, result, true);
                    }
                }
            }
            else
            {
                var context = ValidationContextFactory.Create(value, null);
                Validator.TryValidateObject(value, context, result, true);
            }

            return(result.FirstOrDefault());
        }
Beispiel #2
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            var result = new List <ValidationResult>();

            if (String.IsNullOrWhiteSpace(Title))
            {
                result.Add(new ValidationResult("Feed must contain a title"));
            }

            if (!UriHasValue(Id))
            {
                result.Add(new ValidationResult("Feed must have an id"));
            }
            else
            if (!Id.IsAbsoluteUri)
            {
                result.Add(new ValidationResult("Feed id must be an absolute URI"));
            }

            if (LastUpdated == null)
            {
                result.Add(new ValidationResult("Feed must have a updated date"));
            }

            if (Links.SearchLink != null)
            {
                result.Add(new ValidationResult("Links with rel='search' can only be used on feed entries"));
            }

            bool feedHasAuthor = !String.IsNullOrEmpty(this.AuthorName);

            if (Entries != null)
            {
                foreach (var entry in Entries.Where(e => e != null))
                {
                    if (!feedHasAuthor && entry is ResourceEntry && String.IsNullOrEmpty(((ResourceEntry)entry).AuthorName))
                    {
                        result.Add(new ValidationResult("Bundle's author and Entry author cannot both be empty"));
                    }

                    Validator.TryValidateObject(entry, ValidationContextFactory.Create(entry, null), result, true);
                }
            }

            return(result);
        }
 public override void SetUp()
 {
     this.target = new ValidationContextFactory();
 }