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-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"]

View file

@ -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

View file

@ -6,8 +6,7 @@
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
(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]

View file

@ -6,8 +6,7 @@
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
(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]

View file

@ -6,89 +6,86 @@
;; Copyright (c) 2015-2016 Juan de la Cruz <delacruzgarciajuan@gmail.com>
(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 {}))))))