Just a bit more convenience with clojure.spec
only-keys
:
(require '[clojure.spec.alpha :as s]
'[more.spec.alpha :as ms])
(s/def :acct/first-name string?)
(s/def :acct/last-name string?)
(s/def :acct/middle-name string?)
(s/def :acct/person
(ms/only-keys :req [:acct/first-name :acct/last-name]
:opt [:acct/middle-name]))
(s/valid? :acct/person
{:acct/first-name "Bugs"
:acct/last-name "Bunny"
:acct/email "test@example.com"}) ; :acct/email is not defined.
;;=> false
re-find
:
(s/def :greeting/hello (ms/re-find #"Hello"))
(s/valid? :greeting/hello "Hello, world!")
;;=> true
(s/valid? :greeting/hello "Goodbye, world!")
;;=> false
re-matches
:
(s/def :greeting/hello (ms/re-matches #"Hello"))
(s/valid? :greeting/hello "Hello")
;;=> true
(s/valid? :greeting/hello "Hello, world!")
;;=> false
valid?
in clojure.test:
(require '[clojure.test :refer [deftest is]]
'[more.spec.alpha.test])
(s/def :int/pos (s/and integer? pos?))
(deftest int-pos-test
(is (valid? :int/pos -1)))
(int-pos-test)
;; FAIL in (int-pos-test)
;; expected: (valid? :int/pos -1)
;; actual: "-1 - failed: pos? spec: :int/pos\n" ; spec failure is explained.
;;=> nil
Copyright 2025 Toshiki Takeuchi
Distributed under the MIT License.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close