public static string AsJson(this IElementNavigator navigator, int indent = 0) { var builder = new StringBuilder(); var pad = new string(' ', indent * 2); if (navigator.HasChildren()) { builder.Append($"{pad}{navigator.Name}: "); builder.Append("{\n"); foreach (var child in navigator.Children()) { builder.Append(AsJson(child, indent + 1)); } builder.Append(pad + "},\n"); } else { string content = $"{pad}{navigator.Name}: \"{navigator.Value}\",\n"; builder.Append(content); } return(builder.ToString()); }
// private OperationOutcome validateElement(ElementDefinitionNavigator definition, IElementNavigator instance) private OperationOutcome validateElement(ElementDefinitionNavigator definition, IElementNavigator instance) { var outcome = new OperationOutcome(); try { Trace(outcome, $"Start validation of ElementDefinition at path '{definition.QualifiedDefinitionPath()}'", Issue.PROCESSING_PROGRESS, instance); ScopeTracker.Enter(instance); // If navigator cannot be moved to content, there's really nothing to validate against. if (definition.AtRoot && !definition.MoveToFirstChild()) { outcome.AddIssue($"Snapshot component of profile '{definition.StructureDefinition?.Url}' has no content.", Issue.PROFILE_ELEMENTDEF_IS_EMPTY, instance); return(outcome); } // Any node must either have a value, or children, or both (e.g. extensions on primitives) if (instance.Value == null && !instance.HasChildren()) { outcome.AddIssue("Element must not be empty", Issue.CONTENT_ELEMENT_MUST_HAVE_VALUE_OR_CHILDREN, instance); return(outcome); } var elementConstraints = definition.Current; if (elementConstraints.IsPrimitiveValueConstraint()) { // The "value" property of a FHIR Primitive is the bottom of our recursion chain, it does not have a nameReference // nor a <type>, the only thing left to do to validate the content is to validate the string representation of the // primitive against the regex given in the core definition outcome.Add(VerifyPrimitiveContents(elementConstraints, instance)); } else if (definition.HasChildren) { // Handle in-lined constraints on children. In a snapshot, these children should be exhaustive, // so there's no point in also validating the <type> or <nameReference> // TODO: Check whether this is even true when the <type> has a profile? outcome.Add(this.ValidateChildConstraints(definition, instance)); } else { // No inline-children, so validation depends on the presence of a <type> or <nameReference> if (elementConstraints.Type != null || elementConstraints.NameReference != null) { outcome.Add(this.ValidateType(elementConstraints, instance)); outcome.Add(ValidateNameReference(elementConstraints, definition, instance)); } else { Trace(outcome, "ElementDefinition has no child, nor does it specify a type or nameReference to validate the instance data against", Issue.PROFILE_ELEMENTDEF_CONTAINS_NO_TYPE_OR_NAMEREF, instance); } } outcome.Add(this.ValidateFixed(elementConstraints, instance)); outcome.Add(this.ValidatePattern(elementConstraints, instance)); outcome.Add(this.ValidateMinMaxValue(elementConstraints, instance)); outcome.Add(ValidateMaxLength(elementConstraints, instance)); outcome.Add(ValidateConstraints(elementConstraints, instance)); outcome.Add(this.ValidateBinding(elementConstraints, instance)); // If the report only has partial information, no use to show the hierarchy, so flatten it. if (Settings.Trace == false) { outcome.Flatten(); } return(outcome); } finally { ScopeTracker.Leave(instance); } }