将Clojure模式输入转换为小写。
我可以将clojure模式输入验证为字符串,如下所示将Clojure模式输入转换为小写。,clojure,Clojure,我可以将clojure模式输入验证为字符串,如下所示 name :- s/Str, place :- s/Str, 如何为电子邮件编写defschema以便像这样使用 email : - s/Email 将其转换/强制为小写 我正好有一个 (ns myapp.utils.schema (:import [org.apache.commons.validator.routines EmailValidator]) (:require [clojure.string :as str]
name :- s/Str,
place :- s/Str,
如何为电子邮件编写defschema以便像这样使用
email : - s/Email
将其转换/强制为小写 我正好有一个
(ns myapp.utils.schema
(:import [org.apache.commons.validator.routines EmailValidator])
(:require [clojure.string :as str]
[schema.core :as s]
[schema.coerce :as sco]))
(def valid-email? "Checks that the value is a valid email string."
(let [ev (EmailValidator/getInstance)]
(fn [s]
(and
(string? s)
(.isValid ev, ^String s)
))))
(def Email "Email schema type, stricter than schema.core/Str."
(s/pred valid-email?))
(defn sanitize-email [s]
(if (string? s)
(-> s str/lower-case str/trim)
s))
(defn email-matcher [s]
(when (= Email s) sanitize-email))
(def matcher
"The Plumatic matcher used for Plumatic schema coercion in myapp."
(sco/first-matcher [email-matcher,,,]))
(defn coercer
[schema]
(sco/coercer schema matcher))
;; ...
(ns myapp.customer
(:require [schema.core :as s]
[myapp.utils.schema :as sc]))
(def Customer
{:customer/email sc/Email
:customer/name s/Str})
(def coerce-customer (sc/coercer Customer))
(coerce-customer {:customer/email "vAlENTin@gmaIl.com "
:customer/name "Valentin"})
=> {:customer/email "valentin@gmail.com"
:customer/name "Valentin"}