Skip to content

Instantly share code, notes, and snippets.

@Jannis
Last active October 30, 2015 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jannis/b4fc1c36a68e5bbef170 to your computer and use it in GitHub Desktop.
Save Jannis/b4fc1c36a68e5bbef170 to your computer and use it in GitHub Desktop.
Om Next mutation with validation (thin.ng/validate)
;;;; 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