Closed jasonjckn closed 1 year ago
You can already add Class -> Schema via registry:
(m/validate [:tuple String Long]
["123" 123]
{:registry (merge (m/default-schemas)
{String (m/-string-schema)
Long (m/-int-schema)})})
; => true
... supporting classes out of the box would be nice, but not sure then there would be at least 3 ways to describe a string:
string?
-> spec-like predicates:string
-> malli schemaString
-> java Classnot sure if this makes the library easier to use.
... maybe there should be a :instance
schema? e.g.
(m/validate [:maybe [:instance datomic.db.Db]] my-db) ;; => true
When I started down this path, i was hoping to handle ALL classes, interfaces & protocols out-of-the-box e.g.
(extend-protocol m/IntoSchema
java.lang.Class
.... instance? ....
clojure.imaginary.Protocol
..... satisfies? .......
But protocols don't have their own Class to dispatch off of, rather are persistent data structures, and
(extend-protocol m/IntoSchema
clojure.lang.PersistentArrayMap
Seems a bit janky, given all that, so i'm a lot less enthusiastic my original idea
Also, to your point about the performance of satisfies?
, folks may want fine grained control e.g. [:maybe [:instance MyProtocol]]
, [:maybe [:satisfies MyProtocol]]
,
I can send a PR for that if you'd like... but since this is so close to [:fn #(satisfies? P %)]
maybe it doesn't warrant its own abstraction... am split 50/50 on this too, maybe it's just better to keep malli API leen.
"You can already add Class -> Schema via registry"
sure, but I still need something like :instance
.
(m/validate [:tuple String Long]
["123" 123]
{:registry (merge (m/default-schemas)
{datomic.db.Db [:maybe [:instance datomic.db.Db]]})})
going to close this, while i'm going to commit this code to my own codebase, i don't feel like it's worthy of malli codebase yet... still needs hammock time for supporting all of classes, interfaces, protocols, etc — not just classes.
Please see https://github.com/metosin/malli/issues/960 though, i'm still interested in the capability of -being able to do this-.
It would be nice if I could write code like
(or likewise for protocols.)
and it would just work out of the box .
If someone can confirm this is an acceptable feature, i'll write a PR.
e.g. ............ (work in progress) any early feedback?
protocols might be more challenging TBD.