forked from dmotz/natal
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch schema for cljs.spec - close #60
- Loading branch information
Showing
6 changed files
with
38 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters