Fork me on GitHub
#clojure-spec
<
2022-01-01
>
Ho0man11:01:27

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)

  )

;;------------------------------------------------------------------;;
;;------------------------------------------------------------------;;

Ho0man12:01:55

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"]

lassemaatta13:01:49

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.

Ho0man10:01:08

Year you're right. Thanks a lot @U0178V2SLAY.

Ho0man12:01:55
replied to a thread: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? (-&gt; x :component/system? true?)] (case system? true (-&gt; :systemized-component) false (-&gt; 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"]