From b72f1ba140f3d7e431afb37d4e33c7fc4a8f6c5a Mon Sep 17 00:00:00 2001 From: Kimo Knowles Date: Fri, 24 Nov 2023 10:11:43 +0100 Subject: [PATCH] Catch regular atoms in validation Fixes #291 --- CHANGELOG.md | 1 + src/re_com/debug.cljs | 3 +++ src/re_com/util.cljs | 2 +- src/re_com/validate.cljs | 16 ++++++++++++---- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b29d578..3e1c0040 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ - Selected tabs & radio buttons no longer trigger their on-change handler (#333) - Use alert-circle for input-text error state, not spinner (#325) - Stop using deprecated keycodes. This fixes some edge cases with keyboard layouts. (#197) +- Props which are non-reactive atoms are now caught in validation. (#291) ## 2.13.2 (2021-02-24) diff --git a/src/re_com/debug.cljs b/src/re_com/debug.cljs index a422557f..4276bd97 100644 --- a/src/re_com/debug.cljs +++ b/src/re_com/debug.cljs @@ -134,6 +134,9 @@ :required (js/console.log (str "• %cMissing required parameter: %c" arg-name) error-style code-style) + :ref (js/console.log + (str "• %cParameter %c" arg-name "%c expected a reactive atom but got a %c" actual) + error-style code-style error-style code-style) :validate-fn (js/console.log (str "• %cParameter %c" arg-name "%c expected %c" (:type expected) "%c but got %c" actual) error-style code-style error-style code-style error-style code-style) diff --git a/src/re_com/util.cljs b/src/re_com/util.cljs index 1467deda..b410cfe6 100644 --- a/src/re_com/util.cljs +++ b/src/re_com/util.cljs @@ -25,7 +25,7 @@ (defn deref-or-value "Takes a value or an atom If it's a value, returns it - If it's a Reagent object that supports IDeref, returns the value inside it by derefing + If it's an object that supports IDeref, returns the value inside it by derefing " [val-or-atom] (if (satisfies? IDeref val-or-atom) diff --git a/src/re_com/validate.cljs b/src/re_com/validate.cljs index f77f436d..3b8357b0 100644 --- a/src/re_com/validate.cljs +++ b/src/re_com/validate.cljs @@ -80,11 +80,14 @@ [args-with-validators passed-args problems] (let [validate-arg (fn [[_ v-arg-def]] (let [arg-name (:name v-arg-def) - arg-val (deref-or-value-peek (arg-name passed-args)) ;; Automatically extract value if it's in an atom + arg-val (try (deref-or-value-peek (arg-name passed-args)) + (catch js/Error _ {::problem :ref})) + ref-problem? (some? (get arg-val ::problem)) validate-fn (:validate-fn v-arg-def) - validate-result (if (= 1 (.-length ^js/Function validate-fn)) - (validate-fn arg-val) ;; Standard call, just pass the arg - (validate-fn arg-val (satisfies? IDeref (arg-name passed-args)))) ;; Extended call, also wants to know if arg-val is an atom + validate-result (when-not ref-problem? + (if (= 1 (.-length ^js/Function validate-fn)) + (validate-fn arg-val) ;; Standard call, just pass the arg + (validate-fn arg-val (satisfies? IDeref (arg-name passed-args))))) ;; Extended call, also wants to know if arg-val is an atom required? (:required v-arg-def) problem-base {:arg-name arg-name} warning? (= (:status validate-result) :warning)] @@ -94,6 +97,11 @@ (not required?))) nil + ref-problem? + (merge problem-base + {:problem :ref + :actual (left-string (pr-str (type (arg-name passed-args))) 60)}) + (false? validate-result) (merge problem-base {:problem :validate-fn