Skip to content

Commit

Permalink
Fix validation error
Browse files Browse the repository at this point in the history
validation/def worked inside the project, but when consumed by other
projects it blew up. Switching protocols to cljc and renaming def to
defvalidation helped solve the problem.
  • Loading branch information
emil0r committed Jun 2, 2020
1 parent 200f36e commit c844a56
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 12 deletions.
8 changes: 4 additions & 4 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ Form validation and i18n are supported via protocols and have default implementa
(:require [clojure.spec.alpha :as spec]
[clojure.string :as str]
[ez-wire.form :as form]
[ez-wire.form.validation :as validation]
[myns.fomantic.elements :as e] ; <-- elements from fomantic UI. e/input is a standard input field from fomantic UI
[reagent.core :as r])
(:require-macros [ez-wire.form.macros :refer [defform]]))
(:require-macros [ez-wire.form.macros :refer [defform]]
[ez-wire.form.validation :refer [defvalidation]]))
;; we adapt our input to ez-wire.form's internal data model
;; NOTE: this can be used to create an input that automatically converts to another data format (say an integer, instead of a string)
Expand All @@ -37,12 +37,12 @@ Form validation and i18n are supported via protocols and have default implementa
;; validation can be anything and is supported via protocols.
;; in this case the default implementation uses spec (version 1) and a error message (fn or string/keyword)
;; i18n is also supported via protocols and supports nil, strings and keywords by default. extend to any i18n library you wish
(validation/def ::my-validation
(defvalidation ::my-validation
(spec/or :int int? :blank str/blank?)
(fn [{:keys [value]}]
[:div "current value is " [:strong "'" value "'"] " and it needs to be an integer"]))
(validation/def ::stupid
(defvalidation ::stupid
(spec/and string?
#(> (count %) 2))
"Need to be a string and more than two characters")
Expand Down
10 changes: 5 additions & 5 deletions dev/cljs/ez_wire/test_page.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
[clojure.string :as str]
[reagent.core :as r]
[ez-wire.element :as e]
[ez-wire.form :as form :refer [defform]]
[ez-wire.form.validation :as validation]
[ez-wire.form :as form]
[ez-wire.util :as util]
[re-frame.core :as rf])
(:require-macros [ez-wire.form.macros :refer [defform]]))
(:require-macros [ez-wire.form.macros :refer [defform]]
[ez-wire.form.validation :refer [defvalidation]]))

(enable-console-print!)

Expand All @@ -18,11 +18,11 @@
:on-change #(reset! model (-> % .-target .-value))}
(select-keys data [:id :placeholder]))])))

(validation/def ::my-validation
(defvalidation ::my-validation
(spec/or :int int? :blank str/blank?)
(fn [{:keys [value]}]
[:div "current value is " [:strong "'" value "'"] " and it needs to be an integer"]))
(validation/def ::stupid
(defvalidation ::stupid
(spec/and string?
#(> (count %) 2))
"Need to be a string and more than two characters")
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject ez-wire "0.1.0"
(defproject ez-wire "0.1.1"

:description "Wiring galore"

Expand Down
2 changes: 1 addition & 1 deletion src/ez_wire/form/common.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
:$errors (render-error-element field form-map)
:$text (render-text field form-map)
:$help (render-help field form-map)})


;; otherwise we're good to go with using the default row
:else
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/ez_wire/form/validation.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

(defonce errors (atom {}))

(defmacro def [spec-k spec-v t-fn-or-keyword]
(defmacro defvalidation [spec-k spec-v t-fn-or-keyword]
`(do (spec/def ~spec-k ~spec-v)
(swap! errors assoc ~spec-k ~t-fn-or-keyword)))

Expand Down

0 comments on commit c844a56

Please sign in to comment.