-
Notifications
You must be signed in to change notification settings - Fork 1
/
coerce.cljc
42 lines (38 loc) · 1.6 KB
/
coerce.cljc
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
(ns paprika.schemas.coerce
(:require [schema.core :as s]
[schema.coerce :as coerce]
[schema.utils :as utils]))
; Functions got from here: https://gist.github.com/abp/0c4106eba7b72802347b
; following this thread: https://groups.google.com/forum/#!topic/prismatic-plumbing/SaOBraHzoHE
; and this one: https://stackoverflow.com/questions/31587590/prismatic-schema-removing-unanticipated-keys
(defn filter-schema-keys [m schema-keys extra-keys-walker]
(reduce-kv (fn [m k v]
(if (or (contains? schema-keys k)
(and extra-keys-walker
(not (utils/error? (extra-keys-walker k)))))
m
(dissoc m k)))
m
m))
(defn map-filter-matcher [s]
(when (and (not (record? s)) (map? s))
(let [extra-keys-schema (s/find-extra-keys-schema s)
extra-keys-walker (when extra-keys-schema (s/checker extra-keys-schema))
explicit-keys (some->> (dissoc s extra-keys-schema)
keys
(mapv s/explicit-schema-key)
(into #{}))]
(when (or extra-keys-walker (seq explicit-keys))
(fn [x]
(if (map? x)
(filter-schema-keys x explicit-keys extra-keys-walker)
x))))))
(defn strict-coercer [coercions]
(fn [schema]
(or (coercions schema)
(coerce/keyword-enum-matcher schema))))
(defn loose-coercer [coercions]
(let [strict (strict-coercer coercions)]
(fn [schema]
(or (map-filter-matcher schema)
(strict schema)))))