Skip to content

Commit

Permalink
mx/def
Browse files Browse the repository at this point in the history
  • Loading branch information
ikitommi committed Oct 28, 2022
1 parent 546eb66 commit 9d21743
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
{:lint-as {malli.experimental/defn schema.core/defn}
{:lint-as {malli.experimental/defn schema.core/defn
malli.experimental/def schema.core/def}
:linters {:unresolved-symbol {:exclude [(malli.core/=>)]}}}
26 changes: 26 additions & 0 deletions src/demo.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(ns demo
(:require [malli.experimental :as mx]
[malli.provider :as mp]))

;; pull schema from example
(def Config
(mp/provide
[{:port 8080
:host "127.0.0.1"
:db {:adapter "postgresql"
:username "example"
:password "example"
:server-name "localhost"
:port-number 5432
:database-name "example"}}]))

;; fail fast
(mx/def config :- Config
{:port "8080"
:host "127.0.0.1"
:db {:adapter "postgresql"
:username "example"
:password "example"
:server-name "localhost"
:port-number "5432"
:database-name :example}})
43 changes: 38 additions & 5 deletions src/malli/experimental.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
#?(:cljs (:require-macros malli.experimental))
(:require [clojure.core :as c]
[malli.core :as m]
[malli.destructure :as md]))
[malli.destructure :as md]
[malli.dev.pretty :as pretty]))

(c/defn -schema [inline-schemas]
(c/defn -defn-schema [inline-schemas]
(m/schema
[:schema
{:registry {"Schema" any?
Expand All @@ -32,8 +33,28 @@
[:meta [:? :map]]]]]]]}}
"Params"]))

(def SchematizedParams (-schema true))
(def Params (-schema false))
(c/defn -def-schema [inline-schemas]
(m/schema
[:schema
{:registry {"Schema" any?
"Separator" (if inline-schemas [:= :-] md/Never)
"Params" [:catn
[:name symbol?]
[:return [:? [:catn
[:- "Separator"]
[:schema "Schema"]]]]
[:doc [:? string?]]
[:body any?]]}}
"Params"]))

(def SchematizedDefnParams (-defn-schema true))
(def DefnParams (-defn-schema false))

(def ^:depecated SchematizedParams SchematizedDefnParams)
(def ^:depecated Params DefnParams)

(def SchematizedDefParams (-def-schema true))
(def DefParams (-def-schema false))

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

(c/defn -def [schema args]
(let [{:keys [name doc body] {:keys [schema]} :return :as parsed} (m/parse schema args)]
(when (= ::m/invalid parsed) (m/-fail! ::parse-error {:schema schema, :args args}))
`(let [def# (def
~(with-meta name {:schema schema})
~@(some-> doc vector))]
(when (and ~schema (not (m/validate ~schema ~body)))
(pretty/explain ~schema ~body)
(m/-fail! ::invalid-data {:def ~name, :schema ~schema, :body ~body}))
def#)))

;;
;; public api
;;

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

0 comments on commit 9d21743

Please sign in to comment.