Fork me on GitHub
#malli
<
2022-11-03
>
jeroenvandijk08:11:59

Did someone already use rewrite-cljc to point out malli validation errors inline? I believe it is possible and I’m thinking of doing this

jeroenvandijk09:11:47

All little bit how expound underlines invalid values with ^^^ (see https://github.com/bhb/expound#expound-1), so a little different than .pretty/explain i think. More like the output of babashka with for instance:

echo '(/ 100 0)' > foo.clj; bb foo.clj 

....
----- Context ------------------------------------------------------------------
1: (/ 100 0)
   ^--- Divide by zero

ikitommi09:11:09

I would also like to see those. Also, paiting the errors with pretty, like expound does it.

ikitommi09:11:35

drafted mx/def on the way to ClojureDays, which could have a clj-kondo hook to highlight the errors in the editor: https://twitter.com/ikitommi/status/1585995979748409345. ping @borkdude

❤️ 1
borkdude09:11:21

rewrite-cljc is now rewrite-clj maintained under org.clj-commons

👍 1
jeroenvandijk09:11:51

ah yeah made a mistake rewrite-clj indeed

jeroenvandijk09:11:12

However just found that I can probably do what I want with edamame as well

jeroenvandijk09:11:51

Ah useful thank you!

borkdude09:11:10

@ikitommi I think we could add "type checking" for def - this is what you need right?

ikitommi09:11:02

@borkdude “type-checking” using clj-kondo type system, yes, that would be awesome!

ikitommi09:11:20

another try would be use use malli for the validation and just emit errors. But that’s way more work I think (all the relevant schemas should be loaded, e.g. should need to run the whole program to figure our what is the schema)

ikitommi09:11:53

so, should the mx/def emit clj-kondo types?

borkdude10:11:09

@ikitommi when clj-kondo supports this yes, but currently I don't think it supports it yet

borkdude10:11:17

let's see if there is already an issue for it

borkdude10:11:31

ah yes, a very old one :rolling_on_the_floor_laughing: https://github.com/clj-kondo/clj-kondo/issues/609

borkdude10:11:37

no upvotes ;)

ikitommi10:11:25

now, one! 🙂

ikitommi10:11:15

but, would that work here? the mx/def says “here’s a var which should have a value of type XYZ”

borkdude10:11:33

there is no support for this yet, I think

borkdude10:11:38

only for functions

borkdude10:11:59

or perhaps there is

ikitommi10:11:03

could hooks work here?

borkdude10:11:06

maybe try {:tag :int} or so

ikitommi10:11:14

:thinking_face:

borkdude10:11:20

I have to go now, be back in an hour

ikitommi10:11:31

:tag takes a clj-kondo type?

borkdude10:11:59

yes: {:namespaces {foo {bar {:tag :int}}}} - if you are lucky this works, not sure

jeroenvandijk14:11:47

FYI @ikitommi, I have something that I’m happy with (@borkdude also thanks to edamame), see https://gist.github.com/jeroenvandijk/080370966fadb7e65601931c3de47ed5#file-malli_inline_output-clj-L129. Here is some example output:

1: {:b :z
         ^------- should be an integer
  2: 
  3: :c {:c0 2}
              ^------- missing required key :x
  4: 
  5: }
     ^------- missing required key :a

🎉 4
lread14:11:18

Oh that's pretty neat @U0FT7SRLP. I might experiment with this strategy for reporting errors in cljdoc.edn files.

❤️ 1
borkdude14:11:15

@U0FT7SRLP Cool stuff, I think it's worth a blog post or maybe an article in the malli or edamame repo :)

2
jeroenvandijk14:11:39

Thanks @borkdude! Yeah I guess it can be useful for others, or in other contexts. Also it is combining some funny techniques together. Will think about an article. Do you have an example format you are thinking of?

jeroenvandijk14:11:25

Btw, I have created a similar mechanism for giving feedback on invalid edn input, also using Edamame. Maybe can include this in the same article

borkdude14:11:47

I'm open to having an examples/malli/README.md thing in edamame for example, or similar, or doc/postprocess.md or so

borkdude14:11:59

but if you want to post it in your own blog, also fine, then we can link there

borkdude14:11:26

If you don't have a blog, you can quickly create one with ... https://github.com/borkdude/quickblog :)

jeroenvandijk14:11:59

haha good one, let me think about it for a bit. No blog at the moment. I’ll come back to it

borkdude14:11:11

no pressure

ikitommi15:11:44

Looks great! Also would like see both the code and the post 👍

borkdude11:11:40

(Need to fix a few test...

Ran 366 tests containing 3188 assertions.
80 failures, 8 errors.
)

ikitommi11:11:12

oh, super! that was fast. I’ll emit the clj-kondo types when this ships. So, this would work with complex types like maps too and also for defining the values? e.g. “this var should hold a map of :age key with int value” and saying (def mything {:age "1"}) would be a failure?

borkdude11:11:37

I'll have to test it :)

👌 1
borkdude11:11:47

Getting closer:

Ran 366 tests containing 3233 assertions.
1 failures, 0 errors.

borkdude11:11:23

This is obviously wrong facepalm

borkdude12:11:49

Ran 366 tests containing 3233 assertions.
0 failures, 0 errors.
🎉

borkdude12:11:09

Still getting false positives with test corpus... lunch time

borkdude12:11:54

It's funny, I'm getting errors from dynamic vars which are initialized to nil and then later called with swap! - I guess I'll have to ignore dynamic vars

borkdude13:11:46

so swap! is giving a type error

borkdude13:11:30

@ikitommi Now got this working:

$ clojure -M:clj-kondo/dev --lint - --config '{:linters {:type-mismatch {:namespaces {user {x {:tag {:op :keys :req {:x :any}} }}}}}}' <<< "(def x) (inc x)"
<stdin>:1:14: warning: Expected: number, received: map.

borkdude13:11:52

So here we declare that x is a map that has at least an :x key

borkdude13:11:06

but this is probably the reverse of what you were proposing. the config in clj-kondo is to override types that have been inferred, possibly incorrectly

borkdude14:11:03

Perhaps clj-kondo should both check the type and take the config as the source of truth...?

borkdude14:11:26

I'm not sure how one would use that in practice

J09:11:49

Hi guys! Did you know if there a lib to convert a plumatic schema to a malli schema?

jeroenvandijk12:11:53

Not sure if it exists, but maybe the lite syntax can help you convert your plumatic schema https://github.com/metosin/malli#lite

👍 1
jeroenvandijk14:11:47

FYI @ikitommi, I have something that I’m happy with (@borkdude also thanks to edamame), see https://gist.github.com/jeroenvandijk/080370966fadb7e65601931c3de47ed5#file-malli_inline_output-clj-L129. Here is some example output:

1: {:b :z
         ^------- should be an integer
  2: 
  3: :c {:c0 2}
              ^------- missing required key :x
  4: 
  5: }
     ^------- missing required key :a

🎉 4