|
3 | 3 | #?(:cljs (:require-macros malli.experimental)) |
4 | 4 | (:require [clojure.core :as c] |
5 | 5 | [malli.core :as m] |
6 | | - [malli.destructure :as md])) |
| 6 | + [malli.destructure :as md] |
| 7 | + [malli.dev.pretty :as pretty])) |
7 | 8 |
|
8 | | -(c/defn -schema [inline-schemas] |
| 9 | +(c/defn -defn-schema [inline-schemas] |
9 | 10 | (m/schema |
10 | 11 | [:schema |
11 | 12 | {:registry {"Schema" any? |
|
32 | 33 | [:meta [:? :map]]]]]]]}} |
33 | 34 | "Params"])) |
34 | 35 |
|
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)) |
37 | 58 |
|
38 | 59 | (c/defn -defn [schema args] |
39 | 60 | (let [{:keys [name return doc meta arities] :as parsed} (m/parse schema args) |
|
53 | 74 | (m/=> ~name ~schema) |
54 | 75 | defn#))) |
55 | 76 |
|
| 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 | + |
56 | 88 | ;; |
57 | 89 | ;; public api |
58 | 90 | ;; |
59 | 91 |
|
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