metosin / malli

High-performance data-driven data specification library for Clojure/Script.
Eclipse Public License 2.0
1.48k stars 210 forks source link

Feature: extend-protocol IntoSchema for java.lang.Class and protocols #961

Closed jasonjckn closed 1 year ago

jasonjckn commented 1 year ago

It would be nice if I could write code like

(m/validate [:maybe datomic.db.Db] my-db) ;; => true 

(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?

(extend-protocol m/IntoSchema
  java.lang.Class
  (-type [^Class this] (symbol (.getName this)))
  (-type-properties [this])
  (-properties-schema [this options])
  (-children-schema [this options])
  (-into-schema [^Class this properties children options]
    (m/schema
      [:fn (merge {:error/message (str "should be an instance of java " this)} properties)
       #(clojure.core/instance? this %)]
      options)))

(m/validate String "foo") ;;=>  true 

protocols might be more challenging TBD.

ikitommi commented 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
ikitommi commented 1 year ago

... supporting classes out of the box would be nice, but not sure then there would be at least 3 ways to describe a string:

not sure if this makes the library easier to use.

ikitommi commented 1 year ago

... maybe there should be a :instance schema? e.g.

(m/validate [:maybe [:instance datomic.db.Db]] my-db) ;; => true 
jasonjckn commented 1 year ago

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]]})})
jasonjckn commented 1 year ago

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-.