Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clojure.specを活用したコード・ファーストスタイルのRESTサービス開発

 Clojure.specを活用したコード・ファーストスタイルのRESTサービス開発

(増席) 教養としてのClojure - そろそろ学んでおきたい2017年秋
https://d-cube.connpass.com/event/67567/presentation/

Kenji Nakamura

October 05, 2017
Tweet

Other Decks in Technology

Transcript

  1. フレームワーク フレームワーク を定義する スペックに準拠し、設計、 ドキュメントなどをサポートするツール群 データ構造を定義し、バリデーションなどを行うラ イブラリ に や ス

    キーマ、 サポート機能を付加するライブラリ サーバーを起動することなく 上のロジックをテスト することができるライブラリ
  2. (defproject compojure-api-with-spec "0.1.0-SNAPSHOT" :description "compojure-api v2 and clojure.spec demo" :url

    "https://github.com/k2n/compojure-api-with-spec" :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :dependencies [[clj-time "0.14.0"] [metosin/compojure-api "2.0.0-alpha7"] [metosin/spec-tools "0.3.2"] [org.clojure/clojure "1.9.0-beta1"]] :ring {:handler compojure-api-with-spec.handler/app :async? true :nrepl {:start? true}} :uberjar-name "server.jar" :profiles {:dev {:dependencies [[peridot "0.5.0"]] :plugins [[lein-ring "0.12.0"] [com.jakemccrary/lein-test-refresh "0.21.1"]] :global-vars {*warn-on-reflection* true}}})
  3. (ns compojure-api-with-spec.handler (:require [compojure.api.sweet :refer [api]] [compojure-api-with-spec [spec-coercion-routes :refer [spec-coercion-routes]]

    [spec-validation-routes :refer [spec-validation-routes]]])) (def app (api {:swagger {:ui "/" :spec "/swagger.json" :data {:info {:title "Clojureでコード・ファーストなSwagger駆動REST API"} :tags [{:name "validation" :description "clojure.specによるバリデーション"} {:name "coercion" :description "clojure.specでコアーション"}]}}} spec-validation-routes spec-coercion-routes))
  4. (ns compojure-api-with-spec.spec-validation-routes (:require [compojure.api.sweet :refer [context GET POST]] [ring.util.http-response :refer

    [ok]] [clojure.spec.alpha :as s] [spec-tools.spec :as spec])) (def email-regex #"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$") (def max-age 140) (s/def ::greeting (assoc spec/string? :description "このように注釈を定義することができる")) (s/def ::comment (assoc spec/string? :description "年齢の偶奇数を判定します")) (s/def ::response (s/keys :req-un [::greeting ::comment])) (s/def ::email (s/and spec/string? #(re-matches email-regex %))) (s/def ::age (s/and spec/int? #(> max-age %) #((complement neg?) %))) (defn validation-handler [email-address current-age] {:pre [(s/valid? ::email email-address) (s/valid? ::age current-age)] :post [(s/valid? ::response %)]} {:greeting (str email-address "を登録しました!") :comment (if (even? current-age) "あなたの年齢は偶数です"
  5. (ns compojure-api-with-spec.spec-coercion-routes (:require [compojure.api.sweet :refer [context GET POST]] [compojure.api.coercion.spec :as

    spec-coercion] [ring.util.http-response :refer [ok]] [clojure.spec.alpha :as s] [spec-tools.conform :as conform] [spec-tools.core :as st] [spec-tools.spec :as spec])) (s/def ::name spec/string?) (s/def ::amount (assoc spec/bigdec? :json-schema/type "string" :description "整数または小数を文字列として入力")) (s/def ::amount-type spec/string?) (defn string->bigdec [_ x] (cond (bigdec? x) x (string? x) (try (java.math.BigDecimal. x) (catch NumberFormatException e ::s/invalid)) :else ::s/invalid)) (def string-conforming (st/type-conforming
  6. (ns compojure-api-with-spec.spec-routes-test (:require [clojure.test :refer :all] [clojure.edn :as edn] [cheshire.core

    :as json] [compojure-api-with-spec.handler :refer [app]] [peridot.core :refer :all])) (deftest spec-routes-test (testing "validation" (testing "get" (testing "valid email address" (is (= 200 (-> (session app) (request "/[email protected]") :response :status)))) (testing "email address without domain" (is (= 400 (-> (session app) (request "/validation?email-address=foo") :response :status)))) (testing "email address without TLD" (is (= 400 (-> (session app) (request "/validation?email-address=foo@example") :response :status)))) (testing "email address with 1 character TLD, which is invalid."