Search in sources :

Example 56 with ValidationMessage

use of org.hl7.fhir.utilities.validation.ValidationMessage in project org.hl7.fhir.core by hapifhir.

the class InstanceValidator method checkCodeableConcept.

private void checkCodeableConcept(List<ValidationMessage> errors, String path, Element focus, CodeableConcept fixed, String fixedSource, boolean pattern) {
    checkFixedValue(errors, path + ".text", focus.getNamedChild("text"), fixed.getTextElement(), fixedSource, "text", focus, pattern);
    List<Element> codings = new ArrayList<Element>();
    focus.getNamedChildren("coding", codings);
    if (pattern) {
        if (rule(errors, IssueType.VALUE, focus.line(), focus.col(), path, codings.size() >= fixed.getCoding().size(), I18nConstants.TERMINOLOGY_TX_CODING_COUNT, Integer.toString(fixed.getCoding().size()), Integer.toString(codings.size()))) {
            for (int i = 0; i < fixed.getCoding().size(); i++) {
                Coding fixedCoding = fixed.getCoding().get(i);
                boolean found = false;
                List<ValidationMessage> allErrorsFixed = new ArrayList<>();
                List<ValidationMessage> errorsFixed;
                for (int j = 0; j < codings.size() && !found; ++j) {
                    errorsFixed = new ArrayList<>();
                    checkFixedValue(errorsFixed, path + ".coding", codings.get(j), fixedCoding, fixedSource, "coding", focus, pattern);
                    if (!hasErrors(errorsFixed)) {
                        found = true;
                    } else {
                        errorsFixed.stream().filter(t -> t.getLevel().ordinal() >= IssueSeverity.ERROR.ordinal()).forEach(t -> allErrorsFixed.add(t));
                    }
                }
                if (!found) {
                    if (fixedCoding.hasUserSelected()) {
                        rule(errors, IssueType.VALUE, focus.line(), focus.col(), path, false, pattern ? I18nConstants.TYPE_CHECKS_PATTERN_CC_US : I18nConstants.TYPE_CHECKS_FIXED_CC_US, fixedCoding.getSystemElement().asStringValue(), fixedCoding.getCodeElement().asStringValue(), fixedCoding.getDisplayElement().asStringValue(), fixedSource, allErrorsFixed, fixedCoding.getUserSelected());
                    } else {
                        rule(errors, IssueType.VALUE, focus.line(), focus.col(), path, false, pattern ? I18nConstants.TYPE_CHECKS_PATTERN_CC : I18nConstants.TYPE_CHECKS_FIXED_CC, fixedCoding.getSystemElement().asStringValue(), fixedCoding.getCodeElement().asStringValue(), fixedCoding.getDisplayElement().asStringValue(), fixedSource, allErrorsFixed);
                    }
                }
            }
        }
    } else {
        if (rule(errors, IssueType.VALUE, focus.line(), focus.col(), path, codings.size() == fixed.getCoding().size(), I18nConstants.TERMINOLOGY_TX_CODING_COUNT, Integer.toString(fixed.getCoding().size()), Integer.toString(codings.size()))) {
            for (int i = 0; i < codings.size(); i++) checkFixedValue(errors, path + ".coding", codings.get(i), fixed.getCoding().get(i), fixedSource, "coding", focus, false);
        }
    }
}
Also used : TypedElementDefinition(org.hl7.fhir.r5.utils.FHIRPathEngine.TypedElementDefinition) XmlParser(org.hl7.fhir.r5.elementmodel.XmlParser) IntegerType(org.hl7.fhir.r5.model.IntegerType) VersionUtilities(org.hl7.fhir.utilities.VersionUtilities) ExtensionContextType(org.hl7.fhir.r5.model.StructureDefinition.ExtensionContextType) TimeType(org.hl7.fhir.r5.model.TimeType) TerminologyServiceErrorClass(org.hl7.fhir.r5.terminologies.ValueSetExpander.TerminologyServiceErrorClass) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode) CanonicalResource(org.hl7.fhir.r5.model.CanonicalResource) ConstraintSeverity(org.hl7.fhir.r5.model.ElementDefinition.ConstraintSeverity) StringUtils(org.apache.commons.lang3.StringUtils) SearchParameterValidator(org.hl7.fhir.validation.instance.type.SearchParameterValidator) BigDecimal(java.math.BigDecimal) Document(org.w3c.dom.Document) Map(java.util.Map) DateTimeType(org.hl7.fhir.r5.model.DateTimeType) JsonParser(org.hl7.fhir.r5.elementmodel.JsonParser) ImplementationGuide(org.hl7.fhir.r5.model.ImplementationGuide) Resource(org.hl7.fhir.r5.model.Resource) Source(org.hl7.fhir.utilities.validation.ValidationMessage.Source) ObjectConverter(org.hl7.fhir.r5.elementmodel.ObjectConverter) ConceptDefinitionComponent(org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent) FormatUtilities(org.hl7.fhir.r5.formats.FormatUtilities) VersionURLInfo(org.hl7.fhir.utilities.VersionUtilities.VersionURLInfo) XVerExtensionManager(org.hl7.fhir.r5.utils.XVerExtensionManager) IssueSeverity(org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity) Timing(org.hl7.fhir.r5.model.Timing) ValidatorHostContext(org.hl7.fhir.validation.instance.utils.ValidatorHostContext) ContactPoint(org.hl7.fhir.r5.model.ContactPoint) Set(java.util.Set) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) ResourceValidationTracker(org.hl7.fhir.validation.instance.utils.ResourceValidationTracker) StandardCharsets(java.nio.charset.StandardCharsets) TypeRefComponent(org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent) PropertyRepresentation(org.hl7.fhir.r5.model.ElementDefinition.PropertyRepresentation) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) Coding(org.hl7.fhir.r5.model.Coding) Utilities(org.hl7.fhir.utilities.Utilities) BooleanType(org.hl7.fhir.r5.model.BooleanType) ParserBase(org.hl7.fhir.r5.elementmodel.ParserBase) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) ElementDefinitionSlicingDiscriminatorComponent(org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionSlicingDiscriminatorComponent) ElementDefinitionConstraintComponent(org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionConstraintComponent) BindingStrength(org.hl7.fhir.r5.model.Enumerations.BindingStrength) StructureDefinitionContextComponent(org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionContextComponent) BaseValidator(org.hl7.fhir.validation.BaseValidator) BundleValidator(org.hl7.fhir.validation.instance.type.BundleValidator) ArrayList(java.util.ArrayList) ValidationPolicy(org.hl7.fhir.r5.elementmodel.ParserBase.ValidationPolicy) QuestionnaireValidator(org.hl7.fhir.validation.instance.type.QuestionnaireValidator) Calendar(java.util.Calendar) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) Decimal(org.fhir.ucum.Decimal) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) ValidationLevel(org.hl7.fhir.validation.cli.utils.ValidationLevel) DataType(org.hl7.fhir.r5.model.DataType) IOException(java.io.IOException) InstantType(org.hl7.fhir.r5.model.InstantType) File(java.io.File) Base(org.hl7.fhir.r5.model.Base) Manager(org.hl7.fhir.r5.elementmodel.Manager) ElementInfo(org.hl7.fhir.validation.instance.utils.ElementInfo) CodeableConcept(org.hl7.fhir.r5.model.CodeableConcept) FHIRException(org.hl7.fhir.exceptions.FHIRException) ExpressionNode(org.hl7.fhir.r5.model.ExpressionNode) TypeDetails(org.hl7.fhir.r5.model.TypeDetails) JsonObject(com.google.gson.JsonObject) NodeType(org.hl7.fhir.utilities.xhtml.NodeType) IndexedElement(org.hl7.fhir.validation.instance.utils.IndexedElement) FHIRPathEngine(org.hl7.fhir.r5.utils.FHIRPathEngine) StructureDefinitionKind(org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionKind) SampledData(org.hl7.fhir.r5.model.SampledData) Range(org.hl7.fhir.r5.model.Range) QuestionnaireMode(org.hl7.fhir.validation.cli.utils.QuestionnaireMode) I18nConstants(org.hl7.fhir.utilities.i18n.I18nConstants) ByteArrayInputStream(java.io.ByteArrayInputStream) StructureDefinitionMappingComponent(org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionMappingComponent) Gson(com.google.gson.Gson) CanonicalType(org.hl7.fhir.r5.model.CanonicalType) Identifier(org.hl7.fhir.r5.model.Identifier) AggregationMode(org.hl7.fhir.r5.model.ElementDefinition.AggregationMode) Period(org.hl7.fhir.r5.model.Period) ToolingExtensions(org.hl7.fhir.r5.utils.ToolingExtensions) Collection(java.util.Collection) Reference(org.hl7.fhir.r5.model.Reference) StructureDefinitionSnapshotComponent(org.hl7.fhir.r5.model.StructureDefinition.StructureDefinitionSnapshotComponent) UUID(java.util.UUID) Quantity(org.hl7.fhir.r5.model.Quantity) PrimitiveType(org.hl7.fhir.r5.model.PrimitiveType) ChildIterator(org.hl7.fhir.validation.instance.utils.ChildIterator) List(java.util.List) ValueSetExpansionContainsComponent(org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionContainsComponent) CodeSystem(org.hl7.fhir.r5.model.CodeSystem) DateType(org.hl7.fhir.r5.model.DateType) ElementDefinitionBindingComponent(org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionBindingComponent) Enumeration(org.hl7.fhir.r5.model.Enumeration) Base64InputStream(org.apache.commons.codec.binary.Base64InputStream) DecimalStatus(org.hl7.fhir.utilities.Utilities.DecimalStatus) ValidationResult(org.hl7.fhir.r5.context.IWorkerContext.ValidationResult) ImplementationGuideGlobalComponent(org.hl7.fhir.r5.model.ImplementationGuide.ImplementationGuideGlobalComponent) ResolvedReference(org.hl7.fhir.validation.instance.utils.ResolvedReference) NotImplementedException(org.apache.commons.lang3.NotImplementedException) org.hl7.fhir.r5.utils.validation.constants(org.hl7.fhir.r5.utils.validation.constants) ElementDefinitionMappingComponent(org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionMappingComponent) ElementDefinitionSlicingComponent(org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionSlicingComponent) ValueSet(org.hl7.fhir.r5.model.ValueSet) ProfileUtilities(org.hl7.fhir.r5.conformance.ProfileUtilities) HashMap(java.util.HashMap) TypeDerivationRule(org.hl7.fhir.r5.model.StructureDefinition.TypeDerivationRule) NodeStack(org.hl7.fhir.validation.instance.utils.NodeStack) DataRenderer(org.hl7.fhir.r5.renderers.DataRenderer) CodeSystemValidator(org.hl7.fhir.validation.instance.type.CodeSystemValidator) HashSet(java.util.HashSet) UriType(org.hl7.fhir.r5.model.UriType) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) StructureDefinitionValidator(org.hl7.fhir.validation.instance.type.StructureDefinitionValidator) BuildExtensions(org.hl7.fhir.r5.utils.BuildExtensions) SearchParameter(org.hl7.fhir.r5.model.SearchParameter) ValidationOptions(org.hl7.fhir.utilities.validation.ValidationOptions) DiscriminatorType(org.hl7.fhir.r5.model.ElementDefinition.DiscriminatorType) MeasureValidator(org.hl7.fhir.validation.instance.type.MeasureValidator) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) Ratio(org.hl7.fhir.r5.model.Ratio) IssueType(org.hl7.fhir.utilities.validation.ValidationMessage.IssueType) Attachment(org.hl7.fhir.r5.model.Attachment) IWorkerContext(org.hl7.fhir.r5.context.IWorkerContext) SpecialElement(org.hl7.fhir.r5.elementmodel.Element.SpecialElement) StringType(org.hl7.fhir.r5.model.StringType) Address(org.hl7.fhir.r5.model.Address) SIDUtilities(org.hl7.fhir.utilities.SIDUtilities) FhirFormat(org.hl7.fhir.r5.elementmodel.Manager.FhirFormat) Element(org.hl7.fhir.r5.elementmodel.Element) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition) Extension(org.hl7.fhir.r5.model.Extension) UnicodeUtilities(org.hl7.fhir.utilities.UnicodeUtilities) org.hl7.fhir.r5.utils.validation(org.hl7.fhir.r5.utils.validation) IEvaluationContext(org.hl7.fhir.r5.utils.FHIRPathEngine.IEvaluationContext) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) DecimalType(org.hl7.fhir.r5.model.DecimalType) ValueSetValidator(org.hl7.fhir.validation.instance.type.ValueSetValidator) FHIRLexerException(org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException) InputStream(java.io.InputStream) HumanName(org.hl7.fhir.r5.model.HumanName) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) Coding(org.hl7.fhir.r5.model.Coding) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) IndexedElement(org.hl7.fhir.validation.instance.utils.IndexedElement) SpecialElement(org.hl7.fhir.r5.elementmodel.Element.SpecialElement) Element(org.hl7.fhir.r5.elementmodel.Element) ArrayList(java.util.ArrayList) ContactPoint(org.hl7.fhir.r5.model.ContactPoint)

Example 57 with ValidationMessage

use of org.hl7.fhir.utilities.validation.ValidationMessage in project org.hl7.fhir.core by hapifhir.

the class InstanceValidator method checkCodeableConcept.

private boolean checkCodeableConcept(List<ValidationMessage> errors, String path, Element element, StructureDefinition profile, ElementDefinition theElementCntext, NodeStack stack) {
    boolean res = true;
    if (!noTerminologyChecks && theElementCntext != null && theElementCntext.hasBinding()) {
        ElementDefinitionBindingComponent binding = theElementCntext.getBinding();
        if (warning(errors, IssueType.CODEINVALID, element.line(), element.col(), path, binding != null, I18nConstants.TERMINOLOGY_TX_BINDING_MISSING, path)) {
            if (binding.hasValueSet()) {
                ValueSet valueset = resolveBindingReference(profile, binding.getValueSet(), profile.getUrl());
                if (valueset == null) {
                    CodeSystem cs = context.fetchCodeSystem(binding.getValueSet());
                    if (rule(errors, IssueType.CODEINVALID, element.line(), element.col(), path, cs == null, I18nConstants.TERMINOLOGY_TX_VALUESET_NOTFOUND_CS, describeReference(binding.getValueSet()))) {
                        warning(errors, IssueType.CODEINVALID, element.line(), element.col(), path, valueset != null, I18nConstants.TERMINOLOGY_TX_VALUESET_NOTFOUND, describeReference(binding.getValueSet()));
                    }
                } else {
                    try {
                        CodeableConcept cc = ObjectConverter.readAsCodeableConcept(element);
                        if (!cc.hasCoding()) {
                            if (binding.getStrength() == BindingStrength.REQUIRED)
                                rule(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_CODE_VALUESET, describeValueSet(binding.getValueSet()));
                            else if (binding.getStrength() == BindingStrength.EXTENSIBLE) {
                                if (binding.hasExtension("http://hl7.org/fhir/StructureDefinition/elementdefinition-maxValueSet"))
                                    rule(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_CODE_VALUESETMAX, describeReference(ToolingExtensions.readStringExtension(binding, "http://hl7.org/fhir/StructureDefinition/elementdefinition-maxValueSet")), valueset.getUrl());
                                else
                                    warning(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_CODE_VALUESET_EXT, describeValueSet(binding.getValueSet()));
                            }
                        } else {
                            long t = System.nanoTime();
                            // Check whether the codes are appropriate for the type of binding we have
                            boolean bindingsOk = true;
                            if (binding.getStrength() != BindingStrength.EXAMPLE) {
                                if (binding.getStrength() == BindingStrength.REQUIRED) {
                                    removeTrackedMessagesForLocation(errors, element, path);
                                }
                                boolean atLeastOneSystemIsSupported = false;
                                for (Coding nextCoding : cc.getCoding()) {
                                    String nextSystem = nextCoding.getSystem();
                                    if (isNotBlank(nextSystem) && context.supportsSystem(nextSystem)) {
                                        atLeastOneSystemIsSupported = true;
                                        break;
                                    }
                                }
                                if (!atLeastOneSystemIsSupported && binding.getStrength() == BindingStrength.EXAMPLE) {
                                // ignore this since we can't validate but it doesn't matter..
                                } else {
                                    // we're going to validate the codings directly, so only check the valueset
                                    ValidationResult vr = checkCodeOnServer(stack, valueset, cc, true);
                                    if (!vr.isOk()) {
                                        bindingsOk = false;
                                        if (vr.getErrorClass() != null && vr.getErrorClass() == TerminologyServiceErrorClass.NOSERVICE) {
                                            if (binding.getStrength() == BindingStrength.REQUIRED || (binding.getStrength() == BindingStrength.EXTENSIBLE && binding.hasExtension("http://hl7.org/fhir/StructureDefinition/elementdefinition-maxValueSet"))) {
                                                hint(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOSVC_BOUND_REQ, describeReference(binding.getValueSet()));
                                            } else if (binding.getStrength() == BindingStrength.EXTENSIBLE) {
                                                hint(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOSVC_BOUND_EXT, describeReference(binding.getValueSet()));
                                            }
                                        } else if (vr.getErrorClass() != null && vr.getErrorClass().isInfrastructure()) {
                                            if (binding.getStrength() == BindingStrength.REQUIRED)
                                                txWarning(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_CONFIRM_1_CC, describeReference(binding.getValueSet()), vr.getErrorClass().toString());
                                            else if (binding.getStrength() == BindingStrength.EXTENSIBLE) {
                                                if (binding.hasExtension("http://hl7.org/fhir/StructureDefinition/elementdefinition-maxValueSet"))
                                                    checkMaxValueSet(errors, path, element, profile, ToolingExtensions.readStringExtension(binding, "http://hl7.org/fhir/StructureDefinition/elementdefinition-maxValueSet"), cc, stack);
                                                else if (!noExtensibleWarnings)
                                                    txWarningForLaterRemoval(element, errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_CONFIRM_2_CC, describeReference(binding.getValueSet()), vr.getErrorClass().toString());
                                            } else if (binding.getStrength() == BindingStrength.PREFERRED) {
                                                if (baseOnly) {
                                                    txHint(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_CONFIRM_3_CC, describeReference(binding.getValueSet()), vr.getErrorClass().toString());
                                                }
                                            }
                                        } else {
                                            if (binding.getStrength() == BindingStrength.REQUIRED) {
                                                txRule(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_1_CC, describeValueSet(binding.getValueSet()), ccSummary(cc));
                                            } else if (binding.getStrength() == BindingStrength.EXTENSIBLE) {
                                                if (binding.hasExtension("http://hl7.org/fhir/StructureDefinition/elementdefinition-maxValueSet"))
                                                    checkMaxValueSet(errors, path, element, profile, ToolingExtensions.readStringExtension(binding, "http://hl7.org/fhir/StructureDefinition/elementdefinition-maxValueSet"), cc, stack);
                                                if (!noExtensibleWarnings)
                                                    txWarningForLaterRemoval(element, errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_2_CC, describeValueSet(binding.getValueSet()), ccSummary(cc));
                                            } else if (binding.getStrength() == BindingStrength.PREFERRED) {
                                                if (baseOnly) {
                                                    txHint(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_3_CC, describeValueSet(binding.getValueSet()), ccSummary(cc));
                                                }
                                            }
                                        }
                                    } else if (vr.getMessage() != null) {
                                        res = false;
                                        txWarning(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, vr.getMessage());
                                    } else {
                                        if (binding.getStrength() == BindingStrength.EXTENSIBLE) {
                                            removeTrackedMessagesForLocation(errors, element, path);
                                        }
                                        res = false;
                                    }
                                }
                                // to validate, we'll validate that the codes actually exist
                                if (bindingsOk) {
                                    for (Coding nextCoding : cc.getCoding()) {
                                        checkBindings(errors, path, element, stack, valueset, nextCoding);
                                    }
                                }
                                timeTracker.tx(t, "vc " + DataRenderer.display(context, cc));
                            }
                        }
                    } catch (Exception e) {
                        if (STACK_TRACE)
                            e.printStackTrace();
                        warning(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_ERROR_CODEABLECONCEPT, e.getMessage());
                    }
                }
            } else if (binding.hasValueSet()) {
                hint(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_BINDING_CANTCHECK);
            } else if (!noBindingMsgSuppressed) {
                hint(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_BINDING_NOSOURCE, path);
            }
        }
    }
    return res;
}
Also used : Coding(org.hl7.fhir.r5.model.Coding) ValidationResult(org.hl7.fhir.r5.context.IWorkerContext.ValidationResult) ElementDefinitionBindingComponent(org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionBindingComponent) ValueSet(org.hl7.fhir.r5.model.ValueSet) CodeSystem(org.hl7.fhir.r5.model.CodeSystem) TerminologyServiceException(org.hl7.fhir.exceptions.TerminologyServiceException) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) PathEngineException(org.hl7.fhir.exceptions.PathEngineException) FHIRLexerException(org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException) CodeableConcept(org.hl7.fhir.r5.model.CodeableConcept)

Example 58 with ValidationMessage

use of org.hl7.fhir.utilities.validation.ValidationMessage in project org.hl7.fhir.core by hapifhir.

the class InstanceValidator method getValueForDiscriminator.

private Element getValueForDiscriminator(Object appContext, List<ValidationMessage> errors, Element element, String discriminator, ElementDefinition criteria, NodeStack stack) throws FHIRException, IOException {
    String p = stack.getLiteralPath() + "." + element.getName();
    Element focus = element;
    String[] dlist = discriminator.split("\\.");
    for (String d : dlist) {
        if (focus.fhirType().equals("Reference") && d.equals("reference")) {
            String url = focus.getChildValue("reference");
            if (Utilities.noString(url))
                throw new FHIRException(context.formatMessage(I18nConstants.NO_REFERENCE_RESOLVING_DISCRIMINATOR__FROM_, discriminator, element.getProperty().getName()));
            // Note that we use the passed in stack here. This might be a problem if the discriminator is deep enough?
            Element target = resolve(appContext, url, stack, errors, p);
            if (target == null)
                throw new FHIRException(context.formatMessage(I18nConstants.UNABLE_TO_FIND_RESOURCE__AT__RESOLVING_DISCRIMINATOR__FROM_, url, d, discriminator, element.getProperty().getName()));
            focus = target;
        } else if (d.equals("value") && focus.isPrimitive()) {
            return focus;
        } else {
            List<Element> children = focus.getChildren(d);
            if (children.isEmpty())
                throw new FHIRException(context.formatMessage(I18nConstants.UNABLE_TO_FIND__RESOLVING_DISCRIMINATOR__FROM_, d, discriminator, element.getProperty().getName()));
            if (children.size() > 1)
                throw new FHIRException(context.formatMessage(I18nConstants.FOUND__ITEMS_FOR__RESOLVING_DISCRIMINATOR__FROM_, Integer.toString(children.size()), d, discriminator, element.getProperty().getName()));
            focus = children.get(0);
            p = p + "." + d;
        }
    }
    return focus;
}
Also used : NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) IndexedElement(org.hl7.fhir.validation.instance.utils.IndexedElement) SpecialElement(org.hl7.fhir.r5.elementmodel.Element.SpecialElement) Element(org.hl7.fhir.r5.elementmodel.Element) ArrayList(java.util.ArrayList) List(java.util.List) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 59 with ValidationMessage

use of org.hl7.fhir.utilities.validation.ValidationMessage in project org.hl7.fhir.core by hapifhir.

the class InstanceValidator method sliceMatches.

/**
 * @param element - the candidate that might be in the slice
 * @param path    - for reporting any errors. the XPath for the element
 * @param slicer  - the definition of how slicing is determined
 * @param ed      - the slice for which to test membership
 * @param errors
 * @param stack
 * @param srcProfile
 * @return
 * @throws DefinitionException
 * @throws DefinitionException
 * @throws IOException
 * @throws FHIRException
 */
private boolean sliceMatches(ValidatorHostContext hostContext, Element element, String path, ElementDefinition slicer, ElementDefinition ed, StructureDefinition profile, List<ValidationMessage> errors, List<ValidationMessage> sliceInfo, NodeStack stack, StructureDefinition srcProfile) throws DefinitionException, FHIRException {
    if (!slicer.getSlicing().hasDiscriminator())
        // cannot validate in this case
        return false;
    ExpressionNode n = (ExpressionNode) ed.getUserData("slice.expression.cache");
    if (n == null) {
        long t = System.nanoTime();
        // GG: this approach is flawed because it treats discriminators individually rather than collectively
        StringBuilder expression = new StringBuilder("true");
        boolean anyFound = false;
        Set<String> discriminators = new HashSet<>();
        for (ElementDefinitionSlicingDiscriminatorComponent s : slicer.getSlicing().getDiscriminator()) {
            String discriminator = s.getPath();
            discriminators.add(discriminator);
            List<ElementDefinition> criteriaElements = getCriteriaForDiscriminator(path, ed, discriminator, profile, s.getType() == DiscriminatorType.PROFILE, srcProfile);
            boolean found = false;
            for (ElementDefinition criteriaElement : criteriaElements) {
                found = true;
                if (s.getType() == DiscriminatorType.TYPE) {
                    String type = null;
                    if (!criteriaElement.getPath().contains("[") && discriminator.contains("[")) {
                        discriminator = discriminator.substring(0, discriminator.indexOf('['));
                        String lastNode = tail(discriminator);
                        type = tail(criteriaElement.getPath()).substring(lastNode.length());
                        type = type.substring(0, 1).toLowerCase() + type.substring(1);
                    } else if (!criteriaElement.hasType() || criteriaElement.getType().size() == 1) {
                        if (discriminator.contains("["))
                            discriminator = discriminator.substring(0, discriminator.indexOf('['));
                        if (criteriaElement.hasType()) {
                            type = criteriaElement.getType().get(0).getWorkingCode();
                        } else if (!criteriaElement.getPath().contains(".")) {
                            type = criteriaElement.getPath();
                        } else {
                            throw new DefinitionException(context.formatMessage(I18nConstants.DISCRIMINATOR__IS_BASED_ON_TYPE_BUT_SLICE__IN__HAS_NO_TYPES, discriminator, ed.getId(), profile.getUrl()));
                        }
                    } else if (criteriaElement.getType().size() > 1) {
                        throw new DefinitionException(context.formatMessage(I18nConstants.DISCRIMINATOR__IS_BASED_ON_TYPE_BUT_SLICE__IN__HAS_MULTIPLE_TYPES_, discriminator, ed.getId(), profile.getUrl(), criteriaElement.typeSummary()));
                    } else
                        throw new DefinitionException(context.formatMessage(I18nConstants.DISCRIMINATOR__IS_BASED_ON_TYPE_BUT_SLICE__IN__HAS_NO_TYPES, discriminator, ed.getId(), profile.getUrl()));
                    if (discriminator.isEmpty())
                        expression.append(" and $this is " + type);
                    else
                        expression.append(" and " + discriminator + " is " + type);
                } else if (s.getType() == DiscriminatorType.PROFILE) {
                    if (criteriaElement.getType().size() == 0) {
                        throw new DefinitionException(context.formatMessage(I18nConstants.PROFILE_BASED_DISCRIMINATORS_MUST_HAVE_A_TYPE__IN_PROFILE_, criteriaElement.getId(), profile.getUrl()));
                    }
                    if (criteriaElement.getType().size() != 1) {
                        throw new DefinitionException(context.formatMessage(I18nConstants.PROFILE_BASED_DISCRIMINATORS_MUST_HAVE_ONLY_ONE_TYPE__IN_PROFILE_, criteriaElement.getId(), profile.getUrl()));
                    }
                    List<CanonicalType> list = discriminator.endsWith(".resolve()") || discriminator.equals("resolve()") ? criteriaElement.getType().get(0).getTargetProfile() : criteriaElement.getType().get(0).getProfile();
                    if (list.size() == 0) {
                        throw new DefinitionException(context.formatMessage(I18nConstants.PROFILE_BASED_DISCRIMINATORS_MUST_HAVE_A_TYPE_WITH_A_PROFILE__IN_PROFILE_, criteriaElement.getId(), profile.getUrl()));
                    } else if (list.size() > 1) {
                        CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(" or ");
                        for (CanonicalType c : list) {
                            b.append(discriminator + ".conformsTo('" + c.getValue() + "')");
                        }
                        expression.append(" and (" + b + ")");
                    } else {
                        expression.append(" and " + discriminator + ".conformsTo('" + list.get(0).getValue() + "')");
                    }
                } else if (s.getType() == DiscriminatorType.EXISTS) {
                    if (criteriaElement.hasMin() && criteriaElement.getMin() >= 1)
                        expression.append(" and (" + discriminator + ".exists())");
                    else if (criteriaElement.hasMax() && criteriaElement.getMax().equals("0"))
                        expression.append(" and (" + discriminator + ".exists().not())");
                    else
                        throw new FHIRException(context.formatMessage(I18nConstants.DISCRIMINATOR__IS_BASED_ON_ELEMENT_EXISTENCE_BUT_SLICE__NEITHER_SETS_MIN1_OR_MAX0, discriminator, ed.getId()));
                } else if (criteriaElement.hasFixed()) {
                    buildFixedExpression(ed, expression, discriminator, criteriaElement);
                } else if (criteriaElement.hasPattern()) {
                    buildPattternExpression(ed, expression, discriminator, criteriaElement);
                } else if (criteriaElement.hasBinding() && criteriaElement.getBinding().hasStrength() && criteriaElement.getBinding().getStrength().equals(BindingStrength.REQUIRED) && criteriaElement.getBinding().hasValueSet()) {
                    expression.append(" and (" + discriminator + " memberOf '" + criteriaElement.getBinding().getValueSet() + "')");
                } else {
                    found = false;
                }
                if (found)
                    break;
            }
            if (found)
                anyFound = true;
        }
        if (!anyFound) {
            if (slicer.getSlicing().getDiscriminator().size() > 1)
                throw new DefinitionException(context.formatMessage(I18nConstants.COULD_NOT_MATCH_ANY_DISCRIMINATORS__FOR_SLICE__IN_PROFILE___NONE_OF_THE_DISCRIMINATOR__HAVE_FIXED_VALUE_BINDING_OR_EXISTENCE_ASSERTIONS, discriminators, ed.getId(), profile.getUrl(), discriminators));
            else
                throw new DefinitionException(context.formatMessage(I18nConstants.COULD_NOT_MATCH_DISCRIMINATOR__FOR_SLICE__IN_PROFILE___THE_DISCRIMINATOR__DOES_NOT_HAVE_FIXED_VALUE_BINDING_OR_EXISTENCE_ASSERTIONS, discriminators, ed.getId(), profile.getUrl(), discriminators));
        }
        try {
            n = fpe.parse(fixExpr(expression.toString(), null));
        } catch (FHIRLexerException e) {
            if (STACK_TRACE)
                e.printStackTrace();
            throw new FHIRException(context.formatMessage(I18nConstants.PROBLEM_PROCESSING_EXPRESSION__IN_PROFILE__PATH__, expression, profile.getUrl(), path, e.getMessage()));
        }
        timeTracker.fpe(t);
        ed.setUserData("slice.expression.cache", n);
    }
    ValidatorHostContext shc = hostContext.forSlicing();
    boolean pass = evaluateSlicingExpression(shc, element, path, profile, n);
    if (!pass) {
        slicingHint(sliceInfo, IssueType.STRUCTURE, element.line(), element.col(), path, false, isProfile(slicer), (context.formatMessage(I18nConstants.DOES_NOT_MATCH_SLICE_, ed.getSliceName())), "discriminator = " + Utilities.escapeXml(n.toString()), null);
        for (String url : shc.getSliceRecords().keySet()) {
            slicingHint(sliceInfo, IssueType.STRUCTURE, element.line(), element.col(), path, false, isProfile(slicer), context.formatMessage(I18nConstants.DETAILS_FOR__MATCHING_AGAINST_PROFILE_, stack.getLiteralPath(), url), context.formatMessage(I18nConstants.PROFILE__DOES_NOT_MATCH_FOR__BECAUSE_OF_THE_FOLLOWING_PROFILE_ISSUES__, url, stack.getLiteralPath(), errorSummaryForSlicingAsHtml(shc.getSliceRecords().get(url))), errorSummaryForSlicingAsText(shc.getSliceRecords().get(url)));
        }
    }
    return pass;
}
Also used : CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) FHIRLexerException(org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) CanonicalType(org.hl7.fhir.r5.model.CanonicalType) FHIRException(org.hl7.fhir.exceptions.FHIRException) ElementDefinitionSlicingDiscriminatorComponent(org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionSlicingDiscriminatorComponent) ExpressionNode(org.hl7.fhir.r5.model.ExpressionNode) ValidatorHostContext(org.hl7.fhir.validation.instance.utils.ValidatorHostContext) ArrayList(java.util.ArrayList) List(java.util.List) TypedElementDefinition(org.hl7.fhir.r5.utils.FHIRPathEngine.TypedElementDefinition) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) HashSet(java.util.HashSet)

Example 60 with ValidationMessage

use of org.hl7.fhir.utilities.validation.ValidationMessage in project org.hl7.fhir.core by hapifhir.

the class InstanceValidator method validate.

@Override
public org.hl7.fhir.r5.elementmodel.Element validate(Object appContext, List<ValidationMessage> errors, InputStream stream, FhirFormat format, List<StructureDefinition> profiles) throws FHIRException {
    ParserBase parser = Manager.makeParser(context, format);
    if (parser instanceof XmlParser)
        ((XmlParser) parser).setAllowXsiLocation(allowXsiLocation);
    parser.setupValidation(ValidationPolicy.EVERYTHING, errors);
    long t = System.nanoTime();
    List<NamedElement> list = null;
    try {
        list = parser.parse(stream);
    } catch (IOException e1) {
        throw new FHIRException(e1);
    }
    timeTracker.load(t);
    if (list != null && !list.isEmpty()) {
        String url = parser.getImpliedProfile();
        if (url != null) {
            StructureDefinition sd = context.fetchResource(StructureDefinition.class, url);
            if (sd == null) {
                rule(errors, IssueType.NOTFOUND, "Payload", false, "Implied profile " + url + " not known to validator");
            } else {
                profiles.add(sd);
            }
        }
        for (NamedElement ne : list) {
            validate(appContext, errors, ne.getName(), ne.getElement(), profiles);
        }
    }
    // todo: this is broken, but fixing it really complicates things elsewhere, so we do this for now
    return (list == null || list.isEmpty()) ? null : list.get(0).getElement();
}
Also used : XmlParser(org.hl7.fhir.r5.elementmodel.XmlParser) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) ParserBase(org.hl7.fhir.r5.elementmodel.ParserBase) IOException(java.io.IOException) NamedElement(org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Aggregations

ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)170 ArrayList (java.util.ArrayList)114 FHIRException (org.hl7.fhir.exceptions.FHIRException)92 Element (org.hl7.fhir.r5.elementmodel.Element)60 IOException (java.io.IOException)46 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)44 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)38 NodeStack (org.hl7.fhir.validation.instance.utils.NodeStack)30 IndexedElement (org.hl7.fhir.validation.instance.utils.IndexedElement)28 NotImplementedException (org.apache.commons.lang3.NotImplementedException)21 ProfileUtilities (org.hl7.fhir.r5.conformance.ProfileUtilities)20 ValueSet (org.hl7.fhir.r5.model.ValueSet)20 SpecialElement (org.hl7.fhir.r5.elementmodel.Element.SpecialElement)19 NamedElement (org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement)19 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)18 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)18 FileNotFoundException (java.io.FileNotFoundException)17 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)16 ContactPoint (org.hl7.fhir.r5.model.ContactPoint)16 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)16