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

View File

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

View File

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

View File

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

View File

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

View File

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