Skip to content

Commit

Permalink
Switch schema for cljs.spec - close #60
Browse files Browse the repository at this point in the history
  • Loading branch information
cldwalker committed Jul 24, 2016
1 parent f63d563 commit 3f9323b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 34 deletions.
6 changes: 2 additions & 4 deletions re-natal.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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!"])'
Expand All @@ -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!"])'
Expand Down
8 changes: 5 additions & 3 deletions resources/cljs-reagent/db.cljs
Original file line number Diff line number Diff line change
@@ -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!"})
23 changes: 12 additions & 11 deletions resources/cljs-reagent/handlers.cljs
Original file line number Diff line number Diff line change
@@ -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)))
8 changes: 5 additions & 3 deletions resources/cljs-reagent6/db.cljs
Original file line number Diff line number Diff line change
@@ -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!"})
23 changes: 12 additions & 11 deletions resources/cljs-reagent6/handlers.cljs
Original file line number Diff line number Diff line change
@@ -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)))
4 changes: 2 additions & 2 deletions resources/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]
Expand Down

0 comments on commit 3f9323b

Please sign in to comment.