Skip to content

Commit 10769fe

Browse files
committed
🎨 Schema to Structure Definition conversion is diversified with respect to the existing flat schemas, other examples and onfhir validation rules.
1 parent 991159c commit 10769fe

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

tofhir-common/src/main/scala/io/tofhir/common/util/SchemaUtil.scala

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ object SchemaUtil {
1616
* @return
1717
*/
1818
def convertToStructureDefinitionResource(schemaDefinition: SchemaDefinition, fhirVersion: String): Resource = {
19+
20+
val deepSchema = isDeepSchema(schemaDefinition)
21+
val kindVal = if (deepSchema) "resource" else "logical"
22+
val derivationVal = if (deepSchema) "constraint" else "specialization"
23+
1924
var structureDefinitionResource: Resource =
2025
("id" -> schemaDefinition.id) ~
2126
("resourceType" -> "StructureDefinition") ~
@@ -28,11 +33,11 @@ object SchemaUtil {
2833
structureDefinitionResource ~
2934
("status" -> "draft") ~
3035
("fhirVersion" -> fhirVersion) ~
31-
("kind" -> "resource") ~
36+
("kind" -> kindVal) ~
3237
("abstract" -> false) ~
3338
("type" -> schemaDefinition.`type`) ~
3439
("baseDefinition" -> "http://hl7.org/fhir/StructureDefinition/Element") ~
35-
("derivation" -> "constraint") ~
40+
("derivation" -> derivationVal) ~
3641
("differential" -> ("element" -> generateElementArray(schemaDefinition.id, schemaDefinition.`type`, schemaDefinition.fieldDefinitions.getOrElse(Seq.empty))))
3742
}
3843

@@ -74,16 +79,18 @@ object SchemaUtil {
7479

7580
val flatFds = flatten(fieldDefinitions)
7681

77-
// Validate types after flatten (defensive — we rely on types to build a correct SD)
82+
// Validate types after flatten
7883
val integrityCheck = flatFds.forall(fd => fd.dataTypes.isDefined && fd.dataTypes.get.nonEmpty)
7984
if (!integrityCheck) {
8085
throw new IllegalArgumentException(s"Missing data type.A field definition must have at least one data type. Element rootPath: ${`type`}")
8186
}
8287

8388
val rootElement =
8489
("id" -> `type`) ~
85-
("path" -> `type`)
86-
90+
("path" -> `type`) ~
91+
("min" -> 0) ~
92+
("max" -> "*") ~
93+
("type" -> JArray(List("code" -> "Element")))
8794
// Children (flat list)
8895
val elements = flatFds.map { fd =>
8996
val max: String = fd.maxCardinality match {

0 commit comments

Comments
 (0)