Skip to content

Commit

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

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

(defmethod compile-key :subset-of [_ _ _] {:when set?})
Expand Down Expand Up @@ -328,3 +329,21 @@
(v data opts)
(validation.utils/merge-vtx vtx)))))
vtx))))


(register-compile-key-interpreter!
[:nth ::navigate]
(fn [_ ztx cfg]
(let [schemas (doall
(map (fn [[index v]] [index (get-cached ztx v false)])
cfg))]
(fn [vtx data opts]
(reduce (fn [vtx* [index v]]
(if-let [nth-el (and (< index (count data))
(nth data index))]
(-> (validation.utils/node-vtx vtx* [:nth index] [index])
(v nth-el opts)
(validation.utils/merge-vtx vtx*))
vtx*))
vtx
schemas)))))
18 changes: 0 additions & 18 deletions src/zen/v2_validation.clj
Original file line number Diff line number Diff line change
Expand Up @@ -450,24 +450,6 @@ Probably safe to remove if no one relies on them"
vtx
ks)))))

(defmethod compile-key :nth
[_ ztx cfg]
(let [schemas (doall
(map (fn [[index v]] [index (get-cached ztx v false)])
cfg))]
{:when sequential?
:rule
(fn [vtx data opts]
(reduce (fn [vtx* [index v]]
(if-let [nth-el (and (< index (count data))
(nth data index))]
(-> (node-vtx vtx* [:nth index] [index])
(v nth-el opts)
(merge-vtx vtx*))
vtx*))
vtx
schemas))}))

(defmethod compile-key :keyname-schemas
[_ ztx {:keys [tags]}]
{:when map?
Expand Down

0 comments on commit 5aef6ea

Please sign in to comment.