Fork me on GitHub
#reitit
<
2019-11-28
>
sudakatux12:11:29

So continuing with coersions problems. Im going through an example in the book "Web Development with Clojure" that seems to fail with a coersion error as well. The thing is it looks good to me so i dont understand where the error is. Im asuming i need to be specific and spec the params as a map-of but i tried that and it did not work for me

sudakatux12:11:54

{:post {:parameters
            {:body
             {:login string?
              :password string?}}
            :responses
            {200
             {:body
              {:identity
               {:login string?
                :created_at inst?}}}
             401
             {:body
              {:message string?}}}
            :handler
            (fn [{{{:keys [login password]} :body} :parameters
                                               session :session}]
              (if-some [user (auth/authenticate-user login password)]
                (->
                  (response/ok
                    {:identity user})
                  (assoc :session (assoc session
                                    :identity
                                    user)))

sudakatux12:11:16

So its my understanding from what i read here https://metosin.github.io/reitit/coercion/clojure_spec_coercion.html that simple structure should just work. meaning :identity {:login :created_at} should just work

sudakatux12:11:27

but im getting the following

sudakatux12:11:02

{
  "spec": "(spec-tools.core/spec {:spec (clojure.spec.alpha/keys :req-un [:spec$39136/identity]), :type :map, :leaf? false})",
  "problems": [],
  "type": "reitit.coercion/response-coercion",
  "coercion": "spec",
  "value": {
    "identity": {
      "login": "testuser",
      "created_at": "2019-11-28T08:55:33.9774"
    }
  },
  "in": [
    "response",
    "body"
  ]
}

sudakatux12:11:22

if im not mistaken that means it cant tell identity is a map right?

sudakatux13:11:51

Ha sorry. figured it out

sudakatux13:11:11

instead of map-of i used s/keys and defined the keys outside

sudakatux13:11:17

And it worked

sudakatux13:11:31

Sorry if i spamed the chat 😛

ikitommi16:11:28

@vale would it help to define a print-method for Match?

valerauko02:11:26

i don't know how that works. could you elaborate please?

ikitommi18:11:01

@vale you can override how things get printed extending the print-method multimethod, example here: https://github.com/metosin/malli/blob/master/src/malli/core.cljc#L29-L30

valerauko10:12:47

awesome, thanks!

ikitommi16:11:39

@jstuartmilne glad you got it sorted out. Where is the example of "web development with Clojure"?

sudakatux17:11:49

So the code pasted above comes from the book

sudakatux17:11:20

Planning Our Application

sudakatux17:11:23

The way i solved it was defining the keys outside

sudakatux17:11:27

(s/def ::login string?)
(s/def ::created_at inst?)

sudakatux17:11:49

and changing this part

sudakatux17:11:54

{200
             {:body
              {:identity
               (s/keys ::login ::created_at)}}

sudakatux17:11:24

Great book BTW really enjoying it

👍 4
ikitommi17:11:38

Still on beta, right?