/
schema.cljc
146 lines (113 loc) · 4.67 KB
/
schema.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
; Copyright (c) Alan Thompson. All rights reserved.
; The use and distribution terms for this software are covered by the Eclipse Public
; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the
; file epl-v10.html at the root of this distribution. By using this software in any
; fashion, you are agreeing to be bound by the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns tupelo.schema
"Prismatic Schema type definitions"
(:refer-clojure :exclude [Fn List Single MapEntry])
(:require
[schema.core :as s]
[tupelo.core.impl :as impl])
#?(:clj
(:import [java.util HashSet])))
(def Map {s/Any s/Any} )
(def KeyMap {s/Keyword s/Any} )
(def MapEntry #?(:clj clojure.lang.MapEntry
:cljs cljs.core.MapEntry))
(def TagVal {s/Keyword s/Any}) ; and length=1
(def Set
"Either a Clojure hash-set or a java.util.HashSet"
#?(:clj (s/if set? #{s/Any} java.util.HashSet)
:cljs #{s/Any}))
(def Vec
"An ordered sequence of items of indeterminate length (synonymous for List)."
[s/Any])
(def List
"An ordered sequence of items of indeterminate length (synonymous for Vec)."
[s/Any])
(def Array
"An ordered sequence of vectors."
[Vec])
#?(:clj ; #todo cljs?
(def CharVec
"An ordered sequence of characters of indeterminate length."
[Character]))
(def Single [(s/one s/Any "x1")]) ; length-1 vector
(def Pair [(s/one s/Any "x1") (s/one s/Any "x2")]) ; length-2 vector
(def Triple [(s/one s/Any "x1") (s/one s/Any "x2") (s/one s/Any "x3")]) ; length-3 vector
(def Quad [(s/one s/Any "x1") (s/one s/Any "x2") (s/one s/Any "x3") (s/one s/Any "x4")]) ; length-4 vector
(def EnliveNode
"An Enlive tree node"
{:tag s/Any :attrs KeyMap :content [s/Any]})
(def Quarter (s/enum :Q1 :Q2 :Q3 :Q4))
(def Tuple
"A specific type of sequential collection, typically a vector of constant length where each
element has a pre-defined interpretation."
[s/Any])
(def TupleList
"A sequence of tuples (typically a vector of vectors)"
[Tuple])
(def TupleSet
"The result of any Datomic using the Entity API is logically a hash-set of tuples (vectors).
The contents and order of each tuple is determined by the find clause:
----- query ----- ----- tuples -----
(d/q '{:find [?e ?name ?age] ...) -> [?e ?name ?age]
"
#{Tuple})
(def MapList [Map]) ; a list of Maps
(def TupleMap [Map]) ; a single result returned by Datomic pull api ; #todo needs (s/one ...) ??? or MapList?
(def TupleMaps [TupleMap]) ; a list of results returned by Datomic pull api
(def UuidStr (s/constrained s/Str impl/uuid-str?))
(def Collection
"Any collection type of Vec (& List), Map, or Set"
(s/cond-pre Vec Map Set))
(def Fn (s/make-fn-schema s/Any s/Any))
(def Interceptor ; #todo add to tupelo.forest
"Plumatic Schema type name for interceptor type used by `walk-entity`."
{(s/required-key :enter) s/Any
(s/required-key :leave) s/Any
(s/optional-key :id) s/Keyword})
(defmacro with-validation-enabled
"Run forms with Plumatic Schema enabled"
[& forms]
`(let [orig-validation-state# (s/fn-validation?)]
(s/set-fn-validation! true)
(let [result# (do ~@forms)]
(s/set-fn-validation! orig-validation-state#)
result#)))
; #todo convert to try/finally
(defmacro with-validation-disabled
"Run forms with Plumatic Schema disabled"
[& forms]
`(let [orig-validation-state# (s/fn-validation?)]
(s/set-fn-validation! false)
(let [result# (do ~@forms)]
(s/set-fn-validation! orig-validation-state#)
result#)))
;-----------------------------------------------------------------------------
; HTTP related stuff
(def HttpRequest
{s/Any s/Any
:body s/Any
:content-length s/Any
:headers {s/Str s/Str}
:params s/Any
:protocol s/Str
:remote-addr s/Str
:scheme s/Keyword
:server-name s/Str
:server-port s/Int
:uri s/Str
(s/optional-key :content-type) nil
(s/optional-key :cookies) s/Any
(s/optional-key :flash) nil
(s/optional-key :form-params) s/Any
(s/optional-key :query-params) {}
(s/optional-key :query-string) s/Any
(s/optional-key :request-method) s/Keyword
(s/optional-key :route-handler) s/Any
(s/optional-key :route-params) s/Any
(s/optional-key :session) s/Any
(s/optional-key :ssl-client-cert) s/Any})