-
Notifications
You must be signed in to change notification settings - Fork 7
/
profile.clj
75 lines (65 loc) · 2.96 KB
/
profile.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
73
74
75
(ns com.yetanalytics.datasim.input.profile
"Profile input parsing."
(:require [clojure.spec.alpha :as s]
[clojure.string :as cstr]
[com.yetanalytics.pan :as pan]
[com.yetanalytics.pan.objects.profile :as pan-profile]
[com.yetanalytics.datasim.util.errors :as errs]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Specs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(s/def ::profiles
(s/every ::pan-profile/profile :min-count 1))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Validation
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn validate-profile
[{:keys [id] :as profile}]
(some->> (pan/validate-profile profile
:syntax? true
:result :type-path-string)
(errs/type-path-string-m->map-coll id)))
(defn validate-profiles
[profiles]
(if (vector? profiles)
(let [prof-errs (pan/validate-profile-coll profiles
:syntax? true
:pattern-rels? true
:result :type-path-string)]
(errs/type-path-string-ms->map-coll (map :id profiles)
prof-errs))
;; TODO: Something more solid/less hacky, particularly in the Pan lib itself
[{:path [::profiles]
:text "Profiles must be a vector!"
:id [::profiles]}]))
;; Helpers to `input/validate-pattern-filters`
(defn- validate-pattern-filters-emsg
[pattern-id pattern-id-set]
(format
"Pattern ID %s is not among primary patterns in provided profiles: %s"
pattern-id
(cstr/join \, pattern-id-set)))
(defn- validate-profile-fitlers-emsg
[profile-id profile-id-set]
(format "Profile ID %s is not one of provided profiles: %s"
profile-id
(cstr/join \, profile-id-set)))
(defn validate-pattern-filters
[profiles gen-patterns]
(let [pattern-id-set (->> profiles
(mapcat :patterns)
(keep (fn [{:keys [id primary]}] (when primary id)))
(into #{}))]
(for [[idx pattern-id] (map-indexed vector gen-patterns)
:when (not (contains? pattern-id-set pattern-id))]
{:id (str "parameters-genPatterns-" idx)
:path [:parameters :genPatterns idx]
:text (validate-pattern-filters-emsg pattern-id pattern-id-set)})))
(defn validate-profile-filters
[profiles gen-profiles]
(let [profile-id-set (->> profiles (map :id) (into #{}))]
(for [[idx profile-id] (map-indexed vector gen-profiles)
:when (not (contains? profile-id-set profile-id))]
{:id (str "parameters-genProfiles-" idx)
:path [:parameters :genProfiles idx]
:text (validate-profile-fitlers-emsg profile-id profile-id-set)})))