-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
188 additions
and
55 deletions.
There are no files selected for viewing
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
55 changes: 55 additions & 0 deletions
55
android/engine/src/main/java/org/smartregister/fhircore/engine/di/FhirValidatorModule.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,55 @@ | ||
/* | ||
* Copyright 2021 Ona Systems, Inc | ||
* | ||
* 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://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 org.smartregister.fhircore.engine.di | ||
|
||
import ca.uhn.fhir.context.FhirContext | ||
import ca.uhn.fhir.validation.FhirValidator | ||
import ca.uhn.fhir.validation.ResultSeverityEnum | ||
import ca.uhn.fhir.validation.ValidationResult | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.components.SingletonComponent | ||
import javax.inject.Singleton | ||
import kotlin.coroutines.coroutineContext | ||
import kotlinx.coroutines.withContext | ||
import org.hl7.fhir.common.hapi.validation.validator.FhirInstanceValidator | ||
import org.hl7.fhir.r4.model.Resource | ||
|
||
@InstallIn(SingletonComponent::class) | ||
@Module | ||
class FhirValidatorModule { | ||
|
||
@Singleton | ||
@Provides | ||
fun provideFhirValidator(): FhirValidator { | ||
val fhirContext = FhirContext.forR4() | ||
val validatorModule = FhirInstanceValidator(fhirContext) | ||
return fhirContext.newValidator().apply { registerValidatorModule(validatorModule) } | ||
} | ||
} | ||
|
||
val ValidationResult.errorMessages | ||
get() = buildString { | ||
for (validationMsg in messages.filter { it.severity == ResultSeverityEnum.ERROR }) { | ||
appendLine("${validationMsg.message} - ${validationMsg.locationString}") | ||
} | ||
} | ||
|
||
suspend fun FhirValidator.checkResourceValid(resource: Resource): ValidationResult { | ||
return withContext(coroutineContext) { this@checkResourceValid.validateWithResult(resource) } | ||
} |
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
Oops, something went wrong.