Skip to content

Commit 9d21743

Browse files
committed
mx/def
1 parent 546eb66 commit 9d21743

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
{:lint-as {malli.experimental/defn schema.core/defn}
1+
{:lint-as {malli.experimental/defn schema.core/defn
2+
malli.experimental/def schema.core/def}
23
:linters {:unresolved-symbol {:exclude [(malli.core/=>)]}}}

src/demo.clj

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
(ns demo
2+
(:require [malli.experimental :as mx]
3+
[malli.provider :as mp]))
4+
5+
;; pull schema from example
6+
(def Config
7+
(mp/provide
8+
[{:port 8080
9+
:host "127.0.0.1"
10+
:db {:adapter "postgresql"
11+
:username "example"
12+
:password "example"
13+
:server-name "localhost"
14+
:port-number 5432
15+
:database-name "example"}}]))
16+
17+
;; fail fast
18+
(mx/def config :- Config
19+
{:port "8080"
20+
:host "127.0.0.1"
21+
:db {:adapter "postgresql"
22+
:username "example"
23+
:password "example"
24+
:server-name "localhost"
25+
:port-number "5432"
26+
:database-name :example}})

src/malli/experimental.cljc

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
#?(:cljs (:require-macros malli.experimental))
44
(:require [clojure.core :as c]
55
[malli.core :as m]
6-
[malli.destructure :as md]))
6+
[malli.destructure :as md]
7+
[malli.dev.pretty :as pretty]))
78

8-
(c/defn -schema [inline-schemas]
9+
(c/defn -defn-schema [inline-schemas]
910
(m/schema
1011
[:schema
1112
{:registry {"Schema" any?
@@ -32,8 +33,28 @@
3233
[:meta [:? :map]]]]]]]}}
3334
"Params"]))
3435

35-
(def SchematizedParams (-schema true))
36-
(def Params (-schema false))
36+
(c/defn -def-schema [inline-schemas]
37+
(m/schema
38+
[:schema
39+
{:registry {"Schema" any?
40+
"Separator" (if inline-schemas [:= :-] md/Never)
41+
"Params" [:catn
42+
[:name symbol?]
43+
[:return [:? [:catn
44+
[:- "Separator"]
45+
[:schema "Schema"]]]]
46+
[:doc [:? string?]]
47+
[:body any?]]}}
48+
"Params"]))
49+
50+
(def SchematizedDefnParams (-defn-schema true))
51+
(def DefnParams (-defn-schema false))
52+
53+
(def ^:depecated SchematizedParams SchematizedDefnParams)
54+
(def ^:depecated Params DefnParams)
55+
56+
(def SchematizedDefParams (-def-schema true))
57+
(def DefParams (-def-schema false))
3758

3859
(c/defn -defn [schema args]
3960
(let [{:keys [name return doc meta arities] :as parsed} (m/parse schema args)
@@ -53,8 +74,20 @@
5374
(m/=> ~name ~schema)
5475
defn#)))
5576

77+
(c/defn -def [schema args]
78+
(let [{:keys [name doc body] {:keys [schema]} :return :as parsed} (m/parse schema args)]
79+
(when (= ::m/invalid parsed) (m/-fail! ::parse-error {:schema schema, :args args}))
80+
`(let [def# (def
81+
~(with-meta name {:schema schema})
82+
~@(some-> doc vector))]
83+
(when (and ~schema (not (m/validate ~schema ~body)))
84+
(pretty/explain ~schema ~body)
85+
(m/-fail! ::invalid-data {:def ~name, :schema ~schema, :body ~body}))
86+
def#)))
87+
5688
;;
5789
;; public api
5890
;;
5991

60-
#?(:clj (defmacro defn [& args] (-defn SchematizedParams args)))
92+
#?(:clj (defmacro defn [& args] (-defn SchematizedDefnParams args)))
93+
#?(:clj (defmacro def [& args] (-def SchematizedDefParams args)))

0 commit comments

Comments
 (0)