0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-23 15:09:10 -05:00
penpot/frontend/uxbox/schema.cljs

39 lines
1.1 KiB
Text
Raw Normal View History

2015-12-14 14:17:18 +02:00
(ns uxbox.schema
2015-12-14 20:31:21 +02:00
(:refer-clojure :exclude [keyword uuid])
2015-12-14 14:17:18 +02:00
(:require [bouncer.core :as b]
[bouncer.validators :as v]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Validators
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2015-12-14 20:31:21 +02:00
(v/defvalidator keyword
2015-12-14 14:17:18 +02: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 20:31:21 +02:00
(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))
2015-12-14 14:17:18 +02: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)))))