This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-06-08
Channels
- # announcements (7)
- # babashka (44)
- # beginners (162)
- # cider (22)
- # clara (11)
- # clj-kondo (14)
- # cljsrn (8)
- # clojure (91)
- # clojure-dev (24)
- # clojure-europe (6)
- # clojure-france (4)
- # clojure-italy (11)
- # clojure-nl (4)
- # clojure-spec (11)
- # clojure-uk (14)
- # clojurescript (92)
- # community-development (1)
- # core-logic (1)
- # cryogen (1)
- # cursive (6)
- # data-science (3)
- # datahike (3)
- # datomic (32)
- # degree9 (3)
- # dirac (3)
- # emacs (9)
- # eql (1)
- # events (1)
- # find-my-lib (1)
- # fulcro (67)
- # graphql (13)
- # helix (9)
- # jobs (1)
- # jobs-discuss (92)
- # leiningen (31)
- # malli (8)
- # meander (3)
- # news-and-articles (1)
- # off-topic (46)
- # pathom (2)
- # practicalli (1)
- # re-frame (52)
- # reitit (12)
- # shadow-cljs (40)
- # spacemacs (10)
- # sql (4)
- # xtdb (8)
first class :string
coming up:
(m/validate :string "")
; => true
(m/validate [:string {:min 1}] "")
; => false
(json-schema/transform [:string {:min 1, :max 4}])
; => {:type "string", :minLength 1, :maxLenght 4}
(mg/sample [:string {:min 1, :max 4}])
; => ("RMmR" "5" "oL" "G7" "ENo" "cAh" "iOb" "jG" "l8" "kuo")
(-> [:map
[:a :string]
[:b [:string {:min 1}]]
[:c [:string {:max 4}]]
[:d [:string {:min 1, :max 4}]]]
(m/explain
{:a 123
:b ""
:c "invalid"
:d ""})
(me/humanize))
;{:a ["should be string"],
; :b ["should be at least 1 characters"],
; :c ["should be at most 4 characters"],
; :d ["should be between 1 and 4 characters"]}
Perfect. I accidentally had a string?
predicate in my spec awhile ago allowing unbounded inputs.
@U055NJ5CC any chance for a blank?
option? Or is that too specific to add to malli-core? It's the first thing I have to add to all my string specs, since you almost never want to allow user input of just blank characters. In a similar vein, do you think malli-core generator should be using char-alphanumeric
or simply string
? I understand the first gives you nicer looking example data, but the second is more useful in testing edge cases.
On second thought, the latter question is probably not worth the hassle (and one can always write a custom gen where it's needed). But still curious about a possibility of blank?
or present?
option.
@U05476190 chaned the generator to char
& string
.
Example string-trimmer here too:
(require '[malli.transform :as mt])
(require '[malli.core :as m])
(require '[clojure.string :as str])
(defn string-trimmer []
(mt/transformer
{:decoders
{:string
{:compile (fn [schema _]
(let [{:string/keys [trim]} (m/properties schema)]
(when trim #(cond-> % (string? %) str/trim))))}}}))
(m/decode [:string {:min 1}] " " string-trimmer)
; => " "
(m/decode [:string {:string/trim true, :min 1}] " " string-trimmer)
; => ""
(m/decoder :string string-trimmer)
; => #object[clojure.core$identity] ... no-op! :)