我有基于 compojure 的应用程序,我需要在其中解析请求并检索可以是数字的参数。我希望能够在实际处理请求之前验证参数是否存在并且它们是数字。这是我到目前为止所拥有的:
(defn get-int [str]
"Returns nil if str is not a number"
(try (Integer/parseInt str)
(catch NumberFormatException _)))
(defn some-request [request]
(let [some-number (get-int (get-in request [:route-params :some-number])
other-number (get-int (get-in request [:route-params :other-number])]
(if (every? identity [some-number other-number])
(process-the-request)
(bad-request "The request was malformed")))
有没有更好的方法来进行字符串 -> 数字转换?
有没有更好的方法来进行请求验证?