-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
malli.clj
72 lines (67 loc) · 2.59 KB
/
malli.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
(ns logseq.tasks.malli
"Malli related tasks"
(:require [malli.core :as m]
[malli.error :as me]
[frontend.schema.handler.plugin-config :as plugin-config-schema]
[frontend.schema.handler.global-config :as global-config-schema]
[frontend.schema.handler.repo-config :as repo-config-schema]
[logseq.graph-parser.schema.mldoc :as mldoc-schema]
[babashka.fs :as fs]
[clojure.pprint :as pprint]
[clojure.edn :as edn]))
(defn validate-plugins-edn
"Validate a plugins.edn file"
[file]
(if-let [errors (->> file
slurp
edn/read-string
(m/explain plugin-config-schema/Plugins-edn)
me/humanize)]
(do
(println "Found errors:")
(pprint/pprint errors))
(println "Valid!")))
(defn- validate-file-with-schema
"Validate a file given its schema"
[file schema]
(if-let [errors (->> file
slurp
edn/read-string
(m/explain schema)
me/humanize)]
(do
(println "Found errors:")
(pprint/pprint errors))
(println "Valid!")))
(defn validate-repo-config-edn
"Validate a repo config.edn"
[file]
(validate-file-with-schema file global-config-schema/Config-edn))
(defn validate-global-config-edn
"Validate a global config.edn"
[file]
(validate-file-with-schema file repo-config-schema/Config-edn))
(defn validate-ast
"Validate mldoc ast(s) in a file or as an EDN arg"
[file-or-edn]
(let [edn (edn/read-string
(if (fs/exists? file-or-edn) (slurp file-or-edn) file-or-edn))]
(if (and (sequential? edn) (:ast (first edn)))
;; Validate multiple asts in the format [{:file "" :ast []} ...]
;; Produced by https://github.com/logseq/nbb-logseq/tree/main/examples/from-js#graph_astmjs
(do
(println "Validating" (count edn) "files...")
(if-let [errors-by-file (seq (keep
#(when-let [errors (m/explain mldoc-schema/block-ast-with-pos-coll-schema (:ast %))]
{:file (:file %)
:errors errors})
edn))]
(do
(println "Found errors:")
(pprint/pprint errors-by-file))
(println "All files valid!")))
(if-let [errors (m/explain mldoc-schema/block-ast-with-pos-coll-schema edn)]
(do
(println "Found errors:")
(pprint/pprint errors))
(println "Valid!")))))