-
Notifications
You must be signed in to change notification settings - Fork 1
/
validation.cljc
62 lines (56 loc) · 1.73 KB
/
validation.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(ns loicb.common.validation
(:require [malli.core :as m]
[malli.util :as mu]
[tick.core :as t]
[tick.alpha.interval :as t.i]))
(defn date-valid?
"Returns true if the given the given coll of `dates` is valid."
[[date1 date2]]
(try
(if date2
(= :precedes (t.i/relation (t/date date1)
(t/date date2)))
(t/date? (t/date date1)))
(catch #?(:clj Exception :cljs js/Error) _ false)))
;;---------- Validation Schemas ----------
(def post-schema
[:map {:closed true}
[:post/id :string]
[:post/page :keyword]
[:post/title :string]
[:post/home-page? {:optional true} :boolean]
[:post/date [:and
[:vector :string]
[:fn #(date-valid? %)]]]
[:post/employer {:optional true} :string]
[:post/css-class {:optional true} :string]
[:post/md-content :string]
[:post/md-content-short :string]
[:post/tags {:optional true} [:vector :string]]
[:post/repos {:optional true} [:vector [:vector :string]]]
[:post/articles {:optional true} [:vector [:vector :string]]]
[:post/image
{:optional true}
[:map
[:image/src :string]
[:image/src-dark :string]
[:image/alt :string]]]])
(defn all-keys-optional
"Walk through the given `schema` and set all keys to optional."
[schema]
(m/walk
schema
(m/schema-walker
(fn [sch]
(if (= :map (m/type sch))
(mu/optional-keys sch)
sch)))))
(defn validate
"Validates the given `data` against the given `schema`.
If the validation passes, returns the data.
Else, returns the error data."
[data schema]
(let [validator (m/validator schema)]
(if (validator data)
data
(mu/explain-data schema data))))