Search in sources :

Example 81 with ValidationMessage

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

the class InstanceValidator method startInner.

public void startInner(ValidatorHostContext hostContext, List<ValidationMessage> errors, Element resource, Element element, StructureDefinition defn, NodeStack stack, boolean checkSpecials) {
    // the first piece of business is to see if we've validated this resource against this profile before.
    // if we have (*or if we still are*), then we'll just return our existing errors
    ResourceValidationTracker resTracker = getResourceTracker(element);
    List<ValidationMessage> cachedErrors = resTracker.getOutcomes(defn);
    if (cachedErrors != null) {
        for (ValidationMessage vm : cachedErrors) {
            if (!errors.contains(vm)) {
                errors.add(vm);
            }
        }
        return;
    }
    if (rule(errors, IssueType.STRUCTURE, element.line(), element.col(), stack.getLiteralPath(), defn.hasSnapshot(), I18nConstants.VALIDATION_VAL_PROFILE_NOSNAPSHOT, defn.getUrl())) {
        List<ValidationMessage> localErrors = new ArrayList<ValidationMessage>();
        resTracker.startValidating(defn);
        trackUsage(defn, hostContext, element);
        validateElement(hostContext, localErrors, defn, defn.getSnapshot().getElement().get(0), null, null, resource, element, element.getName(), stack, false, true, null);
        resTracker.storeOutcomes(defn, localErrors);
        for (ValidationMessage vm : localErrors) {
            if (!errors.contains(vm)) {
                errors.add(vm);
            }
        }
    }
    if (checkSpecials) {
        checkSpecials(hostContext, errors, element, stack, checkSpecials);
        validateResourceRules(errors, element, stack);
    }
}
Also used : ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) ResourceValidationTracker(org.hl7.fhir.validation.instance.utils.ResourceValidationTracker) ArrayList(java.util.ArrayList)

Example 82 with ValidationMessage

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

the class InstanceValidator method checkChildByDefinition.

public void checkChildByDefinition(ValidatorHostContext hostContext, List<ValidationMessage> errors, StructureDefinition profile, ElementDefinition definition, Element resource, Element element, String actualType, NodeStack stack, boolean inCodeableConcept, boolean checkDisplayInContext, ElementInfo ei, String extensionUrl, ElementDefinition checkDefn, boolean isSlice) {
    List<String> profiles = new ArrayList<String>();
    String type = null;
    ElementDefinition typeDefn = null;
    checkMustSupport(profile, ei);
    if (checkDefn.getType().size() == 1 && !"*".equals(checkDefn.getType().get(0).getWorkingCode()) && !"Element".equals(checkDefn.getType().get(0).getWorkingCode()) && !"BackboneElement".equals(checkDefn.getType().get(0).getWorkingCode())) {
        type = checkDefn.getType().get(0).getWorkingCode();
        String stype = ei.getElement().fhirType();
        if (checkDefn.isChoice() && !stype.equals(type)) {
            if ("Extension".equals(profile.getType())) {
            // error will be raised elsewhere
            } else {
                rule(errors, IssueType.STRUCTURE, element.line(), element.col(), ei.getPath(), false, I18nConstants.EXTENSION_PROF_TYPE, profile.getUrl(), type, stype);
            }
        }
        // Excluding reference is a kludge to get around versioning issues
        if (checkDefn.getType().get(0).hasProfile()) {
            for (CanonicalType p : checkDefn.getType().get(0).getProfile()) {
                profiles.add(p.getValue());
            }
        }
    } else if (checkDefn.getType().size() == 1 && "*".equals(checkDefn.getType().get(0).getWorkingCode())) {
        String prefix = tail(checkDefn.getPath());
        assert prefix.endsWith("[x]");
        type = ei.getName().substring(prefix.length() - 3);
        if (isPrimitiveType(type))
            type = Utilities.uncapitalize(type);
        if (checkDefn.getType().get(0).hasProfile()) {
            for (CanonicalType p : checkDefn.getType().get(0).getProfile()) {
                profiles.add(p.getValue());
            }
        }
    } else if (checkDefn.getType().size() > 1) {
        String prefix = tail(checkDefn.getPath());
        assert typesAreAllReference(checkDefn.getType()) || checkDefn.hasRepresentation(PropertyRepresentation.TYPEATTR) || prefix.endsWith("[x]") || isResourceAndTypes(checkDefn) : "Multiple Types allowed, but name is wrong @ " + checkDefn.getPath() + ": " + checkDefn.typeSummaryVB();
        if (checkDefn.hasRepresentation(PropertyRepresentation.TYPEATTR)) {
            type = ei.getElement().getType();
        } else if (ei.getElement().isResource()) {
            type = ei.getElement().fhirType();
        } else {
            prefix = prefix.substring(0, prefix.length() - 3);
            for (TypeRefComponent t : checkDefn.getType()) if ((prefix + Utilities.capitalize(t.getWorkingCode())).equals(ei.getName())) {
                type = t.getWorkingCode();
                // Excluding reference is a kludge to get around versioning issues
                if (t.hasProfile() && !type.equals("Reference"))
                    profiles.add(t.getProfile().get(0).getValue());
            }
        }
        if (type == null) {
            TypeRefComponent trc = checkDefn.getType().get(0);
            if (trc.getWorkingCode().equals("Reference"))
                type = "Reference";
            else
                rule(errors, IssueType.STRUCTURE, ei.line(), ei.col(), stack.getLiteralPath(), false, I18nConstants.VALIDATION_VAL_PROFILE_NOTYPE, ei.getName(), describeTypes(checkDefn.getType()));
        }
    } else if (checkDefn.getContentReference() != null) {
        typeDefn = resolveNameReference(profile.getSnapshot(), checkDefn.getContentReference());
    } else if (checkDefn.getType().size() == 1 && ("Element".equals(checkDefn.getType().get(0).getWorkingCode()) || "BackboneElement".equals(checkDefn.getType().get(0).getWorkingCode()))) {
        if (checkDefn.getType().get(0).hasProfile()) {
            CanonicalType pu = checkDefn.getType().get(0).getProfile().get(0);
            if (pu.hasExtension(ToolingExtensions.EXT_PROFILE_ELEMENT))
                profiles.add(pu.getValue() + "#" + pu.getExtensionString(ToolingExtensions.EXT_PROFILE_ELEMENT));
            else
                profiles.add(pu.getValue());
        }
    }
    if (type != null) {
        if (type.startsWith("@")) {
            checkDefn = findElement(profile, type.substring(1));
            if (isSlice) {
                ei.slice = ei.definition;
            } else {
                ei.definition = ei.definition;
            }
            type = null;
        }
    }
    NodeStack localStack = stack.push(ei.getElement(), "*".equals(ei.getDefinition().getBase().getMax()) && ei.count == -1 ? 0 : ei.count, checkDefn, type == null ? typeDefn : resolveType(type, checkDefn.getType()));
    // if (debug) {
    // System.out.println("  check " + localStack.getLiteralPath()+" against "+ei.getDefinition().getId()+" in profile "+profile.getUrl());
    // }
    String localStackLiteralPath = localStack.getLiteralPath();
    String eiPath = ei.getPath();
    if (!eiPath.equals(localStackLiteralPath)) {
        assert (eiPath.equals(localStackLiteralPath)) : "ei.path: " + ei.getPath() + "  -  localStack.getLiteralPath: " + localStackLiteralPath;
    }
    boolean thisIsCodeableConcept = false;
    String thisExtension = null;
    boolean checkDisplay = true;
    SpecialElement special = ei.getElement().getSpecial();
    if (special == SpecialElement.BUNDLE_ENTRY || special == SpecialElement.BUNDLE_OUTCOME || special == SpecialElement.PARAMETER) {
        checkInvariants(hostContext, errors, profile, typeDefn != null ? typeDefn : checkDefn, ei.getElement(), ei.getElement(), localStack, false);
    } else {
        checkInvariants(hostContext, errors, profile, typeDefn != null ? typeDefn : checkDefn, resource, ei.getElement(), localStack, false);
    }
    ei.getElement().markValidation(profile, checkDefn);
    boolean elementValidated = false;
    if (type != null) {
        if (isPrimitiveType(type)) {
            checkPrimitive(hostContext, errors, ei.getPath(), type, checkDefn, ei.getElement(), profile, stack);
        } else {
            if (checkDefn.hasFixed()) {
                checkFixedValue(errors, ei.getPath(), ei.getElement(), checkDefn.getFixed(), profile.getUrl(), checkDefn.getSliceName(), null, false);
            }
            if (checkDefn.hasPattern()) {
                checkFixedValue(errors, ei.getPath(), ei.getElement(), checkDefn.getPattern(), profile.getUrl(), checkDefn.getSliceName(), null, true);
            }
        }
        if (type.equals("Identifier")) {
            checkIdentifier(errors, ei.getPath(), ei.getElement(), checkDefn);
        } else if (type.equals("Coding")) {
            checkCoding(errors, ei.getPath(), ei.getElement(), profile, checkDefn, inCodeableConcept, checkDisplayInContext, stack);
        } else if (type.equals("Quantity")) {
            checkQuantity(errors, ei.getPath(), ei.getElement(), profile, checkDefn, stack);
        } else if (type.equals("Attachment")) {
            checkAttachment(errors, ei.getPath(), ei.getElement(), profile, checkDefn, inCodeableConcept, checkDisplayInContext, stack);
        } else if (type.equals("CodeableConcept")) {
            checkDisplay = checkCodeableConcept(errors, ei.getPath(), ei.getElement(), profile, checkDefn, stack);
            thisIsCodeableConcept = true;
        } else if (type.equals("Reference")) {
            checkReference(hostContext, errors, ei.getPath(), ei.getElement(), profile, checkDefn, actualType, localStack);
        // We only check extensions if we're not in a complex extension or if the element we're dealing with is not defined as part of that complex extension
        } else if (type.equals("Extension")) {
            Element eurl = ei.getElement().getNamedChild("url");
            if (rule(errors, IssueType.INVALID, ei.getPath(), eurl != null, I18nConstants.EXTENSION_EXT_URL_NOTFOUND)) {
                String url = eurl.primitiveValue();
                thisExtension = url;
                if (rule(errors, IssueType.INVALID, ei.getPath(), !Utilities.noString(url), I18nConstants.EXTENSION_EXT_URL_NOTFOUND)) {
                    if (rule(errors, IssueType.INVALID, ei.getPath(), (extensionUrl != null) || Utilities.isAbsoluteUrl(url), I18nConstants.EXTENSION_EXT_URL_ABSOLUTE)) {
                        checkExtension(hostContext, errors, ei.getPath(), resource, element, ei.getElement(), checkDefn, profile, localStack, stack, extensionUrl);
                    }
                }
            }
        } else if (type.equals("Resource") || isResource(type)) {
            validateContains(hostContext, errors, ei.getPath(), checkDefn, definition, resource, ei.getElement(), localStack, idStatusForEntry(element, ei), // if
            profile);
            elementValidated = true;
        // (str.matches(".*([.,/])work\\1$"))
        } else if (Utilities.isAbsoluteUrl(type)) {
            StructureDefinition defn = context.fetchTypeDefinition(type);
            if (defn != null && hasMapping("http://hl7.org/fhir/terminology-pattern", defn, defn.getSnapshot().getElementFirstRep())) {
                List<String> txtype = getMapping("http://hl7.org/fhir/terminology-pattern", defn, defn.getSnapshot().getElementFirstRep());
                if (txtype.contains("CodeableConcept")) {
                    checkTerminologyCodeableConcept(errors, ei.getPath(), ei.getElement(), profile, checkDefn, stack, defn);
                    thisIsCodeableConcept = true;
                } else if (txtype.contains("Coding")) {
                    checkTerminologyCoding(errors, ei.getPath(), ei.getElement(), profile, checkDefn, inCodeableConcept, checkDisplayInContext, stack, defn);
                }
            }
        }
    } else {
        if (rule(errors, IssueType.STRUCTURE, ei.line(), ei.col(), stack.getLiteralPath(), checkDefn != null, I18nConstants.VALIDATION_VAL_CONTENT_UNKNOWN, ei.getName()))
            validateElement(hostContext, errors, profile, checkDefn, null, null, resource, ei.getElement(), type, localStack, false, true, null);
    }
    StructureDefinition p = null;
    String tail = null;
    if (profiles.isEmpty()) {
        if (type != null) {
            p = getProfileForType(type, checkDefn.getType());
            // If dealing with a primitive type, then we need to check the current child against
            // the invariants (constraints) on the current element, because otherwise it only gets
            // checked against the primary type's invariants: LLoyd
            // if (p.getKind() == StructureDefinitionKind.PRIMITIVETYPE) {
            // checkInvariants(hostContext, errors, ei.path, profile, ei.definition, null, null, resource, ei.element);
            // }
            rule(errors, IssueType.STRUCTURE, ei.line(), ei.col(), ei.getPath(), p != null, I18nConstants.VALIDATION_VAL_NOTYPE, type);
        }
    } else if (profiles.size() == 1) {
        String url = profiles.get(0);
        if (url.contains("#")) {
            tail = url.substring(url.indexOf("#") + 1);
            url = url.substring(0, url.indexOf("#"));
        }
        p = this.context.fetchResource(StructureDefinition.class, url);
        rule(errors, IssueType.STRUCTURE, ei.line(), ei.col(), ei.getPath(), p != null, I18nConstants.VALIDATION_VAL_UNKNOWN_PROFILE, profiles.get(0));
    } else {
        elementValidated = true;
        HashMap<String, List<ValidationMessage>> goodProfiles = new HashMap<String, List<ValidationMessage>>();
        HashMap<String, List<ValidationMessage>> badProfiles = new HashMap<String, List<ValidationMessage>>();
        for (String typeProfile : profiles) {
            String url = typeProfile;
            tail = null;
            if (url.contains("#")) {
                tail = url.substring(url.indexOf("#") + 1);
                url = url.substring(0, url.indexOf("#"));
            }
            p = this.context.fetchResource(StructureDefinition.class, typeProfile);
            if (rule(errors, IssueType.STRUCTURE, ei.line(), ei.col(), ei.getPath(), p != null, I18nConstants.VALIDATION_VAL_UNKNOWN_PROFILE, typeProfile)) {
                List<ValidationMessage> profileErrors = new ArrayList<ValidationMessage>();
                validateElement(hostContext, profileErrors, p, getElementByTail(p, tail), profile, checkDefn, resource, ei.getElement(), type, localStack, thisIsCodeableConcept, checkDisplay, thisExtension);
                if (hasErrors(profileErrors))
                    badProfiles.put(typeProfile, profileErrors);
                else
                    goodProfiles.put(typeProfile, profileErrors);
            }
        }
        if (goodProfiles.size() == 1) {
            errors.addAll(goodProfiles.values().iterator().next());
        } else if (goodProfiles.size() == 0) {
            rule(errors, IssueType.STRUCTURE, ei.line(), ei.col(), ei.getPath(), false, I18nConstants.VALIDATION_VAL_PROFILE_NOMATCH, StringUtils.join("; ", profiles));
            for (String m : badProfiles.keySet()) {
                p = this.context.fetchResource(StructureDefinition.class, m);
                for (ValidationMessage message : badProfiles.get(m)) {
                    message.setMessage(message.getMessage() + " (validating against " + p.getUrl() + (p.hasVersion() ? "|" + p.getVersion() : "") + " [" + p.getName() + "])");
                    errors.add(message);
                }
            }
        } else {
            warning(errors, IssueType.STRUCTURE, ei.line(), ei.col(), ei.getPath(), false, I18nConstants.VALIDATION_VAL_PROFILE_MULTIPLEMATCHES, StringUtils.join("; ", goodProfiles.keySet()));
            for (String m : goodProfiles.keySet()) {
                p = this.context.fetchResource(StructureDefinition.class, m);
                for (ValidationMessage message : goodProfiles.get(m)) {
                    message.setMessage(message.getMessage() + " (validating against " + p.getUrl() + (p.hasVersion() ? "|" + p.getVersion() : "") + " [" + p.getName() + "])");
                    errors.add(message);
                }
            }
        }
    }
    if (p != null) {
        trackUsage(p, hostContext, element);
        if (!elementValidated) {
            if (ei.getElement().getSpecial() == SpecialElement.BUNDLE_ENTRY || ei.getElement().getSpecial() == SpecialElement.BUNDLE_OUTCOME || ei.getElement().getSpecial() == SpecialElement.PARAMETER)
                validateElement(hostContext, errors, p, getElementByTail(p, tail), profile, checkDefn, ei.getElement(), ei.getElement(), type, localStack.resetIds(), thisIsCodeableConcept, checkDisplay, thisExtension);
            else
                validateElement(hostContext, errors, p, getElementByTail(p, tail), profile, checkDefn, resource, ei.getElement(), type, localStack, thisIsCodeableConcept, checkDisplay, thisExtension);
        }
        int index = profile.getSnapshot().getElement().indexOf(checkDefn);
        if (index < profile.getSnapshot().getElement().size() - 1) {
            String nextPath = profile.getSnapshot().getElement().get(index + 1).getPath();
            if (!nextPath.equals(checkDefn.getPath()) && nextPath.startsWith(checkDefn.getPath()))
                validateElement(hostContext, errors, profile, checkDefn, null, null, resource, ei.getElement(), type, localStack, thisIsCodeableConcept, checkDisplay, thisExtension);
        }
    }
}
Also used : ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage) HashMap(java.util.HashMap) 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) SpecialElement(org.hl7.fhir.r5.elementmodel.Element.SpecialElement) NodeStack(org.hl7.fhir.validation.instance.utils.NodeStack) CanonicalType(org.hl7.fhir.r5.model.CanonicalType) ContactPoint(org.hl7.fhir.r5.model.ContactPoint) StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) TypeRefComponent(org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent) ArrayList(java.util.ArrayList) List(java.util.List) TypedElementDefinition(org.hl7.fhir.r5.utils.FHIRPathEngine.TypedElementDefinition) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition)

Example 83 with ValidationMessage

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

the class InstanceValidator method checkQuantity.

private void checkQuantity(List<ValidationMessage> errors, String path, Element element, StructureDefinition theProfile, ElementDefinition definition, NodeStack theStack) {
    String value = element.hasChild("value") ? element.getNamedChild("value").getValue() : null;
    String unit = element.hasChild("unit") ? element.getNamedChild("unit").getValue() : null;
    String system = element.hasChild("system") ? element.getNamedChild("system").getValue() : null;
    String code = element.hasChild("code") ? element.getNamedChild("code").getValue() : null;
    if (!Utilities.noString(value) && definition.hasExtension("http://hl7.org/fhir/StructureDefinition/maxDecimalPlaces")) {
        int dp = value.contains(".") ? value.substring(value.indexOf(".") + 1).length() : 0;
        int def = Integer.parseInt(ToolingExtensions.readStringExtension(definition, "http://hl7.org/fhir/StructureDefinition/maxDecimalPlaces"));
        rule(errors, IssueType.STRUCTURE, element.line(), element.col(), path, dp <= def, I18nConstants.TYPE_SPECIFIC_CHECKS_DT_DECIMAL_CHARS, dp, def);
    }
    if (system != null || code != null) {
        checkCodedElement(errors, path, element, theProfile, definition, false, false, theStack, code, system, null, unit);
    }
    if (code != null && "http://unitsofmeasure.org".equals(system)) {
        int b = code.indexOf("{");
        int e = code.indexOf("}");
        if (b >= 0 && e > 0 && b < e) {
            bpCheck(errors, IssueType.BUSINESSRULE, element.line(), element.col(), path, !code.contains("{"), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_NO_ANNOTATIONS, code.substring(b, e + 1));
        }
    }
    if (definition.hasMinValue()) {
        if (warning(errors, IssueType.INVALID, element.line(), element.col(), path, !Utilities.noString(value), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MIN_VALUE_NO_VALUE) && rule(errors, IssueType.INVALID, element.line(), element.col(), path, definition.getMinValue() instanceof Quantity, I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MIN_NO_QTY, definition.getMinValue().fhirType())) {
            Quantity min = definition.getMinValueQuantity();
            if (warning(errors, IssueType.INVALID, element.line(), element.col(), path, !Utilities.noString(min.getSystem()), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MIN_MIN_NO_SYSTEM) && warning(errors, IssueType.INVALID, element.line(), element.col(), path, !Utilities.noString(system), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MIN_VALUE_NO_SYSTEM) && warning(errors, IssueType.INVALID, element.line(), element.col(), path, system.equals(min.getSystem()), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MIN_SYSTEM_MISMATCH, system, min.getSystem()) && warning(errors, IssueType.INVALID, element.line(), element.col(), path, !Utilities.noString(min.getCode()), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MIN_MIN_NO_CODE) && warning(errors, IssueType.INVALID, element.line(), element.col(), path, !Utilities.noString(code), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MIN_VALUE_NO_CODE) && rule(errors, IssueType.INVALID, element.line(), element.col(), path, definition.getMinValueQuantity().hasValue(), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MIN_MIN_NO_VALUE)) {
                if (code.equals(min.getCode())) {
                    // straight value comparison
                    rule(errors, IssueType.INVALID, element.line(), element.col(), path, checkDecimalMinValue(value, min.getValue()), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MIN_VALUE_WRONG, value, min.getValue().toString());
                } else if ("http://unitsofmeasure.org".equals(system)) {
                    if (warning(errors, IssueType.INVALID, element.line(), element.col(), path, context.getUcumService() != null, I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MIN_NO_UCUM_SVC)) {
                        Decimal v = convertUcumValue(value, code, min.getCode());
                        if (rule(errors, IssueType.INVALID, element.line(), element.col(), path, v != null, I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MIN_MIN_NO_CONVERT, value, code, min.getCode())) {
                            rule(errors, IssueType.INVALID, element.line(), element.col(), path, checkDecimalMinValue(v, min.getValue()), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MIN_VALUE_WRONG_UCUM, value, code, min.getValue().toString(), min.getCode());
                        }
                    }
                } else {
                    warning(errors, IssueType.INVALID, element.line(), element.col(), path, false, I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MIN_CODE_MISMATCH, code, min.getCode());
                }
            }
        }
    }
    if (definition.hasMaxValue()) {
        if (warning(errors, IssueType.INVALID, element.line(), element.col(), path, !Utilities.noString(value), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MAX_VALUE_NO_VALUE) && rule(errors, IssueType.INVALID, element.line(), element.col(), path, definition.getMaxValue() instanceof Quantity, I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MAX_NO_QTY, definition.getMaxValue().fhirType())) {
            Quantity max = definition.getMaxValueQuantity();
            if (warning(errors, IssueType.INVALID, element.line(), element.col(), path, !Utilities.noString(max.getSystem()), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MAX_MIN_NO_SYSTEM) && warning(errors, IssueType.INVALID, element.line(), element.col(), path, !Utilities.noString(system), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MAX_VALUE_NO_SYSTEM) && warning(errors, IssueType.INVALID, element.line(), element.col(), path, system.equals(max.getSystem()), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MAX_SYSTEM_MISMATCH, system, max.getSystem()) && warning(errors, IssueType.INVALID, element.line(), element.col(), path, !Utilities.noString(max.getCode()), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MAX_MIN_NO_CODE) && warning(errors, IssueType.INVALID, element.line(), element.col(), path, !Utilities.noString(code), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MAX_VALUE_NO_CODE) && rule(errors, IssueType.INVALID, element.line(), element.col(), path, definition.getMaxValueQuantity().hasValue(), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MAX_MIN_NO_VALUE)) {
                if (code.equals(max.getCode())) {
                    // straight value comparison
                    rule(errors, IssueType.INVALID, element.line(), element.col(), path, checkDecimalMaxValue(value, max.getValue()), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MAX_VALUE_WRONG, value, max.getValue().toString());
                } else if ("http://unitsofmeasure.org".equals(system)) {
                    if (warning(errors, IssueType.INVALID, element.line(), element.col(), path, context.getUcumService() != null, I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MAX_NO_UCUM_SVC)) {
                        Decimal v = convertUcumValue(value, code, max.getCode());
                        if (rule(errors, IssueType.INVALID, element.line(), element.col(), path, v != null, I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MAX_MIN_NO_CONVERT, value, code, max.getCode())) {
                            rule(errors, IssueType.INVALID, element.line(), element.col(), path, checkDecimalMaxValue(v, max.getValue()), I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MAX_VALUE_WRONG_UCUM, value, code, max.getValue().toString(), max.getCode());
                        }
                    }
                } else {
                    warning(errors, IssueType.INVALID, element.line(), element.col(), path, false, I18nConstants.TYPE_SPECIFIC_CHECKS_DT_QTY_MAX_CODE_MISMATCH, code, max.getCode());
                }
            }
        }
    }
}
Also used : BigDecimal(java.math.BigDecimal) Decimal(org.fhir.ucum.Decimal) Quantity(org.hl7.fhir.r5.model.Quantity) ContactPoint(org.hl7.fhir.r5.model.ContactPoint)

Example 84 with ValidationMessage

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

the class InstanceValidator method checkTerminologyCoding.

private void checkTerminologyCoding(List<ValidationMessage> errors, String path, Element element, StructureDefinition profile, ElementDefinition theElementCntext, boolean inCodeableConcept, boolean checkDisplay, NodeStack stack, StructureDefinition logical) {
    Coding c = convertToCoding(element, logical);
    String code = c.getCode();
    String system = c.getSystem();
    String display = c.getDisplay();
    String version = c.getVersion();
    rule(errors, IssueType.CODEINVALID, element.line(), element.col(), path, system == null || isCodeSystemReferenceValid(system), I18nConstants.TERMINOLOGY_TX_SYSTEM_RELATIVE);
    if (system != null && code != null && !noTerminologyChecks) {
        rule(errors, IssueType.CODEINVALID, element.line(), element.col(), path, !isValueSet(system), I18nConstants.TERMINOLOGY_TX_SYSTEM_VALUESET2, system);
        try {
            if (checkCode(errors, element, path, code, system, version, display, checkDisplay, stack))
                if (theElementCntext != null && theElementCntext.hasBinding()) {
                    ElementDefinitionBindingComponent binding = theElementCntext.getBinding();
                    if (warning(errors, IssueType.CODEINVALID, element.line(), element.col(), path, binding != null, I18nConstants.TERMINOLOGY_TX_BINDING_MISSING2, 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 {
                                    long t = System.nanoTime();
                                    ValidationResult vr = null;
                                    if (binding.getStrength() != BindingStrength.EXAMPLE) {
                                        vr = checkCodeOnServer(stack, valueset, c, true);
                                    }
                                    if (binding.getStrength() == BindingStrength.REQUIRED) {
                                        removeTrackedMessagesForLocation(errors, element, path);
                                    }
                                    timeTracker.tx(t, "vc " + system + "#" + code + " '" + display + "'");
                                    if (vr != null && !vr.isOk()) {
                                        if (vr.IsNoService())
                                            txHint(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_BINDING_NOSERVER);
                                        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_4a, describeReference(binding.getValueSet(), valueset), vr.getMessage(), system + "#" + code);
                                            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"), c, stack);
                                                else if (!noExtensibleWarnings)
                                                    txWarningForLaterRemoval(element, errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_CONFIRM_5, describeReference(binding.getValueSet(), valueset));
                                            } else if (binding.getStrength() == BindingStrength.PREFERRED) {
                                                if (baseOnly) {
                                                    txHint(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_CONFIRM_6, describeReference(binding.getValueSet(), valueset));
                                                }
                                            }
                                        } else if (binding.getStrength() == BindingStrength.REQUIRED)
                                            txRule(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_4, describeReference(binding.getValueSet(), valueset), (vr.getMessage() != null ? " (error message = " + vr.getMessage() + ")" : ""), system + "#" + code);
                                        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"), c, stack);
                                            else
                                                txWarning(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_5, describeReference(binding.getValueSet(), valueset), (vr.getMessage() != null ? " (error message = " + vr.getMessage() + ")" : ""), system + "#" + code);
                                        } else if (binding.getStrength() == BindingStrength.PREFERRED) {
                                            if (baseOnly) {
                                                txHint(errors, vr.getTxLink(), IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_NOVALID_6, describeReference(binding.getValueSet(), valueset), (vr.getMessage() != null ? " (error message = " + vr.getMessage() + ")" : ""), system + "#" + code);
                                            }
                                        }
                                    }
                                } catch (Exception e) {
                                    if (STACK_TRACE)
                                        e.printStackTrace();
                                    warning(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_ERROR_CODING1, e.getMessage());
                                }
                            }
                        } else if (binding.hasValueSet()) {
                            hint(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_BINDING_CANTCHECK);
                        } else if (!inCodeableConcept && !noBindingMsgSuppressed) {
                            hint(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_BINDING_NOSOURCE, path);
                        }
                    }
                }
        } catch (Exception e) {
            if (STACK_TRACE)
                e.printStackTrace();
            rule(errors, IssueType.CODEINVALID, element.line(), element.col(), path, false, I18nConstants.TERMINOLOGY_TX_ERROR_CODING2, e.getMessage(), e.toString());
        }
    }
}
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)

Example 85 with ValidationMessage

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

the class InstanceValidator method validateCapabilityStatement.

private void validateCapabilityStatement(List<ValidationMessage> errors, Element cs, NodeStack stack) {
    int iRest = 0;
    for (Element rest : cs.getChildrenByName("rest")) {
        int iResource = 0;
        for (Element resource : rest.getChildrenByName(RESOURCE)) {
            int iSP = 0;
            for (Element searchParam : resource.getChildrenByName("searchParam")) {
                String ref = searchParam.getChildValue("definition");
                String type = searchParam.getChildValue(TYPE);
                if (!Utilities.noString(ref)) {
                    SearchParameter sp = context.fetchResource(SearchParameter.class, ref);
                    if (sp != null) {
                        rule(errors, IssueType.INVALID, searchParam.line(), searchParam.col(), stack.getLiteralPath() + ".rest[" + iRest + "].resource[" + iResource + "].searchParam[" + iSP + "]", sp.getType().toCode().equals(type), I18nConstants.CAPABALITYSTATEMENT_CS_SP_WRONGTYPE, sp.getUrl(), sp.getType().toCode(), type);
                    }
                }
                iSP++;
            }
            iResource++;
        }
        iRest++;
    }
}
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) SearchParameter(org.hl7.fhir.r5.model.SearchParameter) ContactPoint(org.hl7.fhir.r5.model.ContactPoint)

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