From 3d7936707890a5faee702a1fdf04013481497b47 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Mon, 11 Apr 2016 22:29:16 +0300 Subject: [PATCH] Replace bouncer with funcool/struct. --- project.clj | 2 +- src/uxbox/data/dashboard.cljs | 11 ++- src/uxbox/data/shapes.cljs | 3 +- src/uxbox/data/workspace.cljs | 3 +- src/uxbox/schema.cljs | 133 +++++++++++++++++----------------- 5 files changed, 73 insertions(+), 79 deletions(-) diff --git a/project.clj b/project.clj index 28cc3c441..6151364f5 100644 --- a/project.clj +++ b/project.clj @@ -20,13 +20,13 @@ [cljsjs/react "15.0.0-rc.2-0"] [cljsjs/react-dom "15.0.0-rc.2-0"] [cljsjs/moment "2.10.6-3"] + [funcool/struct "0.1.0-SNAPSHOT"] [funcool/lentes "1.0.1"] [funcool/httpurr "0.6.0-SNAPSHOT"] [funcool/promesa "1.1.1"] [funcool/beicon "1.1.1"] [funcool/cuerdas "0.7.1"] [funcool/hodgepodge "0.1.4"] - [bouncer "1.0.0"] [bidi "2.0.4"]] :plugins [[lein-ancient "0.6.7"]] :clean-targets ^{:protect false} ["resources/public/js" "target"] diff --git a/src/uxbox/data/dashboard.cljs b/src/uxbox/data/dashboard.cljs index 72acd89a5..6ca4034c3 100644 --- a/src/uxbox/data/dashboard.cljs +++ b/src/uxbox/data/dashboard.cljs @@ -10,21 +10,20 @@ [uxbox.router :as r] [uxbox.state :as st] [uxbox.schema :as sc] - [uxbox.repo :as rp] - [bouncer.validators :as v])) + [uxbox.repo :as rp])) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Schemas ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (def ^:static +color-replace-schema+ - {:id [v/required sc/uuid] + {:id [sc/required sc/uuid] :from [sc/color] - :to [v/required sc/color]}) + :to [sc/required sc/color]}) (def ^:static +remove-color-schema+ - {:id [v/required sc/uuid] - :color [v/required sc/color]}) + {:id [sc/required sc/uuid] + :color [sc/required sc/color]}) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Helpers diff --git a/src/uxbox/data/shapes.cljs b/src/uxbox/data/shapes.cljs index d59429aa0..24a522cb5 100644 --- a/src/uxbox/data/shapes.cljs +++ b/src/uxbox/data/shapes.cljs @@ -6,8 +6,7 @@ ;; Copyright (c) 2015-2016 Juan de la Cruz (ns uxbox.data.shapes - (:require [bouncer.validators :as v] - [beicon.core :as rx] + (:require [beicon.core :as rx] [uxbox.shapes :as sh] [uxbox.rstore :as rs] [uxbox.router :as r] diff --git a/src/uxbox/data/workspace.cljs b/src/uxbox/data/workspace.cljs index b4ed05720..bfcce6a65 100644 --- a/src/uxbox/data/workspace.cljs +++ b/src/uxbox/data/workspace.cljs @@ -6,8 +6,7 @@ ;; Copyright (c) 2015-2016 Juan de la Cruz (ns uxbox.data.workspace - (:require [bouncer.validators :as v] - [beicon.core :as rx] + (:require [beicon.core :as rx] [uxbox.constants :as c] [uxbox.shapes :as sh] [uxbox.rstore :as rs] diff --git a/src/uxbox/schema.cljs b/src/uxbox/schema.cljs index 14c7da155..ab2195d53 100644 --- a/src/uxbox/schema.cljs +++ b/src/uxbox/schema.cljs @@ -6,89 +6,86 @@ ;; Copyright (c) 2015-2016 Juan de la Cruz (ns uxbox.schema - (:refer-clojure :exclude [keyword uuid vector boolean]) - (:require [bouncer.core :as b] - [bouncer.validators :as v] - [cuerdas.core :as str] + (:refer-clojure :exclude [keyword uuid vector boolean map set]) + (:require [struct.core :as st] + [uxbox.locales :refer (tr)] [uxbox.shapes :refer (shape?)])) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Validators -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; (def datetime +;; {:message "must be an instant" +;; :optional true +;; :validate #(instance? Instant %)}) -(v/defvalidator keyword - "Validates maybe-an-int is a valid integer. - For use with validation functions such as `validate` or `valid?`" - {:default-message-format "%s must be a keyword"} - [v] - (cljs.core/keyword? v)) +(def required + (assoc st/required :message "errors.form.required")) -(v/defvalidator uuid - "Validates maybe-an-int is a valid integer. - For use with validation functions such as `validate` or `valid?`" - {:default-message-format "%s must be a uuid instance"} - [v] - (instance? cljs.core.UUID v)) +(def string + (assoc st/string :message "errors.form.string")) -(v/defvalidator color - "Validates if a string is a valid color." - {:default-message-format "%s must be a valid hex color"} - [v] - (not (nil? (re-find #"^#[0-9A-Fa-f]{6}$" v)))) +(def number + (assoc st/number :message "errors.form.number")) -(v/defvalidator shape-type - "Validates if a keyword is a shape type." - {:default-message-format "%s must be a shape type keyword."} - [v] - (shape? v)) +(def integer + (assoc st/integer :message "errors.form.integer")) -(v/defvalidator vector - "Validats if `v` is vector." - {:default-message-format "%s must be a vector instance."} - [v] - (vector? v)) +(def boolean + (assoc st/boolean :message "errors.form.bool")) -(v/defvalidator function - "Validats if `v` is function." - {:default-message-format "%s must be a function."} - [v] - (fn? v)) +(def identical-to + (assoc st/identical-to :message "errors.form.identical-to")) -(def ^:const +email-re+ - #"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$") +;; (def in-range st/in-range) +;; (def uuid-like st/uuid-like) +(def uuid st/uuid) +(def keyword st/keyword) +;; (def integer-like st/integer-like) +;; (def boolean-like st/boolean-like) +;; (def email st/email) +;; (def function st/function) +;; (def positive st/positive) +;; (def validate st/validate) +;; (def validate! st/validate!) -(v/defvalidator email - "Validate if `v` is a valid email." - {:default-message-format "% must be a valid email."} - [v] - (clojure.core/boolean (re-seq +email-re+ v))) +(def max-len + {:message "errors.form.max-len" + :optional true + :validate (fn [v n] + (let [len (count v)] + (>= len v)))}) -(def required v/required) -(def number v/number) -(def integer v/integer) -(def boolean v/boolean) -(def string v/string) +(def min-len + {:message "errors.form.min-len" + :optional true + :validate (fn [v n] + (>= (count v) n))}) -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -;; Public Api -;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(def color + {:message "errors.form.color" + :optional true + :validate #(not (nil? (re-find #"^#[0-9A-Fa-f]{6}$" %)))}) + +(def shape-type + {:message "should be shape" + :optional true + :validate #(shape? %)}) (defn validate - ([schema] #(validate schema %)) - ([schema data] (first (b/validate data schema)))) + [data schema] + (let [opts {:strip true + :translate tr}] + (st/validate data schema opts))) (defn validate! - ([schema] #(validate! schema %)) - ([schema data] - (when-let [errors (validate schema data)] - (throw (ex-info "Invalid data" errors))))) + [data schema] + (when-let [errors (first (validate schema data))] + (throw (ex-info "Invalid data" errors)))) -(defn valid? - [validator data] - (let [result (validator data)] - (if result - result - (let [message (:default-message-format (meta validator)) - message (str/format message data)] - (throw (ex-info message {})))))) +;; (defn valid? +;; [validator data] +;; (let [result (validator data)] +;; (if result +;; result +;; (let [message (:default-message-format (meta validator)) +;; message (str/format message data)] +;; (throw (ex-info message {}))))))