0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-09 08:20:45 -05:00
penpot/frontend/uxbox/schema.cljs

58 lines
1.7 KiB
Text
Raw Normal View History

2015-12-14 07:17:18 -05:00
(ns uxbox.schema
2015-12-29 16:40:01 -05:00
(:refer-clojure :exclude [keyword uuid vector])
2015-12-14 07:17:18 -05:00
(:require [bouncer.core :as b]
2015-12-29 16:40:01 -05:00
[bouncer.validators :as v]
[uxbox.shapes :refer (shape?)]))
2015-12-14 07:17:18 -05:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Validators
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2015-12-14 13:31:21 -05:00
(v/defvalidator keyword
2015-12-14 07:17:18 -05:00
"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))
2015-12-14 13:31:21 -05:00
(v/defvalidator uuid
"Validates maybe-an-int is a valid integer.
For use with validation functions such as `validate` or `valid?`"
2015-12-29 16:39:19 -05:00
{:default-message-format "%s must be a uuid instance"}
2015-12-14 13:31:21 -05:00
[v]
(instance? cljs.core.UUID v))
2015-12-24 12:33:53 -05:00
(v/defvalidator color
"Validates if a string is a valid color."
2015-12-29 16:39:19 -05:00
{:default-message-format "%s must be a valid hex color"}
2015-12-24 12:33:53 -05:00
[v]
(not (nil? (re-find #"^#[0-9A-Fa-f]{6}$" v))))
2015-12-29 16:39:19 -05:00
(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))
(v/defvalidator vector
"Validats if `v` is vector."
{:default-message-format "%s must be a vector instance."}
[v]
(vector? v))
(def required v/required)
2015-12-14 07:17:18 -05:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Public Api
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn validate
([schema] #(validate schema %))
([schema data] (first (b/validate data schema))))
(defn validate!
([schema] #(validate! schema %))
([schema data]
(when-let [errors (validate schema data)]
(throw (ex-info "Invalid data" errors)))))