Skip to content

Commit

Permalink
Catch regular atoms in validation
Browse files Browse the repository at this point in the history
Fixes #291
  • Loading branch information
kimo-k committed Nov 24, 2023
1 parent dab03ef commit b72f1ba
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions src/re_com/debug.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/re_com/util.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 12 additions & 4 deletions src/re_com/validate.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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
Expand Down

0 comments on commit b72f1ba

Please sign in to comment.