Skip to content

Commit

Permalink
Merge pull request #247 from samply/246-spec
Browse files Browse the repository at this point in the history
Fix Error Reporting on Invalid Cardinality
  • Loading branch information
alexanderkiel authored Oct 27, 2020
2 parents cc5551a + a560e01 commit ec4a9f1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
28 changes: 15 additions & 13 deletions modules/fhir-structure/src/blaze/fhir/spec.clj
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -140,24 +140,26 @@
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
"Given a vector `path` and a `resource`, returns the fhir-path as string for
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 ".")))


Expand Down Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions modules/fhir-structure/src/blaze/fhir/spec/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -391,7 +391,7 @@
(vector? old)
(conj old element)
old
(conj [old] element)
[old element]
:else
element))

Expand Down
15 changes: 10 additions & 5 deletions modules/fhir-structure/test/blaze/fhir/spec/impl_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
15 changes: 11 additions & 4 deletions modules/fhir-structure/test/blaze/fhir/spec_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -467,17 +467,17 @@
[: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"
:name ""})
[: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"
Expand Down Expand Up @@ -837,3 +837,10 @@
(testing "XML"
(is (= xhtml-element
(s2/unform :fhir.xml/xhtml #fhir/xhtml"<div xmlns=\"http://www.w3.org/1999/xhtml\"><p>FHIR is cool.</p></div>"))))))


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

0 comments on commit ec4a9f1

Please sign in to comment.