/// <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); }