Skip to content

Commit

Permalink
♻️ refactor: Refactor the validation of restrictions for FHIR referen…
Browse files Browse the repository at this point in the history
…ces, now doing it more accuretly and handle all configurations
  • Loading branch information
Tuncay Namli committed Jan 20, 2025
1 parent 5cc5efa commit be61401
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 116 deletions.
1 change: 1 addition & 0 deletions onfhir-common/src/main/scala/io/onfhir/api/api.scala
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ package object api {
val MONEY = "Money"
val SIMPLE_QUANTITY = "SimpleQuantity"
val MONEY_QUANTITY = "MoneyQuantity"
val CODEABLE_REFERENCE = "CodeableReference"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ case class FhirLogicalReference(rtype:Option[String], system:Option[String], ide
* @param version Version
* @param fragment Fragment
*/
case class FhirCanonicalReference(url:String, rtype:String, rid:String, version:Option[String], fragment:Option[String])extends FhirReference {
case class FhirCanonicalReference(url:String, rtype:String, rid:String, version:Option[String], fragment:Option[String]) extends FhirReference {
def getReference():String = s"$url/$rtype/$rid${version.map(v => "|"+v).getOrElse("")}${fragment.map(f => "#" + f).getOrElse("")}"

def getUrl():String = s"$url/$rtype/$rid"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,11 @@ class StructureDefinitionParser(fhirComplexTypes: Set[String], fhirPrimitiveType
}),
ConstraintKeys.REFERENCE_TARGET ->
dataTypeAndProfile
.find(_._1 == FHIR_DATA_TYPES.REFERENCE)
.map(dt => (dt._3, dt._4, dt._5))
.map {
case (targetProfiles, versioning, aggregation) =>
ReferenceRestrictions(targetProfiles,
versioning.map {
case "specific" => true
case "independent" => false
},
aggregation
)
}
.find(dtp =>
Set(FHIR_DATA_TYPES.REFERENCE, FHIR_DATA_TYPES.CODEABLE_REFERENCE, FHIR_DATA_TYPES.CANONICAL)
.contains(dtp._1)
)
.map(dt => ReferenceRestrictions(Set(dt._1), dt._3.toSet, dt._4, dt._5.toSet))
)
.filter(_._2.isDefined).map(r => r._1 -> r._2.get)
.toMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ class FhirContentValidator(
* @return
*/
private def evaluateReferenceConstraint(dataType: String, value: JValue, elementRestrictions: Seq[ElementRestrictions]): Seq[ConstraintFailure] = {
if (dataType == FHIR_DATA_TYPES.REFERENCE) {
if (dataType == FHIR_DATA_TYPES.REFERENCE || dataType == FHIR_DATA_TYPES.CODEABLE_REFERENCE) {
getAllRestrictions(ConstraintKeys.REFERENCE_TARGET, elementRestrictions) match {
case Nil => Nil
case Seq(r) => r.evaluate(value, this)
Expand All @@ -1226,6 +1226,7 @@ class FhirContentValidator(
.map(_.asInstanceOf[ReferenceRestrictions])
.reduceRight[ReferenceRestrictions]((r1,r2) =>
ReferenceRestrictions(
r1.referenceDataTypes ++ r2.referenceDataTypes,
if(r1.targetProfiles.nonEmpty) r1.targetProfiles else r2.targetProfiles,
if(r1.versioning.isDefined) r1.versioning else r2.versioning,
if(r1.aggregationMode.nonEmpty) r1.aggregationMode else r2.aggregationMode
Expand Down
Loading

0 comments on commit be61401

Please sign in to comment.