Minor refactoring.

This commit is contained in:
Simon Brooke 2022-12-21 22:35:31 +00:00
parent 1b2adb5296
commit 21220970a8

View file

@ -117,46 +117,44 @@
:fault fault :fault fault
:narrative (messages fault))) :narrative (messages fault)))
(defmacro nil-if-empty
"if `x` is an empty collection, return `nil`; else return `x`."
[x]
`(if (coll? ~x)
(if (empty? ~x) nil ~x)
~x))
(defn object-faults (defn object-faults
"Return a list of faults found in object `x`, or `nil` if none are." "Return a list of faults found in object `x`, or `nil` if none are."
[x] [x]
(let [faults (remove (nil-if-empty
empty? (remove empty?
(list (list
(when-not (map? x) (when-not (map? x)
(make-fault-object (make-fault-object :critical :not-an-object))
:critical (when-not
:not-an-object)) (has-context? x)
(when-not (make-fault-object :should :no-context))
(has-context? x) (when-not (:type x)
(make-fault-object (make-fault-object :minor :no-type))
:should (when-not (and (map? x) (contains? x :id))
:no-context)) (make-fault-object :minor :no-id-transient))))))
(when-not (:type x)
(make-fault-object
:minor
:no-type))
(when-not (and (map? x) (contains? x :id))
(make-fault-object
:minor
:no-id-transient))))]
(if (empty? faults) nil faults)))
(defn persistent-object-faults (defn persistent-object-faults
"Return a list of faults found in persistent object `x`, or `nil` if none are." "Return a list of faults found in persistent object `x`, or `nil` if none are."
[x] [x]
(let [faults (concat (nil-if-empty
(object-faults x) (remove empty?
(remove empty? (concat
(list (object-faults x)
(if (contains? x :id) (list
(try (let [id (URI. (:id x))] (if (contains? x :id)
(when-not (= (.getScheme id) "https") (try (let [id (URI. (:id x))]
(make-fault-object :should :id-not-https))) (when-not (= (.getScheme id) "https")
(catch URISyntaxException _ (make-fault-object :should :id-not-https)))
(make-fault-object :must :id-not-uri)) (catch URISyntaxException _
(catch NullPointerException _ (make-fault-object :must :id-not-uri))
(make-fault-object :must :null-id-persistent))) (catch NullPointerException _
(make-fault-object :must :no-id-persistent)))))] (make-fault-object :must :null-id-persistent)))
(if (empty? faults) nil faults))) (make-fault-object :must :no-id-persistent)))))))