Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick poke on schematized def #776

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 arities] body-meta :meta :as parsed} (m/parse schema args)
Expand Down Expand Up @@ -64,8 +85,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)))