validation based on value

This commit is contained in:
jem 2021-06-15 21:06:12 +02:00
parent 2466dbc85f
commit 3c4be8b8ff
3 changed files with 79 additions and 52 deletions

View file

@ -12,12 +12,13 @@
<body> <body>
<div class="container jumbotron"> <div class="container jumbotron">
<form class="needs-validation" id="form"> <form class="needs-validation" id="form">
<label for="config" class="form-label">Your fqdn:</label> <label for="fqdn" class="form-label">Your fqdn:</label>
<input class="form-control" type="text" name="fqdn" id="fqdn" value="your.domain.com"> <input class="form-control" type="text" name="fqdn" id="fqdn" value="your.domain.com">
<div class="invalid-feedback"><pre id="fqdn-validation"></pre></div>
<br> <br>
<label for="config" class="form-label">(Optional) Your issuer prod/staging:</label> <label for="issuer" class="form-label">(Optional) Your issuer prod/staging:</label>
<input class="form-control" type="text" name="issuer" id="issuer" value=""> <input class="form-control" type="text" name="issuer" id="issuer" value="">
<div class="invalid-feedback"><pre id="config-validation"></pre></div> <div class="invalid-feedback"><pre id="issuer-validation"></pre></div>
<br><br> <br><br>
<label for="auth" class="form-label">Your auth.edn:</label> <label for="auth" class="form-label">Your auth.edn:</label>

View file

@ -8,6 +8,8 @@
[dda.c4k-keycloak.keycloak :as kc] [dda.c4k-keycloak.keycloak :as kc]
[dda.c4k-keycloak.postgres :as pg])) [dda.c4k-keycloak.postgres :as pg]))
(def config-defaults {:issuer :staging})
(def config? (s/keys :req-un [::kc/fqdn] (def config? (s/keys :req-un [::kc/fqdn]
:opt-un [::kc/issuer])) :opt-un [::kc/issuer]))
@ -17,6 +19,7 @@
(defn-spec generate any? (defn-spec generate any?
[my-config config? [my-config config?
my-auth auth?] my-auth auth?]
(let [resulting-config (merge config-defaults my-config)]
(cs/join "\n" (cs/join "\n"
[(yaml/to-string (pg/generate-config)) [(yaml/to-string (pg/generate-config))
"---" "---"
@ -28,10 +31,10 @@
"---" "---"
(yaml/to-string (kc/generate-secret my-auth)) (yaml/to-string (kc/generate-secret my-auth))
"---" "---"
(yaml/to-string (kc/generate-certificate my-config)) (yaml/to-string (kc/generate-certificate resulting-config))
"---" "---"
(yaml/to-string (kc/generate-ingress my-config)) (yaml/to-string (kc/generate-ingress resulting-config))
"---" "---"
(yaml/to-string (kc/generate-service)) (yaml/to-string (kc/generate-service))
"---" "---"
(yaml/to-string (kc/generate-deployment))])) (yaml/to-string (kc/generate-deployment))])))

View file

@ -1,9 +1,11 @@
(ns dda.c4k-keycloak.browser (ns dda.c4k-keycloak.browser
(:require (:require
[clojure.string :as st]
[clojure.spec.alpha :as s] [clojure.spec.alpha :as s]
[clojure.tools.reader.edn :as edn] [clojure.tools.reader.edn :as edn]
[expound.alpha :as expound] [expound.alpha :as expound]
[dda.c4k-keycloak.core :as core])) [dda.c4k-keycloak.core :as core]
[dda.c4k-keycloak.keycloak :as kc]))
(defn print-debug [sth] (defn print-debug [sth]
(print "debug " sth) (print "debug " sth)
@ -20,10 +22,13 @@
(defn issuer-from-document [] (defn issuer-from-document []
(let [issuer-str (-> (issuer) (let [issuer-str (-> (issuer)
(.-value))] (.-value))]
(if (= issuer-str "") (when-not (st/blank? issuer-str)
:staging
(keyword issuer-str)))) (keyword issuer-str))))
(defn fqdn-from-document []
(-> (fqdn)
(.-value)))
(defn auth [] (defn auth []
(-> js/document (-> js/document
(.getElementById "auth"))) (.getElementById "auth")))
@ -33,11 +38,12 @@
(.getElementById "form"))) (.getElementById "form")))
(defn config-from-document [] (defn config-from-document []
{:fqdn (-> (fqdn) (merge
(.-value)) {:fqdn (fqdn-from-document)}
:issuer (issuer-from-document)}) (when-not (st/blank? (issuer-from-document))
{:issuer (issuer-from-document)})))
(defn auth-from-document [] (defn auth-from-document []
(edn/read-string (-> (auth) (edn/read-string (-> (auth)
(.-value)))) (.-value))))
@ -48,24 +54,41 @@
(.-value) (.-value)
(set! input))) (set! input)))
(defn set-config-validation-result! (defn set-fqdn-validation-result!
[validation-result] [validation-result]
(-> js/document (-> js/document
(.getElementById "config-validation") (.getElementById "fqdn-validation")
(.-innerHTML) (.-innerHTML)
(set! validation-result)) (set! validation-result))
(-> (fqdn) (-> (fqdn)
(.setCustomValidity validation-result)) (.setCustomValidity validation-result))
validation-result)
(defn set-issuer-validation-result!
[validation-result]
(-> js/document
(.getElementById "issuer-validation")
(.-innerHTML)
(set! validation-result))
(-> (issuer) (-> (issuer)
(.setCustomValidity validation-result)) (.setCustomValidity validation-result))
validation-result) validation-result)
(defn validate-config! [] (defn validate-fqdn! []
(let [config-map (config-from-document)] (let [fqdn (fqdn-from-document)]
(if (s/valid? core/config? config-map) (if (s/valid? ::kc/fqdn fqdn)
(set-config-validation-result! "") (set-fqdn-validation-result! "")
(set-config-validation-result! (set-fqdn-validation-result!
(expound/expound-str core/config? config-map {:print-specs? false}))))) (expound/expound-str ::kc/fqdn fqdn {:print-specs? false})))))
(defn validate-issuer! []
(let [issuer (issuer-from-document)]
(print-debug (js->clj issuer))
(print-debug (st/blank? issuer))
(if (or (st/blank? issuer) (s/valid? ::kc/issuer issuer))
(set-issuer-validation-result! "")
(set-issuer-validation-result!
(expound/expound-str ::kc/issuer issuer {:print-specs? false})))))
(defn set-validated! [] (defn set-validated! []
(-> (form) (-> (form)
@ -89,27 +112,27 @@
(set-auth-validation-result! (set-auth-validation-result!
(expound/expound-str core/auth? auth-map {:print-specs? false}))))) (expound/expound-str core/auth? auth-map {:print-specs? false})))))
(defn validate-all! []
(validate-fqdn!)
(validate-issuer!)
(validate-auth!)
(set-validated!))
(defn init [] (defn init []
(-> js/document (-> js/document
(.getElementById "generate-button") (.getElementById "generate-button")
(.addEventListener "click" (.addEventListener "click"
#(do (validate-config!) #(do (validate-all!)
(validate-auth!)
(set-validated!)
(-> (core/generate (config-from-document) (auth-from-document)) (-> (core/generate (config-from-document) (auth-from-document))
(set-output!))))) (set-output!)))))
(-> (fqdn) (-> (fqdn)
(.addEventListener "blur" (.addEventListener "blur"
#(do (validate-config!) #(do (validate-all!))))
(validate-auth!)
(set-validated!))))
(-> (auth)
(.addEventListener "blur"
#(do (validate-config!)
(validate-auth!)
(set-validated!))))
(-> (issuer) (-> (issuer)
(.addEventListener "blur" (.addEventListener "blur"
#(do (validate-config!) #(do (validate-all!))))
(validate-auth!) (-> (auth)
(set-validated!))))) (.addEventListener "blur"
#(do (validate-all!))))
)