This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-01-13
Channels
- # announcements (13)
- # beginners (52)
- # bitcoin (2)
- # calva (2)
- # cider (7)
- # clara (1)
- # clj-commons (11)
- # clj-kondo (6)
- # cljdoc (14)
- # clojure (68)
- # clojure-belgium (1)
- # clojure-denmark (6)
- # clojure-europe (57)
- # clojure-nl (2)
- # clojure-norway (10)
- # clojure-uk (3)
- # clojurescript (7)
- # code-reviews (17)
- # conjure (1)
- # cursive (5)
- # dev-tooling (11)
- # emacs (9)
- # fulcro (12)
- # hugsql (20)
- # introduce-yourself (6)
- # joyride (2)
- # leiningen (1)
- # lsp (61)
- # malli (30)
- # missionary (11)
- # nbb (6)
- # off-topic (26)
- # portal (5)
- # practicalli (5)
- # re-frame (8)
- # releases (8)
- # sci (21)
- # shadow-cljs (3)
- # sql (17)
- # squint (1)
- # xtdb (3)
Is anyone setting up mock functions with malli? It’s so cool!
(mx/defn eff :- string? [x :- int?]
(str x))
(def mock-eff (mg/generate (m/form (:schema (meta #'eff)))))
;; mock called with good input, returns output that matches the return schema
(mock-eff 3)
"72eSV3WQpD6d9"
;; mock throws (just like the real thing would) on bad input
(try (mock-eff "3")
(catch Exception e (ex-data e)))
;; => {:type :malli.core/invalid-input,
;; :message :malli.core/invalid-input,
;; :data {:input [:cat int?], :args ["3"], :schema [:=> [:cat int?] string?]}}
Nice trick! Thanks for sharing. Maybe also interesting to start with this implementation and complete it when you need to.
How would you approach customizing the generators for these kind of mocks?
Would it be custom :gen
at the mx/defn
level? Or override the schema with custom registry at the (partial mg/generate)
? Or something else?
It’d be nice to put a metadata on a function definition and have it autogen, then take it off recompile and no autogen. Maybe this already exists
So in theory you could build an application this way without filling in the details and have it somewhat working. And if it doesn’t work it would be logical errors like reusing a password, maybe other uniqueness like things, but you could get pretty far I guess. Sounds good!
Would there be any interest in having humanize
say the type that's given? Instead of just {:some-num ["should be an integer"]}
, it could say {:some-num ["should be an integer, given a string"]}
you can get something similar with https://github.com/metosin/malli/blob/master/docs/tips.md#getting-error-values-into-humanized-result
yes, adding the humanized value type into the error message makes sense - as an optional :wrap
impl for example.
When writing encoders/decoders for a transformer, is there a way to match by schema and not just :int or function symbols? For instance
(defn x-transformer [] (mt/transformer
{:decoders {'str-stat-schema (fn [x] (:value x))}
:encoders {'str-stat-schema (fn [x] {:value x})}}))
Was hoping that would work. Would like to make decoders to convert one schema to anotheryou can attach decode functions into any schema at the moment, e.g. have any schema control how it’s being decoded. If you want to push the control to transformer, there has to be a way to identify the schema. it can be any data pulled out of a schema.
here’s one way to do it using schema references (references need to be strings of qualified keywords):
(let [schema [:schema {:registry {::str-stat-schema :string}}
::str-stat-schema]
decoders {::str-stat-schema (fn [x] (:value x))}
encoders {::str-stat-schema (fn [x] {:value x})}
compiler (fn [schema _] (when (m/-ref-schema? schema) (m/-ref schema)))
transformer (mt/transformer
{:default-decoder {:compile (comp decoders compiler)}
:default-encoder {:compile (comp encoders compiler)}})
decode (m/decoder schema transformer)
encode (m/encoder schema transformer)]
((juxt identity decode (comp encode decode))
{:value "kikka"}))
; => [{:value "kikka"} "kikka" {:value "kikka"}]
but, it’s much easier to inject the encoders & decoders into schemas:
(def str-stat-schema
[:string {:decode/custom (fn [x] (:value x))
:encode/custom (fn [x] {:value x})}])
(-> [:map
[:x str-stat-schema]
[:y str-stat-schema]]
(m/decode
{:x {:value "kikka"}
:y {:value "kukka"}}
(mt/transformer {:name :custom})))
; => {:x "kikka", :y "kukka"}
This was helpful. The plan was to make easy transformations to convert from one schema to another. The first option may be best for that, but it's very verbose. I suppose I should just make my own functions that do this manually.
I've been working on integrating malli with react-hook-form for a bit (mostly learning react-hook-form :P ), and now have something working! https://github.com/dvingo/malli-react-hook-form You can play with it here: https://dvingo.github.io/malli-react-hook-form/ I'm sure there may be some tricky things for more complex data shapes, but this is a promising start!
Looks familiar! I wrote a POC for something similar a long time ago. http://escherize.com/works/data-desk
Oh, I thought you were generating this form: https://github.com/dvingo/malli-react-hook-form/blob/mainline/src/app/malli_react_hook_form/entry.cljs#L44
no worries! that's definitely the direction I'm heading :D (generating forms from schemas) this part was just figuring out wiring of the pieces of reusable parts, next will be outputting these from the schema
good to see work on malli->forms! I really would like have that, have few non-complete prototypes and most likely no time to finish.
I'll share a fresh thread from #clojurescript here in an attempt to connect people with similar issues https://clojurians.slack.com/archives/C03S1L9DN/p1673488701932789
just updated to generate the form inputs from the schema https://github.com/dvingo/malli-react-hook-form/blob/8f2076cc27d23c96795c45f825c474e544f5d815/src/app/malli_react_hook_form/entry.cljs#L71
I uploaded the lastest version of my version lately too:
https://escherize.com/works/data-desk/
I think to do it right requires using a similar strategy to malli/experimental/describe.cljc
where there’s a multimethod over the schema types.
Might be cool to do using htmx, too.
nice! do you have the source published or no? And yep for sure, will need to deal with all the built in schemas plus nested/refs
just updated to generate the form inputs from the schema https://github.com/dvingo/malli-react-hook-form/blob/8f2076cc27d23c96795c45f825c474e544f5d815/src/app/malli_react_hook_form/entry.cljs#L71