Switch schema for cljs.spec - close #60

This commit is contained in:
Gabriel Horner 2016-07-25 00:06:55 +02:00
parent f63d563e82
commit 3f9323b391
6 changed files with 38 additions and 34 deletions

View File

@ -42,8 +42,7 @@ interfaceConf =
common: ["handlers.cljs", "subs.cljs", "db.cljs"] common: ["handlers.cljs", "subs.cljs", "db.cljs"]
other: [] other: []
deps: ['[reagent "0.5.1" :exclusions [cljsjs/react]]' deps: ['[reagent "0.5.1" :exclusions [cljsjs/react]]'
'[re-frame "0.6.0"]' '[re-frame "0.6.0"]']
'[prismatic/schema "1.0.4"]']
shims: ["cljsjs.react"] shims: ["cljsjs.react"]
sampleCommandNs: '(in-ns \'$PROJECT_NAME_HYPHENATED$.ios.core)' sampleCommandNs: '(in-ns \'$PROJECT_NAME_HYPHENATED$.ios.core)'
sampleCommand: '(dispatch [:set-greeting "Hello Native World!"])' sampleCommand: '(dispatch [:set-greeting "Hello Native World!"])'
@ -55,8 +54,7 @@ interfaceConf =
common: ["handlers.cljs", "subs.cljs", "db.cljs"] common: ["handlers.cljs", "subs.cljs", "db.cljs"]
other: [["reagent_dom.cljs","reagent/dom.cljs"], ["reagent_dom_server.cljs","reagent/dom/server.cljs"]] other: [["reagent_dom.cljs","reagent/dom.cljs"], ["reagent_dom_server.cljs","reagent/dom/server.cljs"]]
deps: ['[reagent "0.6.0-rc" :exclusions [cljsjs/react cljsjs/react-dom cljsjs/react-dom-server]]' deps: ['[reagent "0.6.0-rc" :exclusions [cljsjs/react cljsjs/react-dom cljsjs/react-dom-server]]'
'[re-frame "0.7.0"]' '[re-frame "0.7.0"]']
'[prismatic/schema "1.0.4"]']
shims: ["cljsjs.react", "cljsjs.react.dom", "cljsjs.react.dom.server"] shims: ["cljsjs.react", "cljsjs.react.dom", "cljsjs.react.dom.server"]
sampleCommandNs: '(in-ns \'$PROJECT_NAME_HYPHENATED$.ios.core)' sampleCommandNs: '(in-ns \'$PROJECT_NAME_HYPHENATED$.ios.core)'
sampleCommand: '(dispatch [:set-greeting "Hello Native World!"])' sampleCommand: '(dispatch [:set-greeting "Hello Native World!"])'

View File

@ -1,8 +1,10 @@
(ns $PROJECT_NAME_HYPHENATED$.db (ns $PROJECT_NAME_HYPHENATED$.db
(:require [schema.core :as s :include-macros true])) (:require [cljs.spec :as s]))
;; schema of app-db ;; spec of app-db
(def schema {:greeting s/Str}) (s/def ::greeting string?)
(s/def ::app-db
(s/keys :req-un [::greeting]))
;; initial state of app-db ;; initial state of app-db
(def app-db {:greeting "Hello Clojure in iOS and Android!"}) (def app-db {:greeting "Hello Clojure in iOS and Android!"})

View File

@ -1,34 +1,35 @@
(ns $PROJECT_NAME_HYPHENATED$.handlers (ns $PROJECT_NAME_HYPHENATED$.handlers
(:require (:require
[re-frame.core :refer [register-handler after]] [re-frame.core :refer [register-handler after]]
[schema.core :as s :include-macros true] [cljs.spec :as s]
[$PROJECT_NAME_HYPHENATED$.db :refer [app-db schema]])) [$PROJECT_NAME_HYPHENATED$.db :as db :refer [app-db]]))
;; -- Middleware ------------------------------------------------------------ ;; -- Middleware ------------------------------------------------------------
;; ;;
;; See https://github.com/Day8/re-frame/wiki/Using-Handler-Middleware ;; See https://github.com/Day8/re-frame/wiki/Using-Handler-Middleware
;; ;;
(defn check-and-throw (defn check-and-throw
"throw an exception if db doesn't match the schema." "Throw an exception if db doesn't have a valid spec."
[a-schema db] [spec db]
(when-let [problems (s/check a-schema db)] (when-not (s/valid? spec db)
(throw (js/Error. (str "schema check failed: " problems))))) (let [explain-data (s/explain-data spec db)]
(throw (ex-info (str "Spec check failed: " explain-data) explain-data)))))
(def validate-schema-mw (def validate-spec-mw
(if goog.DEBUG (if goog.DEBUG
(after (partial check-and-throw schema)) (after (partial check-and-throw ::db/app-db))
[])) []))
;; -- Handlers -------------------------------------------------------------- ;; -- Handlers --------------------------------------------------------------
(register-handler (register-handler
:initialize-db :initialize-db
validate-schema-mw validate-spec-mw
(fn [_ _] (fn [_ _]
app-db)) app-db))
(register-handler (register-handler
:set-greeting :set-greeting
validate-schema-mw validate-spec-mw
(fn [db [_ value]] (fn [db [_ value]]
(assoc db :greeting value))) (assoc db :greeting value)))

View File

@ -1,8 +1,10 @@
(ns $PROJECT_NAME_HYPHENATED$.db (ns $PROJECT_NAME_HYPHENATED$.db
(:require [schema.core :as s :include-macros true])) (:require [cljs.spec :as s]))
;; schema of app-db ;; spec of app-db
(def schema {:greeting s/Str}) (s/def ::greeting string?)
(s/def ::app-db
(s/keys :req-un [::greeting]))
;; initial state of app-db ;; initial state of app-db
(def app-db {:greeting "Hello Clojure in iOS and Android!"}) (def app-db {:greeting "Hello Clojure in iOS and Android!"})

View File

@ -1,34 +1,35 @@
(ns $PROJECT_NAME_HYPHENATED$.handlers (ns $PROJECT_NAME_HYPHENATED$.handlers
(:require (:require
[re-frame.core :refer [register-handler after]] [re-frame.core :refer [register-handler after]]
[schema.core :as s :include-macros true] [cljs.spec :as s]
[$PROJECT_NAME_HYPHENATED$.db :refer [app-db schema]])) [$PROJECT_NAME_HYPHENATED$.db :as db :refer [app-db]]))
;; -- Middleware ------------------------------------------------------------ ;; -- Middleware ------------------------------------------------------------
;; ;;
;; See https://github.com/Day8/re-frame/wiki/Using-Handler-Middleware ;; See https://github.com/Day8/re-frame/wiki/Using-Handler-Middleware
;; ;;
(defn check-and-throw (defn check-and-throw
"throw an exception if db doesn't match the schema." "Throw an exception if db doesn't have a valid spec."
[a-schema db] [spec db]
(when-let [problems (s/check a-schema db)] (when-not (s/valid? spec db)
(throw (js/Error. (str "schema check failed: " problems))))) (let [explain-data (s/explain-data spec db)]
(throw (ex-info (str "Spec check failed: " explain-data) explain-data)))))
(def validate-schema-mw (def validate-spec-mw
(if goog.DEBUG (if goog.DEBUG
(after (partial check-and-throw schema)) (after (partial check-and-throw ::db/app-db))
[])) []))
;; -- Handlers -------------------------------------------------------------- ;; -- Handlers --------------------------------------------------------------
(register-handler (register-handler
:initialize-db :initialize-db
validate-schema-mw validate-spec-mw
(fn [_ _] (fn [_ _]
app-db)) app-db))
(register-handler (register-handler
:set-greeting :set-greeting
validate-schema-mw validate-spec-mw
(fn [db [_ value]] (fn [db [_ value]]
(assoc db :greeting value))) (assoc db :greeting value)))

View File

@ -3,8 +3,8 @@
:url "http://example.com/FIXME" :url "http://example.com/FIXME"
:license {:name "Eclipse Public License" :license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"} :url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.8.0"] :dependencies [[org.clojure/clojure "1.9.0-alpha10"]
[org.clojure/clojurescript "1.8.51"] [org.clojure/clojurescript "1.9.89"]
$INTERFACE_DEPS$] $INTERFACE_DEPS$]
:plugins [[lein-cljsbuild "1.1.1"] :plugins [[lein-cljsbuild "1.1.1"]
[lein-figwheel "0.5.0-6"]] [lein-figwheel "0.5.0-6"]]