diff --git a/modules/fhir-structure/src/blaze/fhir/spec.clj b/modules/fhir-structure/src/blaze/fhir/spec.clj index 9f63eb662..ae0f9ea99 100644 --- a/modules/fhir-structure/src/blaze/fhir/spec.clj +++ b/modules/fhir-structure/src/blaze/fhir/spec.clj @@ -1,7 +1,7 @@ (ns blaze.fhir.spec (:require [blaze.fhir.hash.spec] - [blaze.fhir.spec.impl] + [blaze.fhir.spec.impl :as impl] [blaze.fhir.spec.type :as type] [clojure.alpha.spec :as s2] [clojure.spec.alpha :as s] @@ -140,10 +140,10 @@ from data replaced by their indices." {:arglists '([path data])} [[elem & elems] data] - (if (vector? data) - (cons (first (keep-indexed #(when (= %2 elem) %1) data)) - (when elems (fhir-path-data elems elem))) - (cons elem (if elems (fhir-path-data elems (get data elem)) [])))) + (if (keyword? elem) + (cons elem (if elems (fhir-path-data elems (get data elem)) [])) + (cons (first (keep-indexed #(when (= %2 elem) %1) (impl/ensure-coll data))) + (when elems (fhir-path-data elems elem))))) (defn fhir-path @@ -151,13 +151,15 @@ the given path." [path resource] (->> (fhir-path-data path resource) - (reduce (fn [list elem] (if (keyword? elem) - (conj list (name elem)) - (update list - (dec (count list)) - str - (format "[%d]" elem)))) - []) + (reduce + (fn [list elem] + (if (keyword? elem) + (conj list (name elem)) + (update list + (dec (count list)) + str + (format "[%d]" elem)))) + []) (str/join "."))) @@ -275,7 +277,7 @@ (defn primitive-val? - "" + "Returns true if `x` is a primitive FHIR value." [x] (when-let [fhir-type (fhir-type x)] (and (= "fhir" (namespace fhir-type)) diff --git a/modules/fhir-structure/src/blaze/fhir/spec/impl.clj b/modules/fhir-structure/src/blaze/fhir/spec/impl.clj index 8c252062d..b6086ab95 100644 --- a/modules/fhir-structure/src/blaze/fhir/spec/impl.clj +++ b/modules/fhir-structure/src/blaze/fhir/spec/impl.clj @@ -288,7 +288,7 @@ [(keyword (name key)) (if (= "1" max) key - (if (= :xml modifier) + (if (#{:json :xml} modifier) `(s/and (s/conformer ensure-coll identity) (s/coll-of ~key)) @@ -391,7 +391,7 @@ (vector? old) (conj old element) old - (conj [old] element) + [old element] :else element)) diff --git a/modules/fhir-structure/test/blaze/fhir/spec/impl_test.clj b/modules/fhir-structure/test/blaze/fhir/spec/impl_test.clj index 0837b93ca..8968fe16e 100644 --- a/modules/fhir-structure/test/blaze/fhir/spec/impl_test.clj +++ b/modules/fhir-structure/test/blaze/fhir/spec/impl_test.clj @@ -353,8 +353,10 @@ :type :fhir.json.Bundle/type :timestamp :fhir.json.Bundle/timestamp :total :fhir.json.Bundle/total - :link (s2/coll-of :fhir.json.Bundle/link) - :entry (s2/coll-of :fhir.json.Bundle/entry) + :link (s2/and (s2/conformer impl/ensure-coll identity) + (s2/coll-of :fhir.json.Bundle/link)) + :entry (s2/and (s2/conformer impl/ensure-coll identity) + (s2/coll-of :fhir.json.Bundle/entry)) :signature :fhir.json.Bundle/signature}))) (testing "JSON representation of Bundle.id" @@ -376,9 +378,12 @@ := `(s2/and (s2/schema {:id :fhir.json.Bundle.entry/id - :extension (s2/coll-of :fhir.json.Bundle.entry/extension) - :modifierExtension (s2/coll-of :fhir.json.Bundle.entry/modifierExtension) - :link (s2/coll-of :fhir.json.Bundle.entry/link) + :extension (s2/and (s2/conformer impl/ensure-coll identity) + (s2/coll-of :fhir.json.Bundle.entry/extension)) + :modifierExtension (s2/and (s2/conformer impl/ensure-coll identity) + (s2/coll-of :fhir.json.Bundle.entry/modifierExtension)) + :link (s2/and (s2/conformer impl/ensure-coll identity) + (s2/coll-of :fhir.json.Bundle.entry/link)) :fullUrl :fhir.json.Bundle.entry/fullUrl :resource :fhir.json.Bundle.entry/resource :search :fhir.json.Bundle.entry/search diff --git a/modules/fhir-structure/test/blaze/fhir/spec_test.clj b/modules/fhir-structure/test/blaze/fhir/spec_test.clj index e0377f46d..ad2126250 100644 --- a/modules/fhir-structure/test/blaze/fhir/spec_test.clj +++ b/modules/fhir-structure/test/blaze/fhir/spec_test.clj @@ -467,8 +467,8 @@ [:fhir/issues 0 :fhir.issues/severity] := "error" [:fhir/issues 0 :fhir.issues/code] := "invariant" [:fhir/issues 0 :fhir.issues/diagnostics] := - "Error on value ``. Expected type is `JSON array`." - [:fhir/issues 0 :fhir.issues/expression] := "contact")) + "Error on value ``. Expected type is `Patient.contact`." + [:fhir/issues 0 :fhir.issues/expression] := "contact[0]")) (testing "invalid non-primitive element" (given (fhir-spec/explain-data-json {:resourceType "Patient" @@ -476,8 +476,8 @@ [:fhir/issues 0 :fhir.issues/severity] := "error" [:fhir/issues 0 :fhir.issues/code] := "invariant" [:fhir/issues 0 :fhir.issues/diagnostics] := - "Error on value ``. Expected type is `JSON array`." - [:fhir/issues 0 :fhir.issues/expression] := "name")) + "Error on value ``. Expected type is `HumanName`." + [:fhir/issues 0 :fhir.issues/expression] := "name[0]")) (testing "Include namespace part if more than fhir" (given (fhir-spec/explain-data-json {:resourceType "Patient" @@ -837,3 +837,10 @@ (testing "XML" (is (= xhtml-element (s2/unform :fhir.xml/xhtml #fhir/xhtml"

FHIR is cool.

")))))) + + +(deftest primitive-val? + (are [x] (fhir-spec/primitive-val? x) + "foo" + 1 + #fhir/code"bar"))