From 3f9323b39157b7f73c53a5ebc710b969a6ed02ef Mon Sep 17 00:00:00 2001 From: Gabriel Horner Date: Mon, 25 Jul 2016 00:06:55 +0200 Subject: [PATCH] Switch schema for cljs.spec - close #60 --- re-natal.coffee | 6 ++---- resources/cljs-reagent/db.cljs | 8 +++++--- resources/cljs-reagent/handlers.cljs | 23 ++++++++++++----------- resources/cljs-reagent6/db.cljs | 8 +++++--- resources/cljs-reagent6/handlers.cljs | 23 ++++++++++++----------- resources/project.clj | 4 ++-- 6 files changed, 38 insertions(+), 34 deletions(-) diff --git a/re-natal.coffee b/re-natal.coffee index 8df6df1..c67cc58 100644 --- a/re-natal.coffee +++ b/re-natal.coffee @@ -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!"])' diff --git a/resources/cljs-reagent/db.cljs b/resources/cljs-reagent/db.cljs index 4ca749b..45022ab 100644 --- a/resources/cljs-reagent/db.cljs +++ b/resources/cljs-reagent/db.cljs @@ -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!"}) diff --git a/resources/cljs-reagent/handlers.cljs b/resources/cljs-reagent/handlers.cljs index 9d5db58..5fdf431 100644 --- a/resources/cljs-reagent/handlers.cljs +++ b/resources/cljs-reagent/handlers.cljs @@ -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))) \ No newline at end of file + (assoc db :greeting value))) diff --git a/resources/cljs-reagent6/db.cljs b/resources/cljs-reagent6/db.cljs index 4ca749b..45022ab 100644 --- a/resources/cljs-reagent6/db.cljs +++ b/resources/cljs-reagent6/db.cljs @@ -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!"}) diff --git a/resources/cljs-reagent6/handlers.cljs b/resources/cljs-reagent6/handlers.cljs index 9d5db58..5fdf431 100644 --- a/resources/cljs-reagent6/handlers.cljs +++ b/resources/cljs-reagent6/handlers.cljs @@ -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))) \ No newline at end of file + (assoc db :greeting value))) diff --git a/resources/project.clj b/resources/project.clj index cc1c380..d5844c0 100644 --- a/resources/project.clj +++ b/resources/project.clj @@ -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"]]