/// <summary>
        /// Creates a condensed copy of the result.
        /// </summary>
        public SchemaValidationResults Condense()
        {
            var children = NestedResults.Where(r => r.RelativeLocation != null && !r.RecursionDetected)
                           .Select(r => r.Condense())
                           .Where(r => (!IsValid && !r.IsValid) || (IsValid && r.AnnotationValue != null) || r.NestedResults.Any())
                           .Distinct()
                           .ToList();

            var copy = new SchemaValidationResults();

            copy._CopyDataFrom(this);
            copy.NestedResults = children;

            if (copy.AnnotationValue != null)
            {
                return(copy);
            }

            copy.NestedResults = copy.NestedResults.Where(r => r.IsValid == copy.IsValid).ToList();

            if (copy.NestedResults.Count != 1)
            {
                return(copy);
            }

            copy._CopyDataFrom(copy.NestedResults[0]);

            return(copy);
        }
        /// <summary>
        /// Converts an object to a <see cref="JsonValue"/>.
        /// </summary>
        /// <param name="serializer">The <see cref="JsonSerializer"/> instance to use for additional
        /// serialization of values.</param>
        /// <returns>The <see cref="JsonValue"/> representation of the object.</returns>
        public JsonValue ToJson(JsonSerializer serializer)
        {
            var obj = new JsonObject();

            obj["valid"] = IsValid;
            if (RelativeLocation != null)
            {
                var relativeLocation = RelativeLocation.ToString();
                obj["keywordLocation"] = relativeLocation;
                if (AbsoluteLocation != null && (AbsoluteLocation.Fragment != relativeLocation ||
                                                 RelativeLocation.Contains("$ref") ||
                                                 RelativeLocation.Contains("$recursiveRef")))
                {
                    obj["absoluteKeywordLocation"] = AbsoluteLocation.OriginalString;
                }
            }
            if (InstanceLocation != null)
            {
                obj["instanceLocation"] = InstanceLocation.ToString();
            }

            var nonNullNestedResults = NestedResults.Where(r => !ReferenceEquals(r, Null)).ToList();

            if (Keyword != null)
            {
                obj["keyword"] = Keyword;
            }
            if (IsValid)
            {
                if (AnnotationValue != null)
                {
                    obj["annotation"] = AnnotationValue;
                }
                if (nonNullNestedResults.Any())
                {
                    obj["annotations"] = nonNullNestedResults.Select(r => r.ToJson(serializer)).ToJson();
                }
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(ErrorMessage))
                {
                    obj["error"] = ErrorMessage;
                }
                if (nonNullNestedResults.Any())
                {
                    obj["errors"] = nonNullNestedResults.Select(r => r.ToJson(serializer)).ToJson();
                }
            }
            if (IncludeAdditionalInfo && AdditionalInfo != null && AdditionalInfo.Any())
            {
                obj["additionalInfo"] = AdditionalInfo;
            }

            return(obj);
        }
예제 #3
0
        private IEnumerable <ValidationResults> _GetAllChildren()
        {
            var all = new List <ValidationResults>();

            if (Annotations.Any() || Message != null)
            {
                all.Add(this);
            }
            all.AddRange(NestedResults.SelectMany(r => r._GetAllChildren()));

            _nestedResults.Clear();

            return(all);
        }
 /// <summary>Serves as the default hash function. </summary>
 /// <returns>A hash code for the current object.</returns>
 public override int GetHashCode()
 {
     unchecked
     {
         var hashCode = IsValid.GetHashCode();
         hashCode = (hashCode * 397) ^ (RelativeLocation?.GetHashCode() ?? 0);
         hashCode = (hashCode * 397) ^ (AbsoluteLocation?.GetHashCode() ?? 0);
         hashCode = (hashCode * 397) ^ (InstanceLocation?.GetHashCode() ?? 0);
         hashCode = (hashCode * 397) ^ (AnnotationValue?.GetHashCode() ?? 0);
         hashCode = (hashCode * 397) ^ (ErrorMessage?.GetHashCode() ?? 0);
         hashCode = (hashCode * 397) ^ (Keyword?.GetHashCode() ?? 0);
         hashCode = (hashCode * 397) ^ (AdditionalInfo?.GetHashCode() ?? 0);
         hashCode = (hashCode * 397) ^ (NestedResults?.GetCollectionHashCode() ?? 0);
         return(hashCode);
     }
 }
 /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
 /// <returns>true if the current object is equal to the <paramref name="other" /> parameter; otherwise, false.</returns>
 /// <param name="other">An object to compare with this object.</param>
 public bool Equals(SchemaValidationResults other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(IsValid == other.IsValid &&
            Equals(RelativeLocation, other.RelativeLocation) &&
            Equals(AbsoluteLocation, other.AbsoluteLocation) &&
            Equals(InstanceLocation, other.InstanceLocation) &&
            Equals(AnnotationValue, other.AnnotationValue) &&
            Equals(ErrorMessage, other.ErrorMessage) &&
            string.Equals(Keyword, other.Keyword) &&
            Equals(AdditionalInfo, other.AdditionalInfo) &&
            NestedResults.ContentsEqual(other.NestedResults));
 }
 private IEnumerable <SchemaValidationResults> _GetAllChildren()
 {
     return(NestedResults.Union(NestedResults.SelectMany(r => r._GetAllChildren())));
 }