clojure-spec 2021-12-20

hello, is there a place that explains how to properly implement the s/Spec protocol? I'm not sure about some of the methods, also, is there a place that explains the :path, :via and :in parts of the spec problems? (I see in the spec docs it talks about but doesn't mention exact those keys)

Alex Miller (Clojure team) 2021-12-21T17:37:22.277900Z

they are all "paths", but :in is data, :path is tags, and :via is specs

Alex Miller (Clojure team) 2021-12-20T20:43:47.273900Z

No, we don't really consider that public api

Alex Miller (Clojure team) 2021-12-20T20:44:29.274500Z

But if you have a question I'll try to answer it :)

mostly about the :path, :via and :in, what is expect on each and what makes it different from one another?

Alex Miller (Clojure team) 2021-12-20T21:58:44.274900Z

there is some info at https://clojure.org/guides/spec#_explain

Alex Miller (Clojure team) 2021-12-20T22:03:58.275100Z

although those are words in the error message that map to the keys: • :path - vector of keyword tag "path" segments (usually from :or, :cat, :alt, etc) • :via - vector of spec names from the root • :in - vector of data values from root to failing value (will be omitted if it's the root value)

Alex Miller (Clojure team) 2021-12-20T22:08:07.275300Z

there's an implicit tree being recursively walked here. the nodes are specs (name = :via element). Each edge has a name (:path element) and the value to validate (:in element)

Alex Miller (Clojure team) 2021-12-20T22:08:46.275500Z

in some cases, some of those are synthetic, created by the spec

the examples I'm seeing are mostly related to s/keys, where I see things like this:

(s/def ::a string?)

(s/explain-data (s/keys :req [::a]) {::a 1})

{:clojure.spec.alpha/problems ({:path [:com.piposaude.model.bulk-insert-test/a],
                                :pred clojure.core/string?,
                                :val 1,
                                :via [:com.piposaude.model.bulk-insert-test/a],
                                :in [:com.piposaude.model.bulk-insert-test/a]}),
 :clojure.spec.alpha/spec #object[clojure.spec.alpha$map_spec_impl$reify__1998
                                  0xabd19ae
                                  "clojure.spec.alpha$map_spec_impl$reify__1998@abd19ae"],
 :clojure.spec.alpha/value {:com.piposaude.model.bulk-insert-test/a 1}}

in this case all :path, :via and :in are the same value

but from what I got to you, is like: :in - path from root :path - local path :via - specs in the process is that a fair simplification of them?

and do you have examples that can clear up the distinction?