Skip to content

Instantly share code, notes, and snippets.

@geraldodev
Created April 30, 2024 17:59
Show Gist options
  • Save geraldodev/6c69759db927c018e2d5f491f5f4be31 to your computer and use it in GitHub Desktop.
Save geraldodev/6c69759db927c018e2d5f491f5f4be31 to your computer and use it in GitHub Desktop.
How do I get the "primary" (simplest and most primitive) type of a Malli schema?e.g. :string => :string [:and :string [:fn ,,,]] => :string
(let [kinds #{:map :tuple :vector}
fn-kind {string? :string
int? :int
boolean? :boolean
double? :double}
type-kind {(type "") :string}
simplest-form (fn [form]
(cond
(keyword? form)
form
(fn? form)
(get fn-kind form "not handled fn form")
(vector? form)
(let [[kind-or-and kind] form]
(or (get kinds kind-or-and)
(get kinds kind)
(get fn-kind kind)
(and (= kind-or-and :enum) (type-kind (type kind)) )
"unhandled vector form"))
:else "not handled and not branched")
)]
(for [schema [[:enum "s" "b" "d"]
string?
int?
:string
[:and boolean? [:fn identity]]
[:tuple {:title "location"} :double :double]
[:map [:x int?] [:y int?]]
]]
[(simplest-form schema) " <- " schema]
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment