Fork me on GitHub
#reitit
<
2019-06-07
>
mitchelkuijpers08:06:56

I am kinda fighting with coercion with plumatic/schema and reitit

mitchelkuijpers08:06:03

I would expect this:

(def entity
  {(schema/required-key :id) schema/Int
   (schema/required-key :type) (schema/enum "company" "contact")
   (schema/required-key :fields) {schema/Str schema/Str}})

mitchelkuijpers08:06:19

To easily work but it fails on the {schema/Str schema/Str} part. At first I even had {schema/Int schema/Str} but I wanted to try out s/Str to test it out.. I guess it makes keywords from them and I should use that. But I would expect it to do this for me with the "coercion"

mitchelkuijpers08:06:24

Am I doing something really obvious wrong?

ikitommi10:06:41

@mitchelkuijpers was about to say that “it should just work”, but actually - there are no coercers from keyword->* in Schema.

ikitommi10:06:16

we seem to have extansions to those in our projects, should move those into schema-tools (or Schema itself)

ikitommi10:06:23

just added those to spec-tools while ago.

mitchelkuijpers10:06:53

Ah ok, so that is the problem

mitchelkuijpers10:06:05

That makes sense

mitchelkuijpers10:06:46

I am happy to just add schema-tools

ikitommi10:06:12

in the current project I’m working on, there are these:

(defn cond-matcher [preds]
  (fn [x]
    (or
      (some
        (fn [[f1 f2]]
          (if (f1 x) (f2 x)))
        preds)
      x)))

(def keyword->int
  {s/Int (cond-matcher
           {string? stc/string->long
            number? schema.coerce/safe-long-cast
            keyword? (fn [x]
                       (try
                         (Long/valueOf (name x))
                         (catch Exception _ x)))})})

(def keyword->string
  {String (cond-matcher {keyword? name})})

ikitommi10:06:39

just for the types we have needed, should be polished and pushed out. Interested in doing a PR?

kommen10:06:01

with reitit.frontend.history, is there any way I am missing to let it ignore clicks on certain a elements? e.g we have links which trigger intercom, so this is handled by a 3rd party js, and we want reitit to not perform navigation on those. I see https://github.com/metosin/reitit/blob/master/modules/reitit-frontend/src/reitit/frontend/history.cljs#L84 but it looks like this can not be easily customized?

mitchelkuijpers10:06:13

It seems it will ignore external urls bij default?

kommen11:06:59

yes, but I prefer to not have to add artificial urls

mitchelkuijpers11:06:49

I think it will also only do something if it matches a route

ikitommi10:06:36

I’m quite sure @juhoteperi knows.

👍 1
mitchelkuijpers10:06:51

Sure you want a PR on reitit?

ikitommi10:06:02

(Juho is the lead-dev for the frontend-stuff)

ikitommi10:06:24

I think it would be better to be in the schema-tools.

mitchelkuijpers10:06:16

I'll give it a go

ikitommi10:06:59

awesome! that’s all the code we had in the project I pasted, so all yours 🙂

mitchelkuijpers10:06:00

Sorry quick question should i add something along the lines of:

(def +keyword-coercions+
  {s/Int (comp safe-int keyword->number)
   s/Num keyword->number
   s/Bool keyword->boolean
   #[email protected](:clj [Long (comp safe-int string->long)])
   #[email protected](:clj [Double (comp double string->double)])})
There are already +string-coercions+ and +json-coercions+

ikitommi10:06:59

I would change the current coercions so that they coerce from both strings & keywords, in a single sweep.

ikitommi10:06:25

e.g. {s/Int from-string-or-keyword->number} style

mitchelkuijpers11:06:33

How should I format the code?

ikitommi12:06:29

that was fast 🙂 added few comments.

mitchelkuijpers12:06:40

Nice, I have two questions though, do you rather have them on github or here?

mitchelkuijpers13:06:07

I'll do it on github is better for visibility

mitchelkuijpers13:06:10

For the json coercions would it be ok if they are the same as for the string-coercions? So that I re-use the string->x formats? Or do you want a stricter version that only does keyword->x and does not support strings?

ikitommi13:06:50

json should not do conversions from strings to types that it can present itself. So no str->nbr for example.

mitchelkuijpers13:06:30

Ok so str->number is not ok, but keyword->number is fine

mitchelkuijpers13:06:32

I think I get it

mitchelkuijpers13:06:35

Fixed the PR comments

ikitommi18:06:43

merged the PR and added support for all the types: https://github.com/metosin/schema-tools/pull/57

ikitommi19:06:36

(stc/coerce
  {:1 {:true "1", :false "2"}}
  {s/Int {s/Bool s/Any}}
  stc/json-coercion-matcher)
; {1 {true "1", false "2"}}

y.khmelevskii20:06:53

Hi all! Can you please explain why this code doesn’t work as expected:

(def routes
  ["/users/:id"
   {:get {:name ::get-users
          :parameters {:path {:id ::id}}}}])

(def router
  (r/router routes))

(r/route-names router) ;; => []
route-names return empty vector but should return [::get-users]

y.khmelevskii20:06:48

I see, thank you!