a lot of streamlining for validation

This commit is contained in:
2023-10-24 11:12:31 -07:00
parent 48347bb8c5
commit 91f7e79aed
7 changed files with 161 additions and 168 deletions

View File

@@ -1,15 +1,15 @@
(ns auto-ap.ssr.utils
(:require
[auto-ap.datomic :refer [all-schema]]
[auto-ap.datomic :refer [all-schema conn]]
[auto-ap.logging :as alog]
[clojure.string :as str]
[config.core :refer [env]]
[datomic.api :as dc]
[hiccup2.core :as hiccup]
[malli.core :as mc]
[malli.error :as me]
[malli.transform :as mt2]
[slingshot.slingshot :refer [throw+ try+]]
[manifold.time :as mt]))
[slingshot.slingshot :refer [throw+ try+]]))
(defn html-response [hiccup & {:keys [status headers oob] :or {status 200 headers {} oob []}}]
{:status status
@@ -99,7 +99,7 @@
(def entity-id (mc/schema [nat-int? {:error/message "required"} ]))
(def temp-id (mc/schema :string))
(def temp-id (mc/schema [:string {:min 1}]))
(def money (mc/schema [:double]))
(def percentage (mc/schema [:double {:decode/arbitrary (fn [x] (some-> x (* 0.01)))
:max 1.0
@@ -292,18 +292,18 @@
(:errors (:explain (:error e))))]
(alog/warn ::form-4xx :errors errors)
(form-handler (assoc request
:last-form (:decoded e)
:form-params (:decoded e)
:field-validation-errors errors
:form-errors humanized)))
#_(html-response [:span.error-content.text-red-500 (:message &throw-context)]
:status 400))
(catch [:type :field-validation] e
(form-handler (assoc request
:last-form (:form e)
:form-params (:form e)
:form-errors (:form-errors e))))
(catch [:type :form-validation] e
(form-handler (assoc request
:last-form (:form e)
:form-params (:form e)
:form-validation-errors (:form-validation-errors e)
:form-errors {:errors (:form-validation-errors e)}))))))
@@ -326,3 +326,15 @@
(map (fn [k]
(str "[" (k->n k) "]"))
rest)))))
(defn wrap-entity [handler path read]
(fn wrap-entity-request [request]
(let [entity (some->>
(get-in request path)
(#(if (string? %) (Long/parseLong %) %))
(dc/pull (dc/db conn) read ))]
(handler (if entity
(assoc request
:entity entity)
request)))))