Skip to content

Commit

Permalink
Merge pull request #287 from metosin/test-267
Browse files Browse the repository at this point in the history
267-tests + format + deps
  • Loading branch information
ikitommi authored Jun 29, 2024
2 parents ea023e5 + 70f69fc commit 642fffd
Show file tree
Hide file tree
Showing 34 changed files with 1,321 additions and 1,268 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(ns clj-kondo.com.gfredericks.test.chuck.checking
(:require
[clj-kondo.hooks-api :as api]))

(defn checking
[{{:keys [children]} :node}]
(let [[_checking desc & opt+bindings+body] children
[opts binding-vec & body] (if (api/vector-node? (first opt+bindings+body))
(into [(api/map-node {})] opt+bindings+body)
opt+bindings+body)]
(when-not (even? (count (:children binding-vec)))
(throw (ex-info "checking requires an even number of bindings" {})))
{:node (api/list-node
(list*
(api/token-node 'let)
(api/vector-node (into [(api/token-node (symbol (gensym "_checking-desc"))) desc]
(:children binding-vec)))
opts
body))}))
4 changes: 4 additions & 0 deletions .clj-kondo/com.gfredericks/test.chuck/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{:hooks
{:analyze-call
{com.gfredericks.test.chuck.clojure-test/checking
clj-kondo.com.gfredericks.test.chuck.checking/checking}}}
1 change: 1 addition & 0 deletions .clj-kondo/prismatic/schema/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:lint-as {schema.test/deftest clojure.test/deftest}}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
/target
pom.xml
pom.xml.asc
.cache
node_modules
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Unreleased

* Support transforming schema to JSON Schema. PR [#281](https://github.com/metosin/spec-tools/pull/281)
* Support transforming multi-spec to JSON Schema. PR [#281](https://github.com/metosin/spec-tools/pull/281)
* FIX :reason doesn't compose as expected [#171](https://github.com/metosin/spec-tools/issues/171)
* Update Dependencies

```clojure
[org.clojure/spec.alpha "0.5.238"] is available but we use "0.3.218"
```

# 0.10.6 (2023-08-28)

Expand Down
16 changes: 8 additions & 8 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
:scm {:name "git"
:url "https://github.com/metosin/spec-tools"}

:dependencies [[org.clojure/spec.alpha "0.3.218"]]
:dependencies [[org.clojure/spec.alpha "0.5.238"]]

:profiles {:dev {:plugins [[jonase/eastwood "1.4.0"]
:profiles {:dev {:plugins [[jonase/eastwood "1.4.2"]
[lein-tach "1.1.0"]
[lein-doo "0.1.11"]
[lein-cljsbuild "1.1.8"]
Expand All @@ -27,21 +27,21 @@
[lein-pprint "1.3.2"]]
:jvm-opts ^:replace ["-server"]
;:global-vars {*warn-on-reflection* true}
:dependencies [[org.clojure/clojure "1.11.1"]
[org.clojure/clojurescript "1.11.60"]
:dependencies [[org.clojure/clojure "1.11.3"]
[org.clojure/clojurescript "1.11.132"]
[criterium "0.4.6"]
[prismatic/schema "1.4.1"]
[org.clojure/test.check "1.1.1"]
[org.clojure/tools.namespace "1.4.4"]
[org.clojure/tools.namespace "1.5.0"]
[com.gfredericks/test.chuck "0.2.14"]
; com.bhauman/spell-spec library doesn't get any updates, so it has to be copied here
; under spec-tools.spell-spec namespace in order to fix its bugs.
; If the library gets updated with fixes it would be desirable to switch back to it.
;[com.bhauman/spell-spec "0.1.1"]
[expound "0.9.0"]
[metosin/muuntaja "0.6.8"]
[metosin/ring-swagger "0.26.2"]
[metosin/jsonista "0.3.7"]
[metosin/muuntaja "0.6.10"]
[metosin/ring-swagger "1.0.0"]
[metosin/jsonista "0.3.8"]
[metosin/scjsv "0.6.2"]]}
:perf {:jvm-opts ^:replace ["-server"]}}
:aliases {"all" ["with-profile" "dev"]
Expand Down
139 changes: 69 additions & 70 deletions src/spec_tools/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
[cljs.reader]
[cljs.spec.gen.alpha :as gen]]))
(:import
#?@(:clj
[(clojure.lang AFn IFn Var)
(java.io Writer)])))
#?@(:clj [(clojure.lang IFn)
(java.io Writer)])))

;;
;; helpers
Expand Down Expand Up @@ -47,12 +46,12 @@
if no spec was found."
[name-or-spec]
(or
(and (spec? name-or-spec) name-or-spec)
(get-spec name-or-spec)
(throw
(ex-info
(str "can't coerce to spec: " name-or-spec)
{:name-or-spec name-or-spec}))))
(and (spec? name-or-spec) name-or-spec)
(get-spec name-or-spec)
(throw
(ex-info
(str "can't coerce to spec: " name-or-spec)
{:name-or-spec name-or-spec}))))

(defn ^:skip-wiki serialize
"Writes specs into a string that can be read by the reader.
Expand Down Expand Up @@ -144,36 +143,36 @@
(def json-transformer
"Transformer that transforms data between JSON and EDN."
(type-transformer
{:name :json
:decoders stt/json-type-decoders
:encoders stt/json-type-encoders
:default-encoder stt/any->any}))
{:name :json
:decoders stt/json-type-decoders
:encoders stt/json-type-encoders
:default-encoder stt/any->any}))

(def string-transformer
"Transformer that transforms data between Strings and EDN."
(type-transformer
{:name :string
:decoders stt/string-type-decoders
:encoders stt/string-type-encoders
:default-encoder stt/any->any}))
{:name :string
:decoders stt/string-type-decoders
:encoders stt/string-type-encoders
:default-encoder stt/any->any}))

(def strip-extra-keys-transformer
"Transformer that drop extra keys from `s/keys` specs."
(type-transformer
{:name ::strip-extra-keys
:decoders stt/strip-extra-keys-type-decoders}))
{:name ::strip-extra-keys
:decoders stt/strip-extra-keys-type-decoders}))

(def strip-extra-values-transformer
"Transformer that drop extra values from `s/tuple` specs."
(type-transformer
{:name ::strip-extra-values
:decoders stt/strip-extra-values-type-decoders}))
{:name ::strip-extra-values
:decoders stt/strip-extra-values-type-decoders}))

(def fail-on-extra-keys-transformer
"Transformer that fails on extra keys in `s/keys` specs."
(type-transformer
{:name ::fail-on-extra-keys
:decoders stt/fail-on-extra-keys-type-decoders}))
{:name ::fail-on-extra-keys
:decoders stt/fail-on-extra-keys-type-decoders}))

;;
;; Transforming
Expand Down Expand Up @@ -291,20 +290,20 @@

(defmethod walk :or [{:keys [::parse/items]} value accept options]
(reduce
(fn [v item]
(let [transformed (accept item v options)
valid? (some-> item :spec (s/valid? transformed))]
(if valid?
(reduced transformed)
transformed)))
value items))
(fn [v item]
(let [transformed (accept item v options)
valid? (some-> item :spec (s/valid? transformed))]
(if valid?
(reduced transformed)
transformed)))
value items))

(defmethod walk :and [{:keys [::parse/items]} value accept options]
(reduce
(fn [v item]
(let [transformed (accept item v options)]
transformed))
value items))
(fn [v item]
(let [transformed (accept item v options)]
transformed))
value items))

(defmethod walk :nilable [{:keys [::parse/item]} value accept options]
(accept item value options))
Expand Down Expand Up @@ -334,23 +333,23 @@
(defmethod walk :map [{:keys [::parse/key->spec]} value accept options]
(if (map? value)
(reduce-kv
(fn [acc k v]
(let [spec (if (qualified-keyword? k) (s/get-spec k) (s/get-spec (get key->spec k)))
value (if spec (accept spec v options) v)]
(assoc acc k value)))
value
value)
(fn [acc k v]
(let [spec (if (qualified-keyword? k) (s/get-spec k) (s/get-spec (get key->spec k)))
value (if spec (accept spec v options) v)]
(assoc acc k value)))
value
value)
value))

(defmethod walk :map-of [{:keys [::parse/key ::parse/value]} data accept options]
(if (map? data)
(reduce-kv
(fn [acc k v]
(let [k' (accept key k options)
v' (accept value v options)]
(assoc acc k' v')))
(empty data)
data)
(fn [acc k v]
(let [k' (accept key k options)
v' (accept value v options)]
(assoc acc k' v')))
(empty data)
data)
data))

(defmethod walk :multi-spec [{:keys [::parse/key ::parse/dispatch]} data accept options]
Expand All @@ -367,16 +366,16 @@
(defn- extra-spec-map [data]
(->> (dissoc data :form :spec)
(reduce
(fn [acc [k v]]
(if (= "spec-tools.parse" (namespace k)) acc (assoc acc k v)))
{})))
(fn [acc [k v]]
(if (= "spec-tools.parse" (namespace k)) acc (assoc acc k v)))
{})))

(defn- fail-on-invoke [spec]
(throw
(ex-info
(str
"Can't invoke spec with a non-function predicate: " spec)
{:spec spec})))
(ex-info
(str
"Can't invoke spec with a non-function predicate: " spec)
{:spec spec})))

(defn- leaf? [spec]
(:leaf? (into-spec spec)))
Expand Down Expand Up @@ -460,17 +459,17 @@
spec-reason (:reason this)
with-reason (fn [problem]
(cond-> problem
(and spec-reason (not (:reason problem)))
(assoc :reason spec-reason)))]
(and spec-reason (not (:reason problem)))
(assoc :reason spec-reason)))]
(if problems
(map with-reason problems))))

(gen* [this overrides path rmap]
(if-let [gen (:gen this)]
(gen)
(or
(gen/gen-for-pred spec)
(s/gen* (or (s/spec? spec) (s/specize* spec)) overrides path rmap))))
(gen/gen-for-pred spec)
(s/gen* (or (s/spec? spec) (s/specize* spec)) overrides path rmap))))

(with-gen* [this gfn]
(assoc this :gen gfn))
Expand All @@ -488,9 +487,9 @@
[^Spec t ^Writer w]
(.write w (str "#Spec"
(clojure.core/merge
(select-keys t [:form])
(if (:type t) (select-keys t [:type]))
(extra-spec-map t))))))
(select-keys t [:form])
(if (:type t) (select-keys t [:type]))
(extra-spec-map t))))))

(defn spec? [x]
(if (instance? Spec x) x))
Expand Down Expand Up @@ -550,7 +549,7 @@
type (if (contains? m :type) type (:type info))
name (-> spec meta ::s/name)
record (map->Spec
(clojure.core/merge m info {:spec spec :form form :type type :leaf? (parse/leaf-type? type)}))]
(clojure.core/merge m info {:spec spec :form form :type type :leaf? (parse/leaf-type? type)}))]
(cond-> record name (with-meta {::s/name name}))))

#?(:clj
Expand All @@ -575,10 +574,10 @@
form# '~(impl/resolve-form &env pred)]
(assert (map? info#) (str "spec info should be a map, was: " info#))
(create-spec
(clojure.core/merge
info#
{:form form#
:spec ~pred}))))))
(clojure.core/merge
info#
{:form form#
:spec ~pred}))))))

(defn- into-spec [x]
(cond
Expand Down Expand Up @@ -613,11 +612,11 @@
(gen* [_ overrides path rmap]
(s/gen* merge-spec overrides path rmap)))]
(create-spec
(clojure.core/merge
{:spec spec
:form spec-form
:type :map}
(apply merge-with set/union form-keys)))))
(clojure.core/merge
{:spec spec
:form spec-form
:type :map}
(apply merge-with set/union form-keys)))))

#?(:clj
(defmacro merge [& forms]
Expand Down
Loading

0 comments on commit 642fffd

Please sign in to comment.