Can I validate functions with Clojure spec? - clojure

Can I use the Clojure spec system to define function signatures and verify if functions satisfy them?
Here are some examples I've tried without success
(s/valid? (s/fspec :args string? :ret string?) identity) ;; false
(def nope identity)
(s/valid? (s/fspec :args string? :ret string?) nope) ;; false
(s/conform (s/fspec :args string? :ret string?) identity) ;; invalid
(defn strmap [s] {:pre [(s/valid? string? s)] :post [(s/valid? string? %)]} s)
(s/valid? (s/fspec :args string? :ret string?) strmap) ;; false
(s/fdef strmap :args string? :ret string?)
(s/valid? strmap strmap) ;; true
(s/def ::str-pred (s/fspec :args string? :ret boolean?))
(s/valid ::str-pred (fn [s] true)) ;; false
I know about fdef, but I'd like something I can compose. For example, creating a map of related function signatures.

You can access the :args and :ret spec of a function using s/get-spec and then validate:
user=> (require '[clojure.spec.alpha :as s])
nil
user=> (defn strmap [s] s)
#'user/strmap
user=> (s/fdef strmap :args string? :ret string?)
user/strmap
user=> (s/valid? (:args (s/get-spec `strmap)) "foo")
true
user=> (s/valid? (:args (s/get-spec `strmap)) :bar)
false
I am using this in re-find.web.

Turns out spec does handle functions, but it expects the arguments to be a tuple.
(s/valid? (s/fspec :args (s/cat :arg1 string?) :ret string?) identity) ;; true!
(s/valid? (s/fspec :args (s/cat :arg1 string?) :ret string?) (fn [x] (str x "hi there"))) ;; true
(s/valid? (s/fspec :args (s/cat :arg1 string?) :ret string?) (fn [x] x)) ;; true
(s/valid? (s/fspec :args (s/cat :arg1 string?) :ret string?) (fn [x] 42)) ;; false
There does appear to be some issues with the anonymous function literal. I'm probably misunderstanding something about the macro expansion.
(s/valid? (s/fspec :args (s/cat :arg1 string?) :ret string?) #(%)) ;; false

An alternative to clojure.spec, malli is a data-driven schema library that has support for function schemas

Related

Test for a Valid Instance of java.time.LocalDate using Clojure Spec

I am trying to use Clojure Spec to define a data structure containing a java.time.LocalDate element:
(s/def :ex/first-name string?)
(s/def :ex/last-name string?)
(s/def :ex/birth-date (s/valid? inst? (java.time.LocalDate/now)))
(s/def :ex/person
(s/keys :req [:ex/first-name
:ex/last-name
:ex/birth-date]))
(def p1 #:ex{:first-name "Jenny"
:last-name "Barnes"
:birth-date (java.time.LocalDate/parse "1910-03-15")})
(println p1)
produces the following output
#:ex{:first-name Jenny, :last-name Barnes, :birth-date #object[java.time.LocalDate 0x4ed4f9db 1910-03-15]}
However, when I test to see if p1 conforms to the :ex/person spec, it fails:
(s/valid? :ex/person p1)
ClassCastException java.lang.Boolean cannot be cast to clojure.lang.IFn clojure.spec.alpha/spec-impl/reify--1987 (alpha.clj:875)
Looking closer at the Clojure examples for inst?, I see:
(inst? (java.time.Instant/now))
;;=> true
(inst? (java.time.LocalDateTime/now))
;;=> false
However, I don't see an obvious reason as to why that returns false. This seems to be the root of my issue, but I have not found a solution and would like some help.
You're probably looking for instance?- and your example fails, because in:
(s/def :ex/birth-date (s/valid? inst? (java.time.LocalDate/now)))
this part (s/valid? inst? (java.time.LocalDate/now)) should be a function (predicate), not boolean. The full code:
(s/def :ex/first-name string?)
(s/def :ex/last-name string?)
(s/def :ex/birth-date #(instance? java.time.LocalDate %))
(s/def :ex/person
(s/keys :req [:ex/first-name
:ex/last-name
:ex/birth-date]))
(def p1 #:ex{:first-name "Jenny"
:last-name "Barnes"
:birth-date (java.time.LocalDate/parse "1910-03-15")})
(s/valid? :ex/person p1)
=> true
inst? won't work here, because Inst is a protocol, used to extend java.util.Date and java.time.Instant:
(defprotocol Inst
(inst-ms* [inst]))
(extend-protocol Inst
java.util.Date
(inst-ms* [inst] (.getTime ^java.util.Date inst)))
(defn inst?
"Return true if x satisfies Inst"
{:added "1.9"}
[x]
(satisfies? Inst x))
(extend-protocol clojure.core/Inst
java.time.Instant
(inst-ms* [inst] (.toEpochMilli ^java.time.Instant inst)))
And you can use satisfies? to check whether some object satisfies given protocol:
(satisfies? Inst (java.time.LocalDate/parse "1910-03-15"))
=> false

Clojure.Spec derive or alias another spec

I'd like to use clojure spec to build up a set of type constraints that can be aliased or further constrained by other specs.
For example, I might have many fields that all need to be valid sanitized markdown.
The following example works for validation (s/valid?) but not for generation (gen/generate)
(s/def ::sanitized-markdown string?)
(s/def ::instruction-list #(s/valid? ::sanitized-markdown %)) ;; works
(gen/generate (s/gen ::instruction-list)) ;; fails
However (gen/generate (s/gen ::sanitized-markdown)) does work.
Is there a way to extend ::instruction-list from ::sanitized-markdown so that it preserves all behavior?
You can alias another spec by providing it directly to s/def:
(s/def ::instruction-list ::sanitized-markdown)
You can use s/merge when merging map specs and s/and in other cases.
(s/def ::sanitized-markdown string?)
(s/def ::instruction-list (s/and ::sanitized-markdown #(> (count %) 10)))
(s/valid? ::instruction-list "abcd")
;; false
(s/valid? ::instruction-list "abcdefghijkl")
;; true
(gen/generate (s/gen ::instruction-list))
;; "178wzJW3W3zx2G0GJ1931eEeO"
An example with maps
(s/def ::a string?)
(s/def ::b string?)
(s/def ::c string?)
(s/def ::d string?)
(s/def ::first-map (s/keys :opt [::a ::b]))
(s/def ::second-map (s/keys :opt [::c ::d]))
(s/def ::third-map (s/merge ::first-map ::second-map))
(s/valid? ::third-map {:a "1" :d "2"})
;; true
(gen/generate (s/gen ::third-map))
;; {::b "gvQ7DI1kQ9DxG7C4poeWhk553", ::d "9KIp77974TEqs9HCq", ::c "qeSZA8NcYr7UVpJDsA17K"}

Use macros with doseq to generate spec

I find myself writing a lot of specs like this:
(s/def ::name string?)
(s/def ::logUri string?)
(s/def ::subnet (s/and string? #(> (count %) 5)))
(s/def ::instanceType string?)
...
(s/def ::key (s/and string? #(> (count %) 5)))
(s/def ::instanceCount string?)
(s/def ::bidPct string?)
I.e. Lots of s/and and s/def. This seems like a waste. So I decided to write a macro that did this for me. Something like:
(defmacro and-spec [validations]
(doseq [[keyname & funcs] validations]
`(s/def ~keyname (s/and ~#funcs))))
So I would be able to do something like:
(and-spec [[::name1 [string?]]
[::name2 [string? #(> (count %) 5)]]])
And this would just do all my s/def stuff for me. Unfortunately, the above macro doesn't work, but I'm not sure why.
(s/valid? ::name1 "asdf")
Execution error at emr-cli.utils/eval6038 (form-init17784784591561795514.clj:1).
Unable to resolve spec: :emr-cli.utils/name1
Smaller versions of this work:
(defmacro small-and-spec-works [key-name validations]
`(s/def ~key-name (s/and ~#validations)))
=> #'emr-cli.utils/tsmall
(and-spec-small-works ::mykey [string?])
=> :emr-cli.utils/mykey
(s/valid? ::mykey "asdf")
=> true
But the second I introduce a let binding things start getting weird:
(defmacro small-and-spec [validation]
(let [[key-name & valids] validation]
`(s/def ~key-name (s/and ~#valids))))
=> #'emr-cli.utils/small-and-spec
(small-and-spec [::mykey2 [string?]])
=> :emr-cli.utils/mykey2
(s/valid? ::mykey2 "asdf")
Execution error (IllegalArgumentException) at emr-cli.utils/eval6012 (form-init17784784591561795514.clj:1).
Key must be integer
How can I make the doseq macro work?
What is going wrong with the small-and-spec that creates the Key must be integer error?
(defmacro and-spec [defs]
`(do
~#(map (fn [[name rest]]
`(s/def ~name (s/and ~#rest))) defs)))
doseq is for side effects. It always returns nil.

Use Clojure Spec to check type consistency on a cons?

If I run a library with borrowers and books:
(s/def ::brs (s/coll-of ::br/borrower))
(s/def ::bks (s/coll-of ::bk/book))
And I want a generic function that adds an item to either collection:
(defn add-item [x xs]
(if (some #{x} xs)
xs
(cons x xs)))
How do I write a spec that makes sure I can't add a book to borrowers and vice versa?
Because this spec:
(s/fdef add-item
:args (s/fspec :args (s/or :is-brs (s/and (s/cat :x ::br/borrower) (s/cat :xs ::brs))
:is-bks (s/and (s/cat :x ::bk/book) (s/cat :xs ::bks))))
:ret (s/or :ret-brs ::brs
:ret-bks ::bks))
is not working. :-(
Thank you for your help!
Using int/string definitions for ::brs and ::bks:
(s/def ::brs (s/coll-of int?))
(s/def ::bks (s/coll-of string?))
This should work:
(s/fdef add-item
:args (s/or
:brs (s/cat :x int? :xs ::brs)
:bks (s/cat :x string? :xs ::bks))
:ret (s/or :brs ::brs
:bks ::bks))
;; instrument function here
(add-item 1 [2])
=> (1 2)
(add-item "1" [2]) ;; throws exception

Integrate clojure spec

This question might be very basic, but I am new to clojure and could not figure out how to proceed with this.
abc.clj :
(ns abc)
(defn foo
[i]
(+ i 20))
I am writing clojure spec for this function in another file abc_test.clj.
(ns abc_test
(:require [clojure.spec :as s]
[clojure.spec.test :as stest]
[clojure.test :refer [deftest is run-tests]]
[abc :refer [foo]]
))
(s/fdef foo
:args (s/cat :i string?)
:ret string?
:fn #(> (:ret %) (-> % :args :i)))
(deftest test_foo
(is (empty? (stest/check `foo))))
(run-tests)
This test works absolutely fine (test should fail) if I put the function (foo) in abc_test namespace but if I require it (like above), then the test gives incorrect result.
Not sure what is going wrong here. Any heads up shall be helpful.
Thanks.
In the s/fdef, the symbol name needs to resolve to a fully-qualified symbol. The way you have it, foo is resolving to abc_test/foo. You want it to refer to foo in the other namespace:
(s/fdef abc/foo
:args (s/cat :i string?)
:ret string?
:fn #(> (:ret %) (-> % :args :i)))
Or another trick is to leverage syntax quote (which will resolve symbols inside it given the current namespace mappings):
(s/fdef `foo
:args (s/cat :i string?)
:ret string?
:fn #(> (:ret %) (-> % :args :i)))