Replace bouncer with funcool/struct.

This commit is contained in:
Andrey Antukh 2016-04-11 22:29:16 +03:00
parent cc320215ac
commit 3d79367078
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
5 changed files with 73 additions and 79 deletions

View file

@ -20,13 +20,13 @@
[cljsjs/react "15.0.0-rc.2-0"] [cljsjs/react "15.0.0-rc.2-0"]
[cljsjs/react-dom "15.0.0-rc.2-0"] [cljsjs/react-dom "15.0.0-rc.2-0"]
[cljsjs/moment "2.10.6-3"] [cljsjs/moment "2.10.6-3"]
[funcool/struct "0.1.0-SNAPSHOT"]
[funcool/lentes "1.0.1"] [funcool/lentes "1.0.1"]
[funcool/httpurr "0.6.0-SNAPSHOT"] [funcool/httpurr "0.6.0-SNAPSHOT"]
[funcool/promesa "1.1.1"] [funcool/promesa "1.1.1"]
[funcool/beicon "1.1.1"] [funcool/beicon "1.1.1"]
[funcool/cuerdas "0.7.1"] [funcool/cuerdas "0.7.1"]
[funcool/hodgepodge "0.1.4"] [funcool/hodgepodge "0.1.4"]
[bouncer "1.0.0"]
[bidi "2.0.4"]] [bidi "2.0.4"]]
:plugins [[lein-ancient "0.6.7"]] :plugins [[lein-ancient "0.6.7"]]
:clean-targets ^{:protect false} ["resources/public/js" "target"] :clean-targets ^{:protect false} ["resources/public/js" "target"]

View file

@ -10,21 +10,20 @@
[uxbox.router :as r] [uxbox.router :as r]
[uxbox.state :as st] [uxbox.state :as st]
[uxbox.schema :as sc] [uxbox.schema :as sc]
[uxbox.repo :as rp] [uxbox.repo :as rp]))
[bouncer.validators :as v]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Schemas ;; Schemas
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(def ^:static +color-replace-schema+ (def ^:static +color-replace-schema+
{:id [v/required sc/uuid] {:id [sc/required sc/uuid]
:from [sc/color] :from [sc/color]
:to [v/required sc/color]}) :to [sc/required sc/color]})
(def ^:static +remove-color-schema+ (def ^:static +remove-color-schema+
{:id [v/required sc/uuid] {:id [sc/required sc/uuid]
:color [v/required sc/color]}) :color [sc/required sc/color]})
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Helpers ;; Helpers

View file

@ -6,8 +6,7 @@
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com> ;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
(ns uxbox.data.shapes (ns uxbox.data.shapes
(:require [bouncer.validators :as v] (:require [beicon.core :as rx]
[beicon.core :as rx]
[uxbox.shapes :as sh] [uxbox.shapes :as sh]
[uxbox.rstore :as rs] [uxbox.rstore :as rs]
[uxbox.router :as r] [uxbox.router :as r]

View file

@ -6,8 +6,7 @@
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com> ;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
(ns uxbox.data.workspace (ns uxbox.data.workspace
(:require [bouncer.validators :as v] (:require [beicon.core :as rx]
[beicon.core :as rx]
[uxbox.constants :as c] [uxbox.constants :as c]
[uxbox.shapes :as sh] [uxbox.shapes :as sh]
[uxbox.rstore :as rs] [uxbox.rstore :as rs]

View file

@ -6,89 +6,86 @@
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com> ;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
(ns uxbox.schema (ns uxbox.schema
(:refer-clojure :exclude [keyword uuid vector boolean]) (:refer-clojure :exclude [keyword uuid vector boolean map set])
(:require [bouncer.core :as b] (:require [struct.core :as st]
[bouncer.validators :as v] [uxbox.locales :refer (tr)]
[cuerdas.core :as str]
[uxbox.shapes :refer (shape?)])) [uxbox.shapes :refer (shape?)]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; (def datetime
;; Validators ;; {:message "must be an instant"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; :optional true
;; :validate #(instance? Instant %)})
(v/defvalidator keyword (def required
"Validates maybe-an-int is a valid integer. (assoc st/required :message "errors.form.required"))
For use with validation functions such as `validate` or `valid?`"
{:default-message-format "%s must be a keyword"}
[v]
(cljs.core/keyword? v))
(v/defvalidator uuid (def string
"Validates maybe-an-int is a valid integer. (assoc st/string :message "errors.form.string"))
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))
(v/defvalidator color (def number
"Validates if a string is a valid color." (assoc st/number :message "errors.form.number"))
{:default-message-format "%s must be a valid hex color"}
[v]
(not (nil? (re-find #"^#[0-9A-Fa-f]{6}$" v))))
(v/defvalidator shape-type (def integer
"Validates if a keyword is a shape type." (assoc st/integer :message "errors.form.integer"))
{:default-message-format "%s must be a shape type keyword."}
[v]
(shape? v))
(v/defvalidator vector (def boolean
"Validats if `v` is vector." (assoc st/boolean :message "errors.form.bool"))
{:default-message-format "%s must be a vector instance."}
[v]
(vector? v))
(v/defvalidator function (def identical-to
"Validats if `v` is function." (assoc st/identical-to :message "errors.form.identical-to"))
{:default-message-format "%s must be a function."}
[v]
(fn? v))
(def ^:const +email-re+ ;; (def in-range st/in-range)
#"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$") ;; (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 (def max-len
"Validate if `v` is a valid email." {:message "errors.form.max-len"
{:default-message-format "% must be a valid email."} :optional true
[v] :validate (fn [v n]
(clojure.core/boolean (re-seq +email-re+ v))) (let [len (count v)]
(>= len v)))})
(def required v/required) (def min-len
(def number v/number) {:message "errors.form.min-len"
(def integer v/integer) :optional true
(def boolean v/boolean) :validate (fn [v n]
(def string v/string) (>= (count v) n))})
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (def color
;; Public Api {: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 (defn validate
([schema] #(validate schema %)) [data schema]
([schema data] (first (b/validate data schema)))) (let [opts {:strip true
:translate tr}]
(st/validate data schema opts)))
(defn validate! (defn validate!
([schema] #(validate! schema %)) [data schema]
([schema data] (when-let [errors (first (validate schema data))]
(when-let [errors (validate schema data)] (throw (ex-info "Invalid data" errors))))
(throw (ex-info "Invalid data" errors)))))
(defn valid? ;; (defn valid?
[validator data] ;; [validator data]
(let [result (validator data)] ;; (let [result (validator data)]
(if result ;; (if result
result ;; result
(let [message (:default-message-format (meta validator)) ;; (let [message (:default-message-format (meta validator))
message (str/format message data)] ;; message (str/format message data)]
(throw (ex-info message {})))))) ;; (throw (ex-info message {}))))))