This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-01-01
Channels
- # babashka (2)
- # beginners (45)
- # calva (2)
- # cider (20)
- # clj-kondo (9)
- # cljsrn (1)
- # clojure (25)
- # clojure-europe (7)
- # clojure-nl (24)
- # clojure-spec (4)
- # clojure-uk (3)
- # clojurescript (16)
- # datahike (6)
- # datalevin (6)
- # emacs (35)
- # fulcro (3)
- # holy-lambda (1)
- # lsp (55)
- # nrepl (2)
- # off-topic (17)
- # spacemacs (11)
- # specter (1)
- # xtdb (3)
Hi everyone, an extremely weird thing is happening using spec.alpha
... Please check this simple snippet. Why does the spec/explain-data
at the end try to validate the sample
record against the dfntn-spec
when I'm not even including it in the spec/keys
passed to it ??! Am I doing something wrong or is this a bug ? :
• Clojure Spec Version : [org.clojure/spec.alpha "0.2.194"]
(ns hermes.lib.system.alaki
(:require [clojure.spec.alpha :as spec]
[clojure.string :as clj-str]
[com.stuartsierra.component :as component]
[hermes.lib.system.utils :as utils]
[taoensso.timbre :as timbre]))
;;------------------------------------------------------------------;;
;;------------------------------------------------------------------;;
(defn multimethod-dispatch-fn
[x]
(let [system? (-> x :component/system? true?)]
(case system?
true (-> :systemized-component)
false (-> x :component/type))))
(defmulti dfntn-spec multimethod-dispatch-fn)
(defmulti create multimethod-dispatch-fn)
(spec/def :component/name simple-keyword?)
(spec/def :component/config
(spec/multi-spec dfntn-spec
multimethod-dispatch-fn))
(spec/def :component/deps
(spec/map-of simple-keyword? simple-keyword?))
(comment
(def sample {:component/type [:Schema :v-0-0-1]
:component/system? false
:component/name :schema-1
:component/config {:serde :nippy
:topics [{:topic-name "~~"
:partitions 1}]}
:component/deps {}})
(spec/explain-data
(spec/keys :req [:component/name
:component/deps
])
sample)
)
;;------------------------------------------------------------------;;
;;------------------------------------------------------------------;;
Hi everyone, here is another instance that I just don't get what's going on :
(ns hermes.lib.system.alaki
(:require [clojure.spec.alpha :as spec]
[clojure.string :as clj-str]
[com.stuartsierra.component :as component]
[hermes.lib.system.utils :as utils]
[taoensso.timbre :as timbre]))
;;------------------------------------------------------------------;;
(defn multimethod-dispatch-fn
[x]
(let [system? (-> x :component/system? true?)]
(case system?
true (-> :systemized-component)
false (-> x :component/type))))
(defmulti config-spec multimethod-dispatch-fn)
(defmulti create multimethod-dispatch-fn)
;;------------------------------------------------------------------;;
;;------------------------------------------------------------------;;
(spec/def ::component-config
(spec/multi-spec config-spec
multimethod-dispatch-fn))
;;------------------------------------------------------------------;;
;;------------------------------------------------------------------;;
(def sample
{:component/type [:Schema :v-0-0-1]
:serde :nippy
:topics [{:topic-name "~~"
:partitions 1}]})
(defmethod config-spec [:Schema :v-0-0-1] [& _] any?)
(spec/valid? (config-spec sample) sample)
(spec/explain-data (config-spec sample) sample)
(spec/valid? ::component-config sample)
(spec/explain-data ::component-config sample)
;;------------------------------------------------------------------;;
;;------------------------------------------------------------------;;
While the last spec/valid?
returns true the subsequent spec/explain-data
returns :
#:clojure.spec.alpha{:problems [{:path [[:Schema :v-0-0-1]], :pred hermes.lib.system.alaki/config-spec, :val {:component/type [:Schema :v-0-0-1], :serde :nippy, :topics [{:topic-name "~~", :partitions 1}]}, :via [:hermes.lib.system.alaki/component-config], :in []}], :spec :hermes.lib.system.alaki/component-config, :value {:component/type [:Schema :v-0-0-1], :serde :nippy, :topics [{:topic-name "~~", :partitions 1}]}}
• Spec Version : [org.clojure/spec.alpha "0.3.214"]
I think that the behavior you noticed in your first example is normal for s/keys
, see the docstring:
> In addition, the values of all namespace-qualified keys will be validated (and possibly destructured) by any registered specs.
Year you're right. Thanks a lot @U0178V2SLAY.
Hi everyone, here is another instance that I just don't get what's going on :
(ns hermes.lib.system.alaki
(:require [clojure.spec.alpha :as spec]
[clojure.string :as clj-str]
[com.stuartsierra.component :as component]
[hermes.lib.system.utils :as utils]
[taoensso.timbre :as timbre]))
;;------------------------------------------------------------------;;
(defn multimethod-dispatch-fn
[x]
(let [system? (-> x :component/system? true?)]
(case system?
true (-> :systemized-component)
false (-> x :component/type))))
(defmulti config-spec multimethod-dispatch-fn)
(defmulti create multimethod-dispatch-fn)
;;------------------------------------------------------------------;;
;;------------------------------------------------------------------;;
(spec/def ::component-config
(spec/multi-spec config-spec
multimethod-dispatch-fn))
;;------------------------------------------------------------------;;
;;------------------------------------------------------------------;;
(def sample
{:component/type [:Schema :v-0-0-1]
:serde :nippy
:topics [{:topic-name "~~"
:partitions 1}]})
(defmethod config-spec [:Schema :v-0-0-1] [& _] any?)
(spec/valid? (config-spec sample) sample)
(spec/explain-data (config-spec sample) sample)
(spec/valid? ::component-config sample)
(spec/explain-data ::component-config sample)
;;------------------------------------------------------------------;;
;;------------------------------------------------------------------;;
While the last spec/valid?
returns true the subsequent spec/explain-data
returns :
#:clojure.spec.alpha{:problems [{:path [[:Schema :v-0-0-1]], :pred hermes.lib.system.alaki/config-spec, :val {:component/type [:Schema :v-0-0-1], :serde :nippy, :topics [{:topic-name "~~", :partitions 1}]}, :via [:hermes.lib.system.alaki/component-config], :in []}], :spec :hermes.lib.system.alaki/component-config, :value {:component/type [:Schema :v-0-0-1], :serde :nippy, :topics [{:topic-name "~~", :partitions 1}]}}
• Spec Version : [org.clojure/spec.alpha "0.3.214"]