Fork me on GitHub
#malli
<
2022-06-20
>
Panel04:06:03

Could it be done to validate with an async fn ?

ikitommi16:06:13

not sure what is the question in here.

borkdude12:06:06

How do you express [s/Long] (vector of Long in Schema) in malli?

ikitommi12:06:26

a [:vector :int] is close, but for all int?s.

ikitommi12:06:54

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

ikitommi12:06:48

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

ikitommi12:06:26

someone asked full support for using Java Classes as Schemas. But that would not work with cljs.

ikitommi12:06:51

would allow [:vector Long]

borkdude12:06:49

@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)

ikitommi12:06:54

yeah, that looks great!

ikitommi12:06:02

there is the malli-lite syntax too.

ikitommi12:06:04

(defn coercer [schema]
  (m/decoder (lite/schema schema) (mt/string-transformer)))

((coercer {:a :int}) {:a "123"})
; => {:a 123}

👍 1
ikitommi12:06:41

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?

borkdude12:06:55

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

borkdude12:06:19

And so yes, you should do validation of args the same way you were doing it anyways in Clojure (with malli, schema, manual asserts)

borkdude12:06:58

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

borkdude12:06:01

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)

ikitommi12:06:39

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?

borkdude12:06:29

I want to it be an un-opiniated library so it works together with malli, spec, schema or your hand-rolled things

borkdude12:06:22

although we could add some docs on how you can integrate with malli

borkdude12:06:32

the malli-lite syntax seems great for that

ikitommi12:06:33

I think that’s a good choice. Is it possible to make it pluggable?

ikitommi12:06:53

yes, happy to provide the example/glue if there is an extension point for that.

borkdude12:06:18

we could maybe do this using a protocol or multimethod? :thinking_face:

borkdude13:06:22

but even without this, it's easy to plug in your own thing, since after coercion you're dealing with just clojure data

ikitommi14:06:45

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.

Panel23:06:39

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 ?

Lucy Wang01:06:43

Why not? Simply send the raw form data to the server using ajax, the server validates it, then send back the result.

Panel03:06:18

I was wondering if this can be describe as a malli schema, maybe using a Fn schemas.