Skip to content

Commit

Permalink
[#38] Refactor :schema-index to ::navigate interpreter
Browse files Browse the repository at this point in the history
Co-authored-by: @ghrpp <profunctorlense@gmail.com>
Co-authored-by: @rublag_hs <rublag-ns@yandex.ru>
Co-authored-by: @kenichsberg <kn1017356@gmail.com>
Co-authored-by: @IvannikovG <ivannikogeorge1337@gmail.com>
  • Loading branch information
5 people committed Jan 20, 2023
1 parent a637007 commit e6a6032
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
25 changes: 25 additions & 0 deletions src/zen/schema.clj
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@
(defmethod compile-key :minItems [_ _ _] {:when #(or (sequential? %) (set? %))})
(defmethod compile-key :maxItems [_ _ _] {:when #(or (sequential? %) (set? %))})

(defmethod compile-key :schema-index [_ _ _] {:when sequential?})

(defmethod compile-key :subset-of [_ _ _] {:when set?})
(defmethod compile-key :superset-of [_ _ _] {:when set?})

Expand Down Expand Up @@ -303,3 +305,26 @@
(v data opts)
(validation.utils/merge-vtx vtx)))))
vtx))))


#_"NOTE: Errors mechanism used here comes from ::validate interpreter. Maybe we should untie it from here."
(register-compile-key-interpreter!
[:schema-index ::navigate]
(fn [_ ztx {si :index si-ns :ns}]
(fn [vtx data opts]
(if-let [sch-nm (or (get data si) (nth data si))]
(let [sch-symbol (if si-ns (symbol si-ns (name sch-nm)) sch-nm)
sch (utils/get-symbol ztx sch-symbol)]
(cond
(nil? sch)
(validation.utils/add-err vtx
:schema-index
{:message (format "Could not find schema %s" sch-symbol)
:type "schema"})

:else
(let [v (get-cached ztx sch false)]
(-> (validation.utils/node-vtx vtx [:schema-index sch-symbol])
(v data opts)
(validation.utils/merge-vtx vtx)))))
vtx))))
22 changes: 0 additions & 22 deletions src/zen/v2_validation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -450,28 +450,6 @@ Probably safe to remove if no one relies on them"
vtx
ks)))))

(defmethod compile-key :schema-index
[_ ztx {si :index si-ns :ns}]
{:when sequential?
:rule
(fn [vtx data opts]
(if-let [sch-nm (or (get data si) (nth data si))]
(let [sch-symbol (if si-ns (symbol si-ns (name sch-nm)) sch-nm)
sch (utils/get-symbol ztx sch-symbol)]
(cond
(nil? sch)
(add-err vtx
:schema-index
{:message (format "Could not find schema %s" sch-symbol)
:type "schema"})

:else
(let [v (get-cached ztx sch false)]
(-> (node-vtx vtx [:schema-index sch-symbol])
(v data opts)
(merge-vtx vtx)))))
vtx))})

(defmethod compile-key :nth
[_ ztx cfg]
(let [schemas (doall
Expand Down

0 comments on commit e6a6032

Please sign in to comment.