Fork me on GitHub
#ring-swagger
<
2017-08-10
>
henrik08:08:41

I’m getting an error with spec conforming my input. When I extract the input and conform it manually, the spec reports no errors. When conformed through the API, I get errors. This is the spec:

(s/def ::input-settings (s/and (s/keys :req-un [::endpoint
                                                ::customer-id
                                                ::requestor-id]
                                       :opt-un [::from-year
                                                ::from-month
                                                ::to-year
                                                ::to-month
                                                ::requestor-email
                                                ::requestor-name])
                               date/validate-to-date
                               date/validate-from-date
                               date/validate-both-dates))
The keys are spec’d to be strings or ints. The “validate” functions all return true when the optional keys are not present. This is the input, capture before validation:
{"endpoint" "",
 "customer-id" "abc",
 "requestor-id" "abc",
 :customer-id "abc",
 :endpoint "",
 :requestor-id "abc"}

henrik08:08:47

This is the error:

{
  "spec": "(spec-tools.core/spec {:spec (clojure.spec.alpha/and (clojure.spec.alpha/keys :req-un [:futomaki.api.handler/endpoint :futomaki.api.handler/customer-id :futomaki.api.handler/requestor-id] :opt-un [:futomaki.api.handler/from-year :futomaki.api.handler/from-month :futomaki.api.handler/to-year :futomaki.api.handler/to-month :futomaki.api.handler/requestor-email :futomaki.api.handler/requestor-name]) futomaki.date/validate-to-date futomaki.date/validate-from-date futomaki.date/validate-both-dates), :type :map})",
  "problems": [
    {
      "path": [],
      "pred": [
        "clojure.spec.alpha/and",
        [
          "clojure.spec.alpha/keys",
          "req-un",
          [
            "futomaki.api.handler/endpoint",
            "futomaki.api.handler/customer-id",
            "futomaki.api.handler/requestor-id"
          ],
          "opt-un",
          [
            "futomaki.api.handler/from-year",
            "futomaki.api.handler/from-month",
            "futomaki.api.handler/to-year",
            "futomaki.api.handler/to-month",
            "futomaki.api.handler/requestor-email",
            "futomaki.api.handler/requestor-name"
          ]
        ],
        "futomaki.date/validate-to-date",
        "futomaki.date/validate-from-date",
        "futomaki.date/validate-both-dates"
      ],
      "val": {
        "endpoint": "",
        "customer-id": "abc",
        "requestor-id": "abc"
      },
      "via": [
        "futomaki.api.handler/input-settings"
      ],
      "in": []
    }
  ],
  "type": "compojure.api.exception/request-validation",
  "coercion": "spec",
  "value": {
    "endpoint": "",
    "customer-id": "abc",
    "requestor-id": "abc"
  },
  "in": [
    "request",
    "query-params"
  ]
}
`

henrik08:08:13

It’s not parsing it further down than s/and, so I guess it figures that the entire form is false? But how come it conforms when I run it manually?

henrik08:08:25

So, this spec also fails:

(s/def ::input-settings (s/and (s/keys :req-un [::endpoint
                                                ::customer-id
                                                ::requestor-id]
                                       :opt-un [::from-year
                                                ::from-month
                                                ::to-year
                                                ::to-month
                                                ::requestor-email
                                                ::requestor-name])))
This spec works:
(s/def ::input-settings (s/keys :req-un [::endpoint
                                         ::customer-id
                                         ::requestor-id]
                                :opt-un [::from-year
                                         ::from-month
                                         ::to-year
                                         ::to-month
                                         ::requestor-email
                                         ::requestor-name]))

ikitommi09:08:32

@henrik Sounds odd, have to dig into that. Could you write an issue to Github? might have time later today.

lepistane14:08:19

how does one make

lepistane14:08:54

but body doesnt follow fixed schema

juhoteperi14:08:23

Or no :body or :body-params

juhoteperi14:08:38

empty :body-params says that the endpoint doesn't take any params

lepistane14:08:33

i understand

lepistane14:08:06

Would you be kind and walk me through what i am doing wrong? I am having trouble doing basic thing in Clj frontend is Q1 response1 (input radio button) response2 ... i want to send checked responses id (which is value of input, 1 response per question) to backend and i am failing