From e6a603223413ed49a9cb360a86b31c79f7fdac64 Mon Sep 17 00:00:00 2001 From: Islambeg Katibov Date: Fri, 20 Jan 2023 16:50:24 +0300 Subject: [PATCH] [#38] Refactor :schema-index to ::navigate interpreter Co-authored-by: @ghrpp Co-authored-by: @rublag_hs Co-authored-by: @kenichsberg Co-authored-by: @IvannikovG --- src/zen/schema.clj | 25 +++++++++++++++++++++++++ src/zen/v2_validation.clj | 22 ---------------------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/zen/schema.clj b/src/zen/schema.clj index 5a3d73d..149bf3a 100644 --- a/src/zen/schema.clj +++ b/src/zen/schema.clj @@ -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?}) @@ -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)))) diff --git a/src/zen/v2_validation.clj b/src/zen/v2_validation.clj index 3ce8f05..27a2792 100644 --- a/src/zen/v2_validation.clj +++ b/src/zen/v2_validation.clj @@ -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