-
Notifications
You must be signed in to change notification settings - Fork 4
/
prefix.cljc
33 lines (30 loc) · 1.26 KB
/
prefix.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
(ns com.yetanalytics.flint.validate.prefix
(:require [com.yetanalytics.flint.validate.util :as vu]))
(defn- invalid-prefix?
"Does the prefix of `prefix-iri` exist in the `prefixes` map/set?
(Or for non-namespaced `prefix-iri`, does `:$` exist?)"
[prefixes prefix-iri]
(if-some [pre (namespace prefix-iri)]
(not (contains? prefixes (keyword pre)))
(not (contains? prefixes :$))))
(defn- prefix-err-map
[prefixes prefix-iri loc]
{:prefixes prefixes
:iri prefix-iri
:prefix (or (some->> prefix-iri namespace keyword) :$)
:path (conj (vu/zip-path loc) :ax/prefix-iri)})
(defn validate-prefixes
"Given `node-m` a map from nodes to zipper locs, check that each prefix
node is included in `prefixes`. If validation fails, return a coll of
error maps; otherwise return `nil`."
[prefixes node-m]
(let [prefix-m (:ax/prefix-iri node-m)
errors (reduce (fn [acc [prefix locs]]
(if (invalid-prefix? prefixes prefix)
(->> locs
(map (partial prefix-err-map prefixes prefix))
(concat acc))
acc))
[]
prefix-m)]
(not-empty errors)))