From 8ee544f3cf2ef038440a9d0a57fa5f34ee696ac5 Mon Sep 17 00:00:00 2001 From: Islambeg Katibov Date: Fri, 20 Jan 2023 16:17:25 +0300 Subject: [PATCH] [#38] Refactor :values to ::navigate interpreter Co-authored-by: @kenichsberg --- src/zen/schema.clj | 22 ++++++++++++++++++++++ src/zen/v2_validation.clj | 21 --------------------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/zen/schema.clj b/src/zen/schema.clj index 29da7ce..39687a8 100644 --- a/src/zen/schema.clj +++ b/src/zen/schema.clj @@ -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?}) @@ -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))))) diff --git a/src/zen/v2_validation.clj b/src/zen/v2_validation.clj index a1f6af2..21d0aeb 100644 --- a/src/zen/v2_validation.clj +++ b/src/zen/v2_validation.clj @@ -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)]