Skip to content

Commit

Permalink
[#38] Refactor :values to ::navigate interpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
katibov and kenichsberg committed Jan 20, 2023
1 parent c480aa6 commit 8ee544f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
22 changes: 22 additions & 0 deletions src/zen/schema.clj
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
(defmethod compile-key :keys [_ _ _] {:when map?})
(defmethod compile-key :validation-type [_ _ _] {:when map?})
(defmethod compile-key :require [_ _ _] {:when map?})
(defmethod compile-key :values [_ _ _] {:when map?})

(defmethod compile-key :scale [_ _ _] {:when number?})
(defmethod compile-key :precision [_ _ _] {:when number?})
Expand Down Expand Up @@ -188,3 +189,24 @@
(key-rule v opts)
(validation.utils/merge-vtx vtx*)))
(recur (rest data) vtx*)))))))))


(register-compile-key-interpreter!
[:values ::navigate]
(fn [_ ztx sch]
(let [v (get-cached ztx sch false)]
(fn [vtx data opts]
(reduce-kv (fn [vtx* key value]
(let [node-visited?
(when-let [pth (get (:visited vtx*) (validation.utils/cur-path vtx* [key]))]
(:keys (get (:visited-by vtx*) pth)))

strict?
(= (:valmode opts) :strict)]
(if (and (not strict?) node-visited?)
vtx*
(-> (validation.utils/node-vtx&log vtx* [:values] [key])
(v value opts)
(validation.utils/merge-vtx vtx*)))))
vtx
data)))))
21 changes: 0 additions & 21 deletions src/zen/v2_validation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -405,27 +405,6 @@ Probably safe to remove if no one relies on them"
(map #(conj (:path vtx) %))
unknown-keys))))))

(defmethod compile-key :values
[_ ztx sch]
(let [v (get-cached ztx sch false)]
{:when map?
:rule
(fn [vtx data opts]
(reduce (fn [vtx* [key value]]
(let [node-visited?
(when-let [pth (get (:visited vtx*) (cur-path vtx* [key]))]
(:keys (get (:visited-by vtx*) pth)))

strict?
(= (:valmode opts) :strict)]
(if (and (not strict?) node-visited?)
vtx*
(-> (node-vtx&log vtx* [:values] [key])
(v value opts)
(merge-vtx vtx*)))))
vtx
data))}))

(defmethod compile-key :every
[_ ztx sch]
(let [v (get-cached ztx sch false)]
Expand Down

0 comments on commit 8ee544f

Please sign in to comment.