Files: ab5a17fd3d3a3393ad0c8f8939e17344adacee6b / lib / validators.js
6645 bytesRaw
1 | var valid = require('muxrpc-validation') |
2 | var zerr = require('zerr') |
3 | var ref = require('ssb-ref') |
4 | |
5 | // errors |
6 | var MissingAttr = zerr('Usage', 'Param % must have a .% of type "%"') |
7 | var AttrType = zerr('Usage', '.% of param % must be of type "%"') |
8 | |
9 | function isFilter (v) { |
10 | return (v == '@' || v == '%' || v == '&') |
11 | } |
12 | |
13 | module.exports = valid({ |
14 | msgId: function (v) { |
15 | if (!ref.isMsg(v)) |
16 | return 'type' |
17 | }, |
18 | feedId: function (v) { |
19 | if (!ref.isFeed(v)) |
20 | return 'type' |
21 | }, |
22 | blobId: function (v) { |
23 | if (!ref.isBlob(v)) |
24 | return 'type' |
25 | }, |
26 | |
27 | msgContent: function (v, n) { |
28 | var err = this.get('object')(v, n) |
29 | if (err) return err |
30 | if (!v.type || typeof v.type != 'string') |
31 | return MissingAttr(n, 'type', 'string') |
32 | }, |
33 | |
34 | msg: function (v, n) { |
35 | var err = this.get('object')(v, n) |
36 | if (err) |
37 | return err |
38 | |
39 | //allow content to be string. (i.e. for encrypted messages) |
40 | //or object with type string |
41 | if(!v.content) |
42 | return MissingAttr(n, 'content', 'object|string') |
43 | else if(typeof v.content === 'string') |
44 | ; //check if it's base64? |
45 | else if('object' === typeof v.content) { |
46 | if(!v.content.type || typeof v.content.type != 'string') |
47 | return MissingAttr(n, 'content.type', 'string') |
48 | } |
49 | else |
50 | return MissingAttr(n, 'content', 'object|string') |
51 | |
52 | // .author |
53 | if (!ref.isFeed(v.author)) |
54 | return MissingAttr(n, 'author', 'feedId') |
55 | |
56 | // .sequence |
57 | if (typeof v.sequence != 'number') |
58 | return MissingAttr(n, 'sequence', 'number') |
59 | |
60 | // .previous |
61 | if (v.sequence > 1 && !ref.isMsg(v.previous)) |
62 | return MissingAttr(n, 'previous', 'msgId') |
63 | else if(v.sequence == 1 && v.previous != null) |
64 | return MissingAttr(n, 'previous', 'null') |
65 | |
66 | // .timestamp |
67 | if (typeof v.timestamp != 'number') |
68 | return MissingAttr(n, 'timestamp', 'number') |
69 | |
70 | // .hash |
71 | if (v.hash != 'sha256') |
72 | return zerr('Usage', 'Param % must have .hash set to "sha256"')(n) |
73 | |
74 | // .signature |
75 | if (typeof v.signature != 'string') |
76 | return MissingAttr(n, 'signature', 'string') |
77 | }, |
78 | |
79 | readStreamOpts: function (v, n) { |
80 | var err = this.get('object')(v, n) |
81 | if (err) |
82 | return err |
83 | |
84 | // .live |
85 | if (v.live && typeof v.live != 'boolean' && typeof v.live != 'number') |
86 | return AttrType(n, 'live', 'boolean') |
87 | |
88 | // .reverse |
89 | if (v.reverse && typeof v.reverse != 'boolean' && typeof v.reverse != 'number') |
90 | return AttrType(n, 'reverse', 'boolean') |
91 | |
92 | // .keys |
93 | if (v.keys && typeof v.keys != 'boolean' && typeof v.keys != 'number') |
94 | return AttrType(n, 'keys', 'boolean') |
95 | |
96 | // .values |
97 | if (v.values && typeof v.values != 'boolean' && typeof v.values != 'number') |
98 | return AttrType(n, 'values', 'boolean') |
99 | |
100 | // .limit |
101 | if (v.limit && typeof v.limit != 'number') |
102 | return AttrType(n, 'limit', 'number') |
103 | |
104 | // .fillCache |
105 | if (v.fillCache && typeof v.fillCache != 'boolean' && typeof v.fillCache != 'number') |
106 | return AttrType(n, 'fillCache', 'boolean') |
107 | }, |
108 | |
109 | createHistoryStreamOpts: function (v, n) { |
110 | // .id |
111 | if (!ref.isFeed(v.id)) |
112 | return MissingAttr(n, 'id', 'feedId') |
113 | |
114 | // .seq |
115 | if (v.seq && typeof v.seq != 'number') |
116 | return AttrType(n, 'seq', 'number') |
117 | |
118 | // .live |
119 | if (v.live && typeof v.live != 'boolean' && typeof v.live != 'number') |
120 | return AttrType(n, 'live', 'boolean') |
121 | |
122 | // .limit |
123 | if (v.limit && typeof v.limit != 'number') |
124 | return AttrType(n, 'limit', 'number') |
125 | |
126 | // .keys |
127 | if (v.keys && typeof v.keys != 'boolean' && typeof v.keys != 'number') |
128 | return AttrType(n, 'keys', 'boolean') |
129 | |
130 | // .values |
131 | if (v.values && typeof v.values != 'boolean' && typeof v.values != 'number') |
132 | return AttrType(n, 'values', 'boolean') |
133 | }, |
134 | |
135 | createUserStreamOpts: function (v, n) { |
136 | var err = this.get('readStreamOpts')(v, n) |
137 | if (err) |
138 | return err |
139 | |
140 | // .id |
141 | if (!ref.isFeed(v.id)) |
142 | return MissingAttr(n, 'id', 'feedId') |
143 | }, |
144 | |
145 | messagesByTypeOpts: function (v, n) { |
146 | var err = this.get('readStreamOpts')(v, n) |
147 | if (err) |
148 | return err |
149 | |
150 | // .type |
151 | if (typeof v.type != 'string') |
152 | return MissingAttr(n, 'type', 'string') |
153 | }, |
154 | |
155 | linksOpts: function (v, n) { |
156 | var err = this.get('object')(v, n) |
157 | if (err) |
158 | return err |
159 | |
160 | // .source |
161 | if (v.source && !ref.isLink(v.source) && !isFilter(v.source)) |
162 | return AttrType(n, 'source', 'id|filter') |
163 | |
164 | // .dest |
165 | if (v.dest && !ref.isLink(v.dest) && !isFilter(v.dest)) |
166 | return AttrType(n, 'dest', 'id|filter') |
167 | |
168 | // .rel |
169 | if (v.rel && typeof v.rel != 'string') |
170 | return AttrType(n, 'rel', 'string') |
171 | |
172 | // .live |
173 | if (v.live && typeof v.live != 'boolean' && typeof v.live != 'number') |
174 | return AttrType(n, 'live', 'boolean') |
175 | |
176 | // .reverse |
177 | if (v.reverse && typeof v.reverse != 'boolean' && typeof v.reverse != 'number') |
178 | return AttrType(n, 'reverse', 'boolean') |
179 | |
180 | // .keys |
181 | if (v.keys && typeof v.keys != 'boolean' && typeof v.keys != 'number') |
182 | return AttrType(n, 'keys', 'boolean') |
183 | |
184 | // .values |
185 | if (v.values && typeof v.values != 'boolean' && typeof v.values != 'number') |
186 | return AttrType(n, 'values', 'boolean') |
187 | }, |
188 | |
189 | relatedMessagesOpts: function (v, n) { |
190 | var err = this.get('object')(v, n) |
191 | if (err) |
192 | return err |
193 | |
194 | // .id |
195 | if (!ref.isMsg(v.id)) |
196 | return MissingAttr(n, 'id', 'msgId') |
197 | |
198 | // .rel |
199 | if (v.rel && typeof v.rel != 'string') |
200 | return AttrType(n, 'rel', 'string') |
201 | |
202 | // .count |
203 | if (v.count && typeof v.count != 'boolean' && typeof v.count != 'number') |
204 | return AttrType(n, 'count', 'boolean') |
205 | |
206 | // .parent |
207 | if (v.parent && typeof v.parent != 'boolean' && typeof v.parent != 'number') |
208 | return AttrType(n, 'parent', 'boolean') |
209 | }, |
210 | |
211 | isBlockedOpts: function (v, n) { |
212 | var err = this.get('object')(v, n) |
213 | if (err) |
214 | return err |
215 | |
216 | // .source |
217 | if (v.source && !ref.isFeed(v.source)) |
218 | return AttrType(n, 'source', 'feedId') |
219 | |
220 | // .dest |
221 | if (v.dest && !ref.isFeed(v.dest)) |
222 | return AttrType(n, 'dest', 'feedId') |
223 | }, |
224 | |
225 | createFriendStreamOpts: function (v, n) { |
226 | var err = this.get('object')(v, n) |
227 | if (err) |
228 | return err |
229 | |
230 | // .start |
231 | if (v.start && !ref.isFeed(v.start)) |
232 | return AttrType(n, 'start', 'feedId') |
233 | |
234 | // .graph |
235 | if (v.graph && typeof v.graph != 'string') |
236 | return AttrType(n, 'graph', 'string') |
237 | |
238 | // .dunbar |
239 | if (v.dunbar && typeof v.dunbar != 'number') |
240 | return AttrType(n, 'dunbar', 'number') |
241 | |
242 | // .hops |
243 | if (v.hops && typeof v.hops != 'number') |
244 | return AttrType(n, 'hops', 'number') |
245 | } |
246 | }) |
247 | |
248 | |
249 | |
250 | |
251 | |
252 | |
253 | |
254 | |
255 | |
256 | |
257 |
Built with git-ssb-web