@sockethub/schemas 3.0.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +11 -0
  3. package/dist/helpers/error-parser.d.ts +9 -0
  4. package/dist/helpers/error-parser.js +91 -0
  5. package/dist/helpers/objects.d.ts +206 -0
  6. package/dist/helpers/objects.js +217 -0
  7. package/dist/index.d.ts +15 -0
  8. package/dist/index.js +22 -0
  9. package/dist/index.test.data.credentials.d.ts +37 -0
  10. package/dist/index.test.data.credentials.js +69 -0
  11. package/dist/index.test.data.objects.d.ts +50 -0
  12. package/dist/index.test.data.objects.js +214 -0
  13. package/dist/index.test.data.platform.d.ts +44 -0
  14. package/dist/index.test.data.platform.js +47 -0
  15. package/dist/index.test.data.streams.d.ts +256 -0
  16. package/dist/index.test.data.streams.js +449 -0
  17. package/dist/schemas/activity-object.d.ts +16 -0
  18. package/dist/schemas/activity-object.js +18 -0
  19. package/dist/schemas/activity-stream.d.ts +235 -0
  20. package/dist/schemas/activity-stream.js +49 -0
  21. package/dist/schemas/json/activity-object.json +356 -0
  22. package/dist/schemas/json/activity-stream.json +465 -0
  23. package/dist/schemas/json/platform.json +27 -0
  24. package/dist/schemas/platform.d.ts +41 -0
  25. package/dist/schemas/platform.js +44 -0
  26. package/dist/types.d.ts +20 -0
  27. package/dist/types.js +2 -0
  28. package/dist/validator.d.ts +8 -0
  29. package/dist/validator.js +78 -0
  30. package/package.json +58 -0
  31. package/src/helpers/error-parser.ts +99 -0
  32. package/src/helpers/objects.ts +230 -0
  33. package/src/index.test.data.credentials.ts +71 -0
  34. package/src/index.test.data.objects.ts +235 -0
  35. package/src/index.test.data.platform.ts +45 -0
  36. package/src/index.test.data.streams.ts +470 -0
  37. package/src/index.test.ts +84 -0
  38. package/src/index.ts +26 -0
  39. package/src/schemas/activity-object.ts +19 -0
  40. package/src/schemas/activity-stream.ts +51 -0
  41. package/src/schemas/platform.ts +47 -0
  42. package/src/types.ts +23 -0
  43. package/src/validator.ts +79 -0
@@ -0,0 +1,45 @@
1
+ export default {
2
+ "name": "fake",
3
+ "version": "1.0",
4
+ "messages": {
5
+ "required": ['type'],
6
+ "properties": {
7
+ "type": {
8
+ "enum": ['connect', 'update', 'send', 'join', 'query', 'foo']
9
+ }
10
+ }
11
+ },
12
+ "credentials": {
13
+ "required": ['object'],
14
+ "properties": {
15
+ // TODO platforms shouldn't have to define the actor property if
16
+ // they don't want to, just credential specifics
17
+ "actor": {
18
+ "type": "object",
19
+ "required": ["id"]
20
+ },
21
+ "object": {
22
+ "type": "object",
23
+ "required": ['type', 'username', 'password', 'host'],
24
+ "additionalProperties": false,
25
+ "properties": {
26
+ "type": {
27
+ "type": "string"
28
+ },
29
+ "username": {
30
+ "type": "string"
31
+ },
32
+ "password": {
33
+ "type": "string"
34
+ },
35
+ "host": {
36
+ "type": "string"
37
+ },
38
+ "port": {
39
+ "type": "number"
40
+ }
41
+ }
42
+ }
43
+ }
44
+ }
45
+ };
@@ -0,0 +1,470 @@
1
+ import {ObjectTypesList} from './helpers/objects';
2
+
3
+ export default [
4
+ [
5
+ 'type:send, object:message',
6
+ {
7
+ type: 'send',
8
+ context: 'irc',
9
+ actor: {
10
+ id: 'dood@irc.freenode.net',
11
+ type: 'person',
12
+ name: 'dood'
13
+ },
14
+ target: {
15
+ id: 'irc.freenode.net/server',
16
+ type: 'room',
17
+ name: 'sockethub'
18
+ },
19
+ object: {
20
+ type: 'message',
21
+ content: 'some kind of message'
22
+ }
23
+ },
24
+ true,
25
+ ''
26
+ ],
27
+
28
+ [
29
+ 'type:credentials, object:credentials',
30
+ {
31
+ type: 'credentials',
32
+ context: 'irc',
33
+ actor: {
34
+ id: 'dood@irc.freenode.net',
35
+ type: 'person',
36
+ name: 'dood'
37
+ },
38
+ target: {
39
+ id: 'irc.freenode.net/server',
40
+ type: 'room',
41
+ name: 'sockethub'
42
+ },
43
+ object: {
44
+ type: 'credentials'
45
+ }
46
+ },
47
+ true,
48
+ ''
49
+ ],
50
+
51
+ [
52
+ 'type:credentials',
53
+ {
54
+ context: 'irc',
55
+ type: 'credentials',
56
+ actor: {
57
+ id: 'dood@irc.freenode.net',
58
+ type: 'person',
59
+ name: 'dood'
60
+ },
61
+ object: {
62
+ type: 'credentials',
63
+ user: "foo",
64
+ pass: "bar"
65
+ }
66
+ },
67
+ true,
68
+ ''
69
+ ],
70
+
71
+ [
72
+ 'bad target',
73
+ {
74
+ id: 'blah',
75
+ type: 'send',
76
+ context: 'dood',
77
+ actor: {
78
+ id: 'dood@irc.freenode.net',
79
+ type: 'person',
80
+ name: 'dood'
81
+ },
82
+ target: {
83
+ type: 'person',
84
+ name: 'bob'
85
+ },
86
+ object: {
87
+ type: 'credentials'
88
+ }
89
+ },
90
+ false,
91
+ '/target: must have required property \'id\''
92
+ ],
93
+
94
+ [
95
+ 'bad iri in object id',
96
+ {
97
+ type: 'feed',
98
+ context: 'dood',
99
+ actor: {
100
+ id: 'dood@irc.freenode.net',
101
+ type: 'person'
102
+ },
103
+ object: {
104
+ id: 'example.org/some/path.html',
105
+ type: 'website'
106
+ }
107
+ },
108
+ false,
109
+ '/object/id: must match format "iri"'
110
+ ],
111
+
112
+ [
113
+ 'valid iri in object id',
114
+ {
115
+ type: 'feed',
116
+ context: 'dood',
117
+ actor: {
118
+ id: 'dood@irc.freenode.net',
119
+ type: 'person'
120
+ },
121
+ object: {
122
+ id: 'https://example.org/some/path.html',
123
+ type: 'website'
124
+ }
125
+ },
126
+ true,
127
+ ''
128
+ ],
129
+
130
+ [
131
+ 'wrong actor type',
132
+ {
133
+ type: 'send',
134
+ context: 'dood',
135
+ actor: {
136
+ id: 'doobar@freenode.net/channel',
137
+ type: 'foo',
138
+ content: 'hi there'
139
+ },
140
+ object: {
141
+ type: 'feed',
142
+ id: 'http://rss.example.org/feed.rss'
143
+ }
144
+ },
145
+ false,
146
+ `/actor: must match exactly one schema in oneOf: ${ObjectTypesList.join(', ')}`
147
+ ],
148
+
149
+ [
150
+ 'missing actor id',
151
+ {
152
+ id: 'blah',
153
+ type: 'send',
154
+ context: 'dood',
155
+ actor: {
156
+ type: 'person',
157
+ name: 'dood'
158
+ },
159
+ target: {
160
+ id: 'bob@crusty.net/Home',
161
+ type: 'person',
162
+ name: 'bob'
163
+ },
164
+ object: {
165
+ type: 'feed',
166
+ id: 'http://rss.example.org/feed.rss'
167
+ }
168
+ },
169
+ false,
170
+ '/actor: must have required property \'id\''
171
+ ],
172
+
173
+ [
174
+ 'attendance request',
175
+ {
176
+ id: 'blah',
177
+ type: 'observe',
178
+ context: 'dood',
179
+ actor: {
180
+ id: 'dood',
181
+ type: 'person'
182
+ },
183
+ target: {
184
+ id: 'chatroom@crusty.net',
185
+ type: 'room'
186
+ },
187
+ object: {
188
+ type: 'attendance',
189
+ }
190
+ },
191
+ true,
192
+ ''
193
+ ],
194
+
195
+ [
196
+ 'attendance response',
197
+ {
198
+ id: 'blah',
199
+ type: 'observe',
200
+ context: 'dood',
201
+ actor: {
202
+ id: 'chatroom@crusty.net',
203
+ type: 'room',
204
+ name: 'chatroom'
205
+ },
206
+ object: {
207
+ type: 'attendance',
208
+ members: ['bill', 'bob', 'hank']
209
+ }
210
+ },
211
+ true,
212
+ ''
213
+ ],
214
+
215
+ [
216
+ 'invalid attendance request',
217
+ {
218
+ id: 'blah',
219
+ type: 'observe',
220
+ context: 'dood',
221
+ actor: {
222
+ id: 'dood',
223
+ type: 'person'
224
+ },
225
+ target: {
226
+ id: 'chatroom@crusty.net',
227
+ type: 'room'
228
+ },
229
+ object: {
230
+ type: 'attendance',
231
+ status: 'foobar'
232
+ }
233
+ },
234
+ false,
235
+ '/object: must NOT have additional properties: status'
236
+ ],
237
+
238
+ [
239
+ 'setting presence',
240
+ {
241
+ id: 'blah',
242
+ type: 'observe',
243
+ context: 'dood',
244
+ actor: {
245
+ id: 'dood',
246
+ type: 'person'
247
+ },
248
+ object: {
249
+ type: 'presence',
250
+ presence: 'away',
251
+ content: 'forgot the milk'
252
+ },
253
+ target: {
254
+ id: 'chatroom@crusty.net',
255
+ type: 'room'
256
+ }
257
+ },
258
+ true,
259
+ ''
260
+ ],
261
+
262
+ [
263
+ 'setting invalid presence',
264
+ {
265
+ id: 'blah',
266
+ type: 'observe',
267
+ context: 'dood',
268
+ actor: {
269
+ id: 'dood',
270
+ type: 'person'
271
+ },
272
+ object: {
273
+ type: 'presence',
274
+ presence: 'afk',
275
+ content: 'forgot the milk'
276
+ },
277
+ target: {
278
+ id: 'chatroom@crusty.net',
279
+ type: 'room'
280
+ }
281
+ },
282
+ false,
283
+ '/object/presence: must be equal to one of the allowed values: ' +
284
+ 'away, chat, dnd, xa, offline, online'
285
+ ],
286
+
287
+ [
288
+ 'setting topic',
289
+ {
290
+ id: 'blah',
291
+ type: 'observe',
292
+ context: 'dood',
293
+ actor: {
294
+ id: 'dood',
295
+ type: 'person'
296
+ },
297
+ object: {
298
+ type: 'topic',
299
+ content: 'the topic is goats'
300
+ },
301
+ target: {
302
+ id: 'chatroom@crusty.net',
303
+ type: 'room'
304
+ }
305
+ },
306
+ true,
307
+ ''
308
+ ],
309
+
310
+ [
311
+ 'setting topic incorrectly',
312
+ {
313
+ id: 'blah',
314
+ type: 'observe',
315
+ context: 'dood',
316
+ actor: {
317
+ id: 'dood',
318
+ type: 'person'
319
+ },
320
+ object: {
321
+ type: 'topic',
322
+ topic: 'the topic is goats'
323
+ },
324
+ target: {
325
+ id: 'chatroom@crusty.net',
326
+ type: 'room'
327
+ }
328
+ },
329
+ false,
330
+ '/object: must NOT have additional properties: topic'
331
+ ],
332
+
333
+
334
+ [
335
+ 'receive topic',
336
+ {
337
+ id: 'blah',
338
+ type: 'observe',
339
+ context: 'dood',
340
+ actor: {
341
+ id: 'chatroom@crusty.net',
342
+ type: 'room'
343
+ },
344
+ object: {
345
+ type: 'topic',
346
+ content: 'the topic is goats'
347
+ }
348
+ },
349
+ true,
350
+ ''
351
+ ],
352
+
353
+ [
354
+ 'change user address',
355
+ {
356
+ id: 'blah',
357
+ type: 'update',
358
+ context: 'dood',
359
+ actor: {
360
+ id: 'foobar@example.com',
361
+ type: 'person'
362
+ },
363
+ object: {
364
+ type: 'address'
365
+ },
366
+ target: {
367
+ id: 'futurebar@example.com',
368
+ type: 'person'
369
+ },
370
+ },
371
+ true,
372
+ ''
373
+ ],
374
+
375
+ [
376
+ 'change user address incorrectly',
377
+ {
378
+ type: 'update',
379
+ context: 'dood',
380
+ actor: {
381
+ id: 'foobar@example.com',
382
+ type: 'person'
383
+ },
384
+ object: {
385
+ type: 'address',
386
+ id: 'futurebar@example.com',
387
+ }
388
+ },
389
+ false,
390
+ '/object: must NOT have additional properties: id'
391
+ ],
392
+
393
+ [
394
+ 'invalid activity stream',
395
+ {
396
+ "actor": { "id": "irc://uuu@localhost", "type": "person" },
397
+ "context":"irc",
398
+ "target": { "id": "irc://irc.dooder.net/a-room", "type": "room" }
399
+ },
400
+ false,
401
+ 'activity stream: must have required property \'type\''
402
+ ],
403
+
404
+ [
405
+ 'invalid room',
406
+ {
407
+ "context":"irc",
408
+ "type":"credentials",
409
+ "actor":{
410
+ "id":"sh-9K3Vk@irc.freenode.net",
411
+ "type":"person",
412
+ },
413
+ "target": {
414
+ "type":"room",
415
+ "name":"sh-9K3Vk"
416
+ }
417
+ },
418
+ false,
419
+ '/target: must have required property \'id\''
420
+ ],
421
+
422
+ [
423
+ 'relationship',
424
+ {
425
+ "context":"irc",
426
+ "type":"add",
427
+ "actor":{"type":"person", "id":"alice@localhost", "name":"alice"},
428
+ "target":{"type":"person", "id":"Kilroy@localhost", "name":"Kilroy"},
429
+ "object":{
430
+ "type":"relationship",
431
+ "relationship":"role",
432
+ "subject": {
433
+ "type":"presence",
434
+ "role":"owner"
435
+ },
436
+ "object":{
437
+ "type":"room",
438
+ "id":"localhost/#Finnish",
439
+ "name":"#Finnish"
440
+ }
441
+ }
442
+ },
443
+ true, ""
444
+ ],
445
+
446
+ [
447
+ 'invalid role in relationship',
448
+ {
449
+ "context":"irc",
450
+ "type":"add",
451
+ "actor":{"type":"person", "id":"alice@localhost", "name":"alice"},
452
+ "target":{"type":"person", "id":"Kilroy@localhost", "name":"Kilroy"},
453
+ "object":{
454
+ "type":"relationship",
455
+ "relationship":"role",
456
+ "subject": {
457
+ "type":"presence",
458
+ "role":"manager"
459
+ },
460
+ "object":{
461
+ "type":"room",
462
+ "id":"localhost/#Finnish",
463
+ "name":"#Finnish"
464
+ }
465
+ }
466
+ },
467
+ false, "/object/subject/role: must be equal to one of the allowed values: " +
468
+ "owner, member, participant, admin"
469
+ ]
470
+ ];
@@ -0,0 +1,84 @@
1
+ const chai = require('chai');
2
+ const expect = chai.expect;
3
+
4
+ import ActivityStreamSchema from './schemas/activity-stream';
5
+ import ActivityObjectSchema from './schemas/activity-object';
6
+ import {
7
+ validatePlatformSchema, addPlatformSchema, validateActivityObject,
8
+ validateActivityStream, validateCredentials, getPlatformSchema
9
+ } from './validator';
10
+ import testCredentialsData from './index.test.data.credentials';
11
+ import testActivityObjectsData from './index.test.data.objects';
12
+ import testActivityStreamsData from './index.test.data.streams';
13
+ import testPlatformSchemaData from './index.test.data.platform';
14
+
15
+ describe('Platform schema validation', () => {
16
+ it('returns an empty error for a valid schema', () => {
17
+ let err = validatePlatformSchema(testPlatformSchemaData);
18
+ expect(err).to.equal("");
19
+ });
20
+ it('returns an error for an invalid schema', () => {
21
+ let err = validatePlatformSchema({foo: 'bar'});
22
+ expect(err).to.eql('platform schema failed to validate: must have required property \'name\'');
23
+ });
24
+ });
25
+
26
+
27
+ describe("Adding a PlatformSchema", () => {
28
+ it('returns the same schema when fetched', () => {
29
+ const platform_type = 'test-platform/credentials';
30
+ addPlatformSchema(testPlatformSchemaData.credentials, platform_type);
31
+ const compiledSchema = getPlatformSchema(platform_type);
32
+ expect(compiledSchema.schema).to.eql(testPlatformSchemaData.credentials);
33
+ });
34
+ });
35
+
36
+
37
+ describe('Credentials', () => {
38
+ testCredentialsData.forEach(([name, creds, expectedResult, expectedFailureMessage]) => {
39
+ describe("validateCredential " + name, () => {
40
+ it(`returns expected result`, () => {
41
+ // @ts-ignore
42
+ const err = validateCredentials(creds);
43
+ expect(err).to.equal(expectedFailureMessage);
44
+ expect(!err).to.equal(expectedResult);
45
+ });
46
+ });
47
+ });
48
+ });
49
+
50
+ describe('ActivityObject', () => {
51
+ it('has expected properties', () => {
52
+ expect(typeof ActivityObjectSchema).to.equal('object');
53
+ expect(ActivityObjectSchema['$id']).to.equal('https://sockethub.org/schemas/v0/activity-object#');
54
+ });
55
+
56
+ testActivityObjectsData.forEach(([name, ao, expectedResult, expectedFailureMessage]) => {
57
+ describe("validateActivityObject " + name, () => {
58
+ it(`returns expected result`, () => {
59
+ // @ts-ignore
60
+ const err = validateActivityObject(ao);
61
+ expect(err).to.equal(expectedFailureMessage);
62
+ expect(!err).to.equal(expectedResult);
63
+ });
64
+ });
65
+ });
66
+ });
67
+
68
+ describe('ActivityStream', () => {
69
+ it('has expected properties', () => {
70
+ expect(typeof ActivityStreamSchema).to.equal('object');
71
+ expect(ActivityStreamSchema['$id']).to.equal('https://sockethub.org/schemas/v0/activity-stream#');
72
+ });
73
+
74
+ testActivityStreamsData.forEach(([name, as, expectedResult, expectedFailureMessage]) => {
75
+ describe("validateActivityStream " + name, () => {
76
+ it(`returns expected result`, () => {
77
+ // @ts-ignore
78
+ const err = validateActivityStream(as);
79
+ expect(err).to.equal(expectedFailureMessage);
80
+ expect(!err).to.equal(expectedResult);
81
+ });
82
+ });
83
+ });
84
+ });
package/src/index.ts ADDED
@@ -0,0 +1,26 @@
1
+ import {addPlatformSchema, validateActivityObject, validateActivityStream, validateCredentials,
2
+ validatePlatformSchema} from "./validator";
3
+ import { ObjectTypesList } from "./helpers/objects";
4
+ import ActivityObjectSchema from "./schemas/activity-object";
5
+ import ActivityStreamSchema from "./schemas/activity-stream";
6
+ import PlatformSchema from "./schemas/platform";
7
+
8
+ export default {
9
+ addPlatformSchema,
10
+ validatePlatformSchema,
11
+ validateCredentials,
12
+ validateActivityStream,
13
+ validateActivityObject,
14
+ };
15
+
16
+ export {
17
+ PlatformSchema,
18
+ ActivityObjectSchema,
19
+ ActivityStreamSchema,
20
+ ObjectTypesList
21
+ };
22
+
23
+ export {
24
+ IActivityStream,
25
+ IActivityObject
26
+ } from "./types";
@@ -0,0 +1,19 @@
1
+ import { validObjectRefs, validObjectDefs } from "../helpers/objects";
2
+
3
+ export default {
4
+ "$id": "https://sockethub.org/schemas/v0/activity-object#",
5
+ "description": "Schema for Sockethub Activity Objects",
6
+
7
+ "type": "object",
8
+ "required" : [ "object" ],
9
+ "properties": {
10
+ "object": {
11
+ "type": "object",
12
+ "oneOf": validObjectRefs
13
+ }
14
+ },
15
+
16
+ "definitions": {
17
+ "type": validObjectDefs
18
+ }
19
+ };
@@ -0,0 +1,51 @@
1
+ import activityObject from "./activity-object";
2
+ import { ObjectTypesList, ObjectTypesSchema } from "../helpers/objects";
3
+
4
+ const validActorRefs = activityObject.properties.object.oneOf;
5
+ const validTargetRefs = activityObject.properties.object.oneOf;
6
+ // eslint-disable-next-line security-node/detect-crlf
7
+ console.log(validActorRefs);
8
+
9
+ let validObjectRefs = [];
10
+
11
+ ObjectTypesList.forEach(function (type, i) {
12
+ validObjectRefs.push({ "$ref": "#/definitions/type/" + type });
13
+ });
14
+
15
+ const contextSchema = {
16
+ "type": "string"
17
+ };
18
+ const typeSchema = {
19
+ "type": "string"
20
+ };
21
+
22
+ export default {
23
+ "$id": "https://sockethub.org/schemas/v0/activity-stream#",
24
+ "description": "Schema for Sockethub Activity Streams",
25
+
26
+ "type": "object",
27
+ "required" : [ "context", "type", "actor" ],
28
+ "properties": {
29
+ "id": {
30
+ "type": "string"
31
+ },
32
+ "type": typeSchema,
33
+ "context": contextSchema,
34
+ "actor": {
35
+ "type": "object",
36
+ "oneOf": validActorRefs
37
+ },
38
+ "target": {
39
+ "type": "object",
40
+ "oneOf": validTargetRefs
41
+ },
42
+ "object": {
43
+ "type": "object",
44
+ "oneOf": validObjectRefs
45
+ }
46
+ },
47
+
48
+ "definitions": {
49
+ "type": ObjectTypesSchema
50
+ }
51
+ };