Last active
October 30, 2015 22:56
-
-
Save Jannis/b4fc1c36a68e5bbef170 to your computer and use it in GitHub Desktop.
Om Next mutation with validation (thin.ng/validate)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;;; Parser | |
(def card-schema | |
{:id [(v/number) (v/pos)] | |
:assignees [(v/optional (v/vector))] | |
:text [(v/string) (v/min-length 1 "The description may not be empty")]}) | |
(defn validate-card [card] | |
(assoc card :errors (second (v/validate card card-schema)))) | |
(defmethod mutate 'cards/update | |
[{:keys [state]} _ {:keys [card data]}] | |
{:value [:cards] | |
:action #(swap! state update-in card (comp validate-card merge) data)}) | |
;;;; Component | |
(defui CardEditor | |
static om/Ident | |
(ident [this props] | |
[:card/by-id (:id props)]) | |
static om/IQuery | |
(query [this] | |
[:id :text {:assignees (om/get-query Assignee)} :errors]) | |
Object | |
(render [this] | |
(let [{:keys [id text assignees errors]} (om/props this)] | |
... | |
(when (:assignees errors) | |
(dom/ul nil | |
(map-indexed #(dom/li #js {:key %1} %2) (:assignees errors)))) | |
... | |
(when (:text errors) | |
(dom/ul nil | |
(map-indexed #(dom/li #js {:key %1} %2) (:text errors)))) | |
...))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment