Search in sources :

Example 76 with NodeStack

use of org.hl7.fhir.validation.instance.utils.NodeStack in project org.hl7.fhir.core by hapifhir.

the class QuestionnaireValidator method validateQuestionnaireResponseItem.

public void validateQuestionnaireResponseItem(ValidatorHostContext hostContext, QuestionnaireWithContext qsrc, List<ValidationMessage> errors, Element element, NodeStack stack, boolean inProgress, Element questionnaireResponseRoot, QuestionnaireItemComponent qItem, List<ElementWithIndex> mapItem, QStack qstack) {
    boolean enabled = myEnableWhenEvaluator.isQuestionEnabled(hostContext, qItem, qstack, fpe);
    if (mapItem != null) {
        if (!enabled) {
            for (ElementWithIndex e : mapItem) {
                NodeStack ns = stack.push(e.getElement(), e.getElement().getIndex(), e.getElement().getProperty().getDefinition(), e.getElement().getProperty().getDefinition());
                rule(errors, IssueType.INVALID, e.getElement().line(), e.getElement().col(), ns.getLiteralPath(), enabled, I18nConstants.QUESTIONNAIRE_QR_ITEM_NOTENABLED2, qItem.getLinkId());
            }
        }
        // Recursively validate child items
        validateQuestionnaireResponseItem(hostContext, qsrc, qItem, errors, mapItem, stack, inProgress, questionnaireResponseRoot, qstack);
    } else {
        // item is missing, is the question enabled?
        if (enabled && qItem.getRequired()) {
            String message = context.formatMessage(I18nConstants.QUESTIONNAIRE_QR_ITEM_MISSING, qItem.getLinkId());
            if (inProgress) {
                warning(errors, IssueType.REQUIRED, element.line(), element.col(), stack.getLiteralPath(), false, message);
            } else {
                rule(errors, IssueType.REQUIRED, element.line(), element.col(), stack.getLiteralPath(), false, message);
            }
        }
    }
}
Also used : NodeStack(org.hl7.fhir.validation.instance.utils.NodeStack) ElementWithIndex(org.hl7.fhir.validation.instance.type.QuestionnaireValidator.ElementWithIndex)

Example 77 with NodeStack

use of org.hl7.fhir.validation.instance.utils.NodeStack in project org.hl7.fhir.core by hapifhir.

the class QuestionnaireValidator method checkDateOption.

private void checkDateOption(List<ValidationMessage> errors, Element answer, NodeStack stack, QuestionnaireWithContext qSrc, QuestionnaireItemComponent qItem, boolean openChoice) {
    Element v = answer.getNamedChild("valueDate");
    NodeStack ns = stack.push(v, -1, null, null);
    if (qItem.getAnswerOption().size() > 0) {
        List<DateType> list = new ArrayList<DateType>();
        for (QuestionnaireItemAnswerOptionComponent components : qItem.getAnswerOption()) {
            try {
                list.add(components.getValueDateType());
            } catch (FHIRException e) {
            // If it's the wrong type, just keep going
            }
        }
        if (list.isEmpty() && !openChoice) {
            rule(errors, IssueType.STRUCTURE, v.line(), v.col(), stack.getLiteralPath(), false, I18nConstants.QUESTIONNAIRE_QR_ITEM_NOOPTIONSDATE);
        } else {
            boolean found = false;
            for (DateType item : list) {
                if (item.getValue().equals(v.primitiveValue())) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                rule(errors, IssueType.STRUCTURE, v.line(), v.col(), stack.getLiteralPath(), found, I18nConstants.QUESTIONNAIRE_QR_ITEM_NODATE, v.primitiveValue());
            }
        }
    } else
        hint(errors, IssueType.STRUCTURE, v.line(), v.col(), stack.getLiteralPath(), false, I18nConstants.QUESTIONNAIRE_QR_ITEM_DATENOOPTIONS);
}
Also used : QuestionnaireItemAnswerOptionComponent(org.hl7.fhir.r5.model.Questionnaire.QuestionnaireItemAnswerOptionComponent) Element(org.hl7.fhir.r5.elementmodel.Element) ArrayList(java.util.ArrayList) NodeStack(org.hl7.fhir.validation.instance.utils.NodeStack) DateType(org.hl7.fhir.r5.model.DateType) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 78 with NodeStack

use of org.hl7.fhir.validation.instance.utils.NodeStack in project org.hl7.fhir.core by hapifhir.

the class QuestionnaireValidator method validateQuestionnaireResponseItem.

private void validateQuestionnaireResponseItem(ValidatorHostContext hostContext, QuestionnaireWithContext qsrc, QuestionnaireItemComponent qItem, List<ValidationMessage> errors, Element element, NodeStack stack, boolean inProgress, Element questionnaireResponseRoot, QStack qstack) {
    String text = element.getNamedChildValue("text");
    rule(errors, IssueType.INVALID, element.line(), element.col(), stack.getLiteralPath(), Utilities.noString(text) || text.equals(qItem.getText()), I18nConstants.QUESTIONNAIRE_QR_ITEM_TEXT, qItem.getLinkId());
    List<Element> answers = new ArrayList<Element>();
    element.getNamedChildren("answer", answers);
    if (inProgress)
        warning(errors, IssueType.REQUIRED, element.line(), element.col(), stack.getLiteralPath(), isAnswerRequirementFulfilled(qItem, answers), I18nConstants.QUESTIONNAIRE_QR_ITEM_MISSING, qItem.getLinkId());
    else if (myEnableWhenEvaluator.isQuestionEnabled(hostContext, qItem, qstack, fpe)) {
        rule(errors, IssueType.REQUIRED, element.line(), element.col(), stack.getLiteralPath(), isAnswerRequirementFulfilled(qItem, answers), I18nConstants.QUESTIONNAIRE_QR_ITEM_MISSING, qItem.getLinkId());
    } else if (!answers.isEmpty()) {
    // items without answers should be allowed, but not items with answers to questions that are disabled
    // it appears that this is always a duplicate error - it will always already have been reported, so no need to report it again?
    // GDG 2019-07-13
    // rule(errors, IssueType.INVALID, element.line(), element.col(), stack.getLiteralPath(), !isAnswerRequirementFulfilled(qItem, answers), I18nConstants.QUESTIONNAIRE_QR_ITEM_NOTENABLED, qItem.getLinkId());
    }
    if (answers.size() > 1)
        rule(errors, IssueType.INVALID, answers.get(1).line(), answers.get(1).col(), stack.getLiteralPath(), qItem.getRepeats(), I18nConstants.QUESTIONNAIRE_QR_ITEM_ONLYONEA);
    int i = 0;
    for (Element answer : answers) {
        NodeStack ns = stack.push(answer, i, null, null);
        if (qItem.getType() != null) {
            switch(qItem.getType()) {
                case GROUP:
                    rule(errors, IssueType.STRUCTURE, answer.line(), answer.col(), stack.getLiteralPath(), false, I18nConstants.QUESTIONNAIRE_QR_ITEM_GROUP);
                    break;
                case // nothing
                DISPLAY:
                    break;
                case BOOLEAN:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "boolean");
                    break;
                case DECIMAL:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "decimal");
                    break;
                case INTEGER:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "integer");
                    break;
                case DATE:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "date");
                    break;
                case DATETIME:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "dateTime");
                    break;
                case TIME:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "time");
                    break;
                case STRING:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "string");
                    break;
                case TEXT:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "text");
                    break;
                case URL:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "uri");
                    break;
                case ATTACHMENT:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "Attachment");
                    break;
                case REFERENCE:
                    validateQuestionnaireResponseItemType(errors, answer, ns, "Reference");
                    break;
                case QUANTITY:
                    if ("Quantity".equals(validateQuestionnaireResponseItemType(errors, answer, ns, "Quantity")))
                        if (qItem.hasExtension("???"))
                            validateQuestionnaireResponseItemQuantity(errors, answer, ns);
                    break;
                case CODING:
                    String itemType = validateQuestionnaireResponseItemType(errors, answer, ns, "Coding", "date", "time", "integer", "string");
                    if (itemType != null) {
                        if (itemType.equals("Coding"))
                            validateAnswerCode(errors, answer, ns, qsrc, qItem, false);
                        else if (itemType.equals("date"))
                            checkOption(errors, answer, ns, qsrc, qItem, "date");
                        else if (itemType.equals("time"))
                            checkOption(errors, answer, ns, qsrc, qItem, "time");
                        else if (itemType.equals("integer"))
                            checkOption(errors, answer, ns, qsrc, qItem, "integer");
                        else if (itemType.equals("string"))
                            checkOption(errors, answer, ns, qsrc, qItem, "string");
                    }
                    break;
                // case QUESTION:
                case NULL:
                    // no validation
                    break;
                case QUESTION:
                    throw new Error("Shouldn't get here?");
            }
        }
        if (qItem.getType() != QuestionnaireItemType.GROUP) {
            // if it's a group, we already have an error before getting here, so no need to hammer away on that
            validateQuestionannaireResponseItems(hostContext, qsrc, qItem.getItem(), errors, answer, stack, inProgress, questionnaireResponseRoot, qstack);
        }
        i++;
    }
    if (qItem.getType() == null) {
        fail(errors, IssueType.REQUIRED, element.line(), element.col(), stack.getLiteralPath(), false, I18nConstants.QUESTIONNAIRE_QR_ITEM_NOTYPE, qItem.getLinkId());
    } else if (qItem.getType() == QuestionnaireItemType.DISPLAY) {
        List<Element> items = new ArrayList<Element>();
        element.getNamedChildren("item", items);
        rule(errors, IssueType.STRUCTURE, element.line(), element.col(), stack.getLiteralPath(), items.isEmpty(), I18nConstants.QUESTIONNAIRE_QR_ITEM_DISPLAY, qItem.getLinkId());
    } else if (qItem.getType() != QuestionnaireItemType.GROUP) {
        List<Element> items = new ArrayList<Element>();
        element.getNamedChildren("item", items);
        rule(errors, IssueType.STRUCTURE, element.line(), element.col(), stack.getLiteralPath(), items.isEmpty(), I18nConstants.QUESTIONNAIRE_QR_ITEM_GROUP_ANSWER, qItem.getLinkId());
    } else {
        validateQuestionannaireResponseItems(hostContext, qsrc, qItem.getItem(), errors, element, stack, inProgress, questionnaireResponseRoot, qstack);
    }
}
Also used : Element(org.hl7.fhir.r5.elementmodel.Element) ArrayList(java.util.ArrayList) NodeStack(org.hl7.fhir.validation.instance.utils.NodeStack) List(java.util.List) ArrayList(java.util.ArrayList)

Example 79 with NodeStack

use of org.hl7.fhir.validation.instance.utils.NodeStack in project org.hl7.fhir.core by hapifhir.

the class StructureDefinitionValidator method validateBinding.

private void validateBinding(List<ValidationMessage> errors, Element binding, NodeStack stack, Set<String> typeCodes, boolean snapshot, String path) {
    rule(errors, IssueType.BUSINESSRULE, stack.getLiteralPath(), !snapshot || bindableType(typeCodes) != null, I18nConstants.SD_ED_BIND_NO_BINDABLE, path, typeCodes.toString());
    if (!snapshot) {
        Set<String> bindables = getListofBindableTypes(typeCodes);
        hint(errors, IssueType.BUSINESSRULE, stack.getLiteralPath(), bindables.size() <= 1, I18nConstants.SD_ED_BIND_MULTIPLE_TYPES, path, typeCodes.toString());
    }
    if (binding.hasChild("valueSet")) {
        Element valueSet = binding.getNamedChild("valueSet");
        String ref = valueSet.hasPrimitiveValue() ? valueSet.primitiveValue() : valueSet.getNamedChildValue("reference");
        if (warning(errors, IssueType.BUSINESSRULE, stack.getLiteralPath(), !snapshot || ref != null, I18nConstants.SD_ED_SHOULD_BIND_WITH_VS, path)) {
            Resource vs = context.fetchResource(Resource.class, ref);
            if (warning(errors, IssueType.BUSINESSRULE, stack.getLiteralPath(), vs != null || serverSupportsValueSet(ref), I18nConstants.SD_ED_BIND_UNKNOWN_VS, path, ref)) {
                if (vs != null) {
                    rule(errors, IssueType.BUSINESSRULE, stack.getLiteralPath(), vs instanceof ValueSet, I18nConstants.SD_ED_BIND_NOT_VS, path, ref, vs.fhirType());
                }
            }
        }
    }
}
Also used : Element(org.hl7.fhir.r5.elementmodel.Element) Resource(org.hl7.fhir.r5.model.Resource) ValueSet(org.hl7.fhir.r5.model.ValueSet)

Example 80 with NodeStack

use of org.hl7.fhir.validation.instance.utils.NodeStack in project org.hl7.fhir.core by hapifhir.

the class StructureDefinitionValidator method validateElementList.

private void validateElementList(List<ValidationMessage> errors, Element elementList, NodeStack stack, boolean snapshot, boolean hasSnapshot, StructureDefinition sd) {
    List<Element> elements = elementList.getChildrenByName("element");
    int cc = 0;
    for (Element element : elements) {
        validateElementDefinition(errors, element, stack.push(element, cc, null, null), snapshot, hasSnapshot, sd);
        cc++;
    }
}
Also used : Element(org.hl7.fhir.r5.elementmodel.Element)

Aggregations

Element (org.hl7.fhir.r5.elementmodel.Element)50 ArrayList (java.util.ArrayList)35 NodeStack (org.hl7.fhir.validation.instance.utils.NodeStack)32 FHIRException (org.hl7.fhir.exceptions.FHIRException)22 IndexedElement (org.hl7.fhir.validation.instance.utils.IndexedElement)22 IOException (java.io.IOException)14 SpecialElement (org.hl7.fhir.r5.elementmodel.Element.SpecialElement)14 NamedElement (org.hl7.fhir.r5.elementmodel.ParserBase.NamedElement)13 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)12 ContactPoint (org.hl7.fhir.r5.model.ContactPoint)12 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)12 ValueSet (org.hl7.fhir.r5.model.ValueSet)12 FHIRLexerException (org.hl7.fhir.r5.utils.FHIRLexer.FHIRLexerException)11 NotImplementedException (org.apache.commons.lang3.NotImplementedException)10 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)10 TerminologyServiceException (org.hl7.fhir.exceptions.TerminologyServiceException)10 ValidationResult (org.hl7.fhir.r5.context.IWorkerContext.ValidationResult)10 List (java.util.List)8 CodeSystem (org.hl7.fhir.r5.model.CodeSystem)8 Coding (org.hl7.fhir.r5.model.Coding)8