forked from metosin/ring-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.clj
45 lines (38 loc) · 1.53 KB
/
schema.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(ns ring.swagger.schema
(:require [schema.core :as s]
[schema.coerce :as sc]
[schema.utils :as su]
[potemkin]
[ring.swagger.common :as common]
[ring.swagger.coerce :as coerce]
ring.swagger.json-schema))
(potemkin/import-vars [ring.swagger.json-schema
field
describe])
(defn error?
"Checks whether input is an Schema error."
[x] (su/error? x))
(defn named-schema?
"Checks whether input is a named schema."
[x] (boolean (s/schema-name x)))
(defn coerce
"Coerces a value against a schema using a given coerser. If no errors,
returns the coerced value, otherwise returns ValidationError.
Optional third parameter is a dispatch value to ring.swagger.coerce/coercer,
defaults to :json"
([schema value] (coerce schema value :json))
([schema value type]
(let [coercer (if (keyword? type) (coerce/coercer type) type)]
((sc/coercer (common/value-of schema) coercer) value))))
(defn coerce!
"Coerces a value against a schema using a given coerser. If no errors,
returns the coerced value, otherwise throws a schema.utils.ErrorContainer
enriched with :type ::validation.
Optional third parameter is a dispatch value to ring.swagger.coerce/coercer,
defaults to :json"
([schema value] (coerce! schema value :json))
([schema value type]
(let [result (coerce schema value type)]
(if (error? result)
(throw (ex-info (str "Validation failed") (assoc result :type ::validation)))
result))))