This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-06-20
Channels
- # announcements (5)
- # asami (7)
- # babashka-sci-dev (36)
- # beginners (50)
- # calva (47)
- # cider (1)
- # clj-kondo (19)
- # clojure (33)
- # clojure-europe (25)
- # clojure-nl (2)
- # clojure-uk (4)
- # clojurescript (26)
- # conjure (2)
- # cursive (5)
- # datalog (6)
- # fulcro (5)
- # graalvm (12)
- # leiningen (1)
- # malli (30)
- # off-topic (5)
- # rdf (4)
- # ring (11)
- # shadow-cljs (55)
for exact class, many ways (could be simpler), but one being:
(def a-long (m/-simple-schema {:pred #(instance? Long %)}))
(m/validate a-long (Integer. 12)) ;=> false
(m/validate a-long (Long. 12)) ; => true
with min, max & json-schema translation:
(def a-long
(m/-simple-schema
{:type :long
:pred #(instance? Long %)
:json-schema {:type "long"}
:property-pred (m/-min-max-pred nil)}))
(m/validate [a-long {:min 10}] (Long. 12)) ; => true
(m/validate [a-long {:min 10}] (Long. 8)) ; => false
someone asked full support for using Java Classes as Schemas. But that would not work with cljs.
@U055NJ5CC The reason I'm asking is that my new (work-in-progress) CLI library is using a notation similar to schema for coercion:
https://github.com/babashka/cli
E.g.: {:coerce {:a :int}}
will turn "--a" "1"
into {:a 1}
and {:coerce {:a [:int]}}
will turn "--a" "1"
into {:a [1]}
. This coercion syntax doesn't have to be as powerful as malli. The idea is that you can coerce command line args into a map and then do validation on that (in your clojure function) using spec or malli (since it's a detail whether you are calling this function from the command line or from the REPL)
(defn coercer [schema]
(m/decoder (lite/schema schema) (mt/string-transformer)))
((coercer {:a :int}) {:a "123"})
; => {:a 123}
so, you will have your own transforming part there (the simplest way to do this, looks simple), but the future processing would be done using a full(er) schema/spec lib?
The idea is inspired by clojure -X, you just call a clojure function from the command line basically. And whether you call this function via the command line, or via the REPL, the validation of arguments needs to happen anyway. So why put the validation logic in a CLI library, while it should probably live inside your app code
And so yes, you should do validation of args the same way you were doing it anyways in Clojure (with malli, schema, manual asserts)
And you can then choose whatever you were using. The main job of the CLI library is to transform strings into data, not much more than that
By keeping it simple, you can also put your coercion "spec" in the deps.edn file (it doesn't require any function symbols, just keywords and collections)
that’s one of the goal of malli too, to give a literal notation for schemas. Using full malli is too big for this case I guess? or does it miss something?
I want to it be an un-opiniated library so it works together with malli, spec, schema or your hand-rolled things
but even without this, it's easy to plug in your own thing, since after coercion you're dealing with just clojure data
without yet looking at the code, multimethod sounds good. Global Side Effects For The Win! 🙂 will check the repo later. thanks for the lib(s), again.
If I have a form and want to validate on things that can only be done on server. Could I validate a schema with an async call ?