valid? receives transformed form-data, executes form-specs

master
Josha von Gizycki 6 years ago
parent 53191510ac
commit 79923a7579

@ -33,19 +33,43 @@
(spec/keys :req-un [::fields] (spec/keys :req-un [::fields]
:opt-un [::form-specs])) :opt-un [::form-specs]))
(defn valid? [form-def req] (defn form-data [form-def req]
{:pre [(spec/assert ::form form-def)]} (when-not (spec/valid? ::form form-def)
(throw (ex-info "Form def fails spec" (spec/explain-data ::form form-def))))
(reduce (fn [coll [id field]]
(let [value (get-in req [:params id])]
(assoc coll
id
(if-let [from-req (:from-req field)]
(from-req value)
value))))
{}
(:fields form-def)))
(defn form-specs-valid? [form-def data]
(reduce (fn [valid? field-spec]
(if (spec/valid? field-spec data)
true
(reduced false)))
true
(:form-specs form-def)))
(defn field-specs-valid? [form-def data]
(reduce-kv (reduce-kv
(fn [result field value] (fn [result field field-def]
(if-let [field-spec (get-in form-def [:fields field :spec])] (if-let [field-spec (:spec field-def)]
(let [from-req (get-in form-def [:fields field :from-req]) (if (spec/valid? field-spec (field data))
check-value (if from-req (from-req value) value)] true
(if (spec/valid? field-spec check-value) (reduced false))
true
(reduced false)))
result)) result))
true true
(:params req))) (:fields form-def)))
(defn valid? [form-def req]
{:pre [(spec/assert ::form form-def)]}
(let [data (form-data form-def req)]
(and (field-specs-valid? form-def data)
(form-specs-valid? form-def data))))
(def ^:dynamic *row-theme* theme/row) (def ^:dynamic *row-theme* theme/row)
(def ^:dynamic *widget-error-theme* theme/widget-error) (def ^:dynamic *widget-error-theme* theme/widget-error)
@ -74,14 +98,6 @@
(*label-theme* id def) (*label-theme* id def)
(renderer id def (if validate? req-value value))))) (renderer id def (if validate? req-value value)))))
(defn form-specs-valid? [form-def req]
(reduce (fn [valid? field-spec]
(if (spec/valid? field-spec (:params req))
true
(reduced false)))
true
(:form-specs form-def)))
(defn form-hash [form-def values] (defn form-hash [form-def values]
(str (hash [form-def values]))) (str (hash [form-def values])))
@ -110,16 +126,3 @@
[form-errors]) [form-errors])
all-widgets) all-widgets)
all-widgets))) all-widgets)))
(defn form-data [form-def req]
(when-not (spec/valid? ::form form-def)
(throw (ex-info "Form def fails spec" (spec/explain-data ::form form-def))))
(reduce (fn [coll [id field]]
(let [value (get-in req [:params id])]
(assoc coll
id
(if-let [from-req (:from-req field)]
(from-req value)
value))))
{}
(:fields form-def)))

Loading…
Cancel
Save