Play with clojure-spec to validate server responses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[clojure.spec.alpha :as s]) | |
(s/def ::response-ok | |
(s/and | |
#(= (:status %) 200) | |
#(= (get-in % [:body :message]) "OK"))) | |
(s/def ::response-not-found #(= (:status %) 404)) | |
(s/explain-data ::response-ok { :status 200 :body { :message "OK" }}) | |
(s/explain-data ::response-ok { :status 200 :body {}}) | |
(s/explain-data ::response-ok { :status 404 }) | |
(s/explain-data ::response-not-found { :status 404 }) | |
(s/explain-data ::response-not-found { :status 200 :body { :message "OK" }}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could also use
s/or
to categorize each response withconform
, if that is helpful.Here is an alternate version that uses
multispec
. Depending on your use case, this may or may not be any better: