-
Notifications
You must be signed in to change notification settings - Fork 11
/
validate-yaml.cljs
63 lines (54 loc) · 2.35 KB
/
validate-yaml.cljs
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
62
63
(require '[cljs.nodejs :as nodejs])
(require '[clojure.spec.alpha :as s])
(def fs (nodejs/require "fs"))
(def js-yaml (nodejs/require "js-yaml"))
(s/def ::url (s/nilable (s/and string? #(not= "" %))))
(s/def ::email (fn [x] (or (nil? x) (and (string? x) (.includes x "@")))))
(s/def ::github (s/nilable string?))
(s/def ::image ::url)
(s/def ::name (s/and string? #(not= "" %)))
(s/def ::twitter (s/nilable string?))
(s/def ::layout #(= "post" %))
(s/def ::permalink string?)
(s/def ::country string?)
(s/def ::city string?)
(s/def ::street (s/nilable string?))
(s/def ::latitude (s/nilable (s/double-in :min -90.0 :max 90.0)))
(s/def ::longitude (s/nilable (s/double-in :min -180.0 :max 180.0)))
(s/def ::start-date #(instance? js/Date %))
(s/def ::end-date #(instance? js/Date %))
(s/def ::registration-url (s/nilable ::url))
(s/def ::city-image-url ::url)
(s/def ::gravatar-email ::email)
(s/def ::organizers (s/coll-of (s/keys :req-un [::name ::email ::github ::twitter])))
(s/def ::sponsors (s/coll-of (s/keys :req-un [::name ::url ::image])))
(s/def ::workshop-metadata (s/keys :req-un [::layout
::permalink
::country
::city
::street
::latitude
::longitude
::start-date
::end-date
::registration-url
::city-image-url
::gravatar-email
::organizers
::sponsors]))
(defn validate [k o]
(let [valid (s/valid? k o)]
(when (not valid) (s/explain k o))
valid))
(def validate-file
(comp #(validate ::workshop-metadata %)
#(js->clj % :keywordize-keys true)
#(.safeLoad js-yaml %)
second
#(clojure.string/split % "---\n")
#(.readFileSync fs % "utf8")))
(defn validate-dir [dir]
(let [files (js->clj (.readdirSync fs dir "utf8"))
valid (every? #(validate-file (str dir "/" %)) files)]
(js/process.exit (if valid 0 1))))
(validate-dir "_posts")