-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Constraint extension support (local and error case only) (#2199)
* Create a base interface for validator * Extend base interface to other validator * Make validate suspend for QuestionnaireResponseItemConstraintValidator * Add constraint-extension urls and severity types * Create ConstraintExtensionValidator * Put QR and expressionEvaluator in the validator constructor * Change parameter from answers to responseItem * Move the high order func for cqfCalculated value to the validator itself * Add constraint sample to catalog * Update tests * Update TODO comment * Use FHIRPathUtil convertToBoolean * spotlessApply * Rename ConstraintExtensionValidator to ConstraintItemExtensionValidator * Fix test
- Loading branch information
1 parent
a5db185
commit b157fd4
Showing
18 changed files
with
444 additions
and
146 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
catalog/src/main/assets/behavior_questionnaire_constraint.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
{ | ||
"resourceType": "Questionnaire", | ||
"item": [ | ||
{ | ||
"linkId": "1", | ||
"text": "Password", | ||
"type": "string", | ||
"item": [ | ||
{ | ||
"extension": [ | ||
{ | ||
"url": "http:https://hl7.org/fhir/StructureDefinition/questionnaire-displayCategory", | ||
"valueCodeableConcept": { | ||
"coding": [ | ||
{ | ||
"system": "http:https://hl7.org/fhir/questionnaire-display-category", | ||
"code": "instructions" | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"linkId": "1.1", | ||
"text": "Fill the password first", | ||
"type": "display" | ||
} | ||
] | ||
}, | ||
{ | ||
"extension": [ | ||
{ | ||
"url": "http:https://hl7.org/fhir/StructureDefinition/questionnaire-constraint", | ||
"extension": [ | ||
{ | ||
"url": "key", | ||
"valueId": "constraint-1" | ||
}, | ||
{ | ||
"url": "requirements", | ||
"valueString": "Confirm password field must have the same value as password field" | ||
}, | ||
{ | ||
"url": "severity", | ||
"valueCode": "error" | ||
}, | ||
{ | ||
"url": "expression", | ||
"valueString": "%context.answer.value = %resource.descendants().where(linkId='1').answer.value" | ||
}, | ||
{ | ||
"url": "human", | ||
"valueString": "Password does not match" | ||
}, | ||
{ | ||
"url": "location", | ||
"valueString": "1" | ||
} | ||
] | ||
} | ||
], | ||
"linkId": "2", | ||
"text": "Confirm password", | ||
"type": "string", | ||
"item": [ | ||
{ | ||
"extension": [ | ||
{ | ||
"url": "http:https://hl7.org/fhir/StructureDefinition/questionnaire-displayCategory", | ||
"valueCodeableConcept": { | ||
"coding": [ | ||
{ | ||
"system": "http:https://hl7.org/fhir/questionnaire-display-category", | ||
"code": "instructions" | ||
} | ||
] | ||
} | ||
} | ||
], | ||
"linkId": "2.1", | ||
"text": "Show error message if confirm password does not match with password", | ||
"type": "display" | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<vector | ||
xmlns:android="http:https://schemas.android.com/apk/res/android" | ||
android:width="48dp" | ||
android:height="48dp" | ||
android:viewportWidth="960" | ||
android:viewportHeight="960" | ||
> | ||
<path | ||
android:fillColor="#1A73E8" | ||
android:pathData="M576,800L534,758L645,647L534,536L576,494L687,605L798,494L840,536L729,647L840,758L798,800L687,689L576,800ZM659,426L517,284L559,242L659,341L838,162L880,205L659,426ZM80,670L80,610L440,610L440,670L80,670ZM80,350L80,290L440,290L440,350L80,350Z" | ||
/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...n/java/com/google/android/fhir/datacapture/validation/ConstraintItemExtensionValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2022-2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http:https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.fhir.datacapture.validation | ||
|
||
import android.content.Context | ||
import com.google.android.fhir.datacapture.extensions.ConstraintSeverityTypes | ||
import com.google.android.fhir.datacapture.extensions.EXTENSION_QUESTIONNAIRE_CONSTRAINT_EXPRESSION | ||
import com.google.android.fhir.datacapture.extensions.EXTENSION_QUESTIONNAIRE_CONSTRAINT_HUMAN | ||
import com.google.android.fhir.datacapture.extensions.EXTENSION_QUESTIONNAIRE_CONSTRAINT_SEVERITY | ||
import com.google.android.fhir.datacapture.extensions.EXTENSION_QUESTIONNAIRE_CONSTRAINT_URL | ||
import com.google.android.fhir.datacapture.extensions.asStringValue | ||
import com.google.android.fhir.datacapture.fhirpath.ExpressionEvaluator | ||
import com.google.android.fhir.datacapture.fhirpath.convertToBoolean | ||
import org.hl7.fhir.r4.model.CodeType | ||
import org.hl7.fhir.r4.model.Expression | ||
import org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent | ||
import org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseItemComponent | ||
|
||
/** | ||
* TODO: Add constraint support for global case, create a separate validator, | ||
* https://github.com/google/android-fhir/issues/2479 | ||
*/ | ||
internal class ConstraintItemExtensionValidator( | ||
private val expressionEvaluator: ExpressionEvaluator, | ||
) : QuestionnaireResponseItemConstraintValidator { | ||
override suspend fun validate( | ||
questionnaireItem: QuestionnaireItemComponent, | ||
questionnaireResponseItem: QuestionnaireResponseItemComponent, | ||
context: Context, | ||
): List<ConstraintValidator.Result> { | ||
return questionnaireItem.extension | ||
.filter { extension -> | ||
/** | ||
* TODO: Add constraint support for warning case, update the [ConstraintValidator.Result] | ||
* data class to also include warning state, | ||
* https://github.com/google/android-fhir/issues/2480 | ||
*/ | ||
extension.url == EXTENSION_QUESTIONNAIRE_CONSTRAINT_URL && | ||
(extension.getExtensionByUrl(EXTENSION_QUESTIONNAIRE_CONSTRAINT_SEVERITY).value | ||
as CodeType) | ||
.valueAsString == ConstraintSeverityTypes.ERROR.code | ||
} | ||
.map { extension -> | ||
val expression = | ||
Expression().apply { | ||
language = "text/fhirpath" | ||
expression = | ||
extension | ||
.getExtensionByUrl(EXTENSION_QUESTIONNAIRE_CONSTRAINT_EXPRESSION) | ||
.value | ||
.asStringValue() | ||
} | ||
val isValid = | ||
expressionEvaluator | ||
.evaluateExpression( | ||
questionnaireItem, | ||
questionnaireResponseItem, | ||
expression, | ||
) | ||
.let { convertToBoolean(it) } | ||
if (isValid) { | ||
ConstraintValidator.Result(true, null) | ||
} else { | ||
val errorMessage = | ||
extension | ||
.getExtensionByUrl(EXTENSION_QUESTIONNAIRE_CONSTRAINT_HUMAN) | ||
.value | ||
.asStringValue() | ||
ConstraintValidator.Result(false, errorMessage) | ||
} | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...pture/src/main/java/com/google/android/fhir/datacapture/validation/ConstraintValidator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2022-2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http:https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.fhir.datacapture.validation | ||
|
||
/** Validator base interface. */ | ||
internal interface ConstraintValidator { | ||
|
||
/** | ||
* The validation result containing whether the response item is valid and any error message if it | ||
* is not valid. | ||
*/ | ||
data class Result(val isValid: Boolean, val errorMessage: String?) | ||
} |
Oops, something went wrong.