@sockethub/schemas 3.0.0-alpha.3 → 3.0.0-alpha.5

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/package.json +16 -31
  2. package/src/index.ts +23 -22
  3. package/src/schemas/activity-object.ts +15 -15
  4. package/src/schemas/activity-stream.ts +41 -38
  5. package/{dist → src}/schemas/json/activity-object.json +1 -1
  6. package/{dist → src}/schemas/json/activity-stream.json +5 -1
  7. package/{dist → src}/schemas/json/platform.json +1 -0
  8. package/src/schemas/json/sockethub-config.json +144 -0
  9. package/src/schemas/platform.ts +22 -22
  10. package/src/schemas/sockethub-config.ts +140 -0
  11. package/dist/helpers/error-parser.d.ts +0 -9
  12. package/dist/helpers/error-parser.js +0 -91
  13. package/dist/helpers/objects.d.ts +0 -206
  14. package/dist/helpers/objects.js +0 -217
  15. package/dist/index.d.ts +0 -15
  16. package/dist/index.js +0 -22
  17. package/dist/index.test.data.credentials.d.ts +0 -37
  18. package/dist/index.test.data.credentials.js +0 -69
  19. package/dist/index.test.data.objects.d.ts +0 -50
  20. package/dist/index.test.data.objects.js +0 -214
  21. package/dist/index.test.data.platform.d.ts +0 -44
  22. package/dist/index.test.data.platform.js +0 -47
  23. package/dist/index.test.data.streams.d.ts +0 -256
  24. package/dist/index.test.data.streams.js +0 -449
  25. package/dist/schemas/activity-object.d.ts +0 -16
  26. package/dist/schemas/activity-object.js +0 -18
  27. package/dist/schemas/activity-stream.d.ts +0 -235
  28. package/dist/schemas/activity-stream.js +0 -49
  29. package/dist/schemas/platform.d.ts +0 -41
  30. package/dist/schemas/platform.js +0 -44
  31. package/dist/types.d.ts +0 -20
  32. package/dist/types.js +0 -2
  33. package/dist/validator.d.ts +0 -8
  34. package/dist/validator.js +0 -78
  35. package/src/helpers/error-parser.ts +0 -99
  36. package/src/helpers/objects.ts +0 -230
  37. package/src/index.test.data.credentials.ts +0 -71
  38. package/src/index.test.data.objects.ts +0 -235
  39. package/src/index.test.data.platform.ts +0 -45
  40. package/src/index.test.data.streams.ts +0 -470
  41. package/src/index.test.ts +0 -84
  42. package/src/types.ts +0 -23
  43. package/src/validator.ts +0 -79
@@ -1,49 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const activity_object_1 = __importDefault(require("./activity-object"));
7
- const objects_1 = require("../helpers/objects");
8
- const validActorRefs = activity_object_1.default.properties.object.oneOf;
9
- const validTargetRefs = activity_object_1.default.properties.object.oneOf;
10
- // eslint-disable-next-line security-node/detect-crlf
11
- console.log(validActorRefs);
12
- let validObjectRefs = [];
13
- objects_1.ObjectTypesList.forEach(function (type, i) {
14
- validObjectRefs.push({ "$ref": "#/definitions/type/" + type });
15
- });
16
- const contextSchema = {
17
- "type": "string"
18
- };
19
- const typeSchema = {
20
- "type": "string"
21
- };
22
- exports.default = {
23
- "$id": "https://sockethub.org/schemas/v0/activity-stream#",
24
- "description": "Schema for Sockethub Activity Streams",
25
- "type": "object",
26
- "required": ["context", "type", "actor"],
27
- "properties": {
28
- "id": {
29
- "type": "string"
30
- },
31
- "type": typeSchema,
32
- "context": contextSchema,
33
- "actor": {
34
- "type": "object",
35
- "oneOf": validActorRefs
36
- },
37
- "target": {
38
- "type": "object",
39
- "oneOf": validTargetRefs
40
- },
41
- "object": {
42
- "type": "object",
43
- "oneOf": validObjectRefs
44
- }
45
- },
46
- "definitions": {
47
- "type": objects_1.ObjectTypesSchema
48
- }
49
- };
@@ -1,41 +0,0 @@
1
- /**
2
- * This file is part of Sockethub.
3
- *
4
- * Developed by Nick Jennings (https://github.com/silverbucket)
5
- *
6
- * server is licensed under the LGPL.
7
- * See the LICENSE file for details.
8
- *
9
- * The latest version of server can be found here:
10
- * git://github.com/sockethub/sockethub.git
11
- *
12
- * For more information about Sockethub visit https://sockethub.org/.
13
- *
14
- * This program is distributed in the hope that it will be useful,
15
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
- */
18
- declare const _default: {
19
- type: string;
20
- required: string[];
21
- additionalProperties: boolean;
22
- properties: {
23
- credentials: {
24
- title: string;
25
- type: string;
26
- };
27
- messages: {
28
- title: string;
29
- type: string;
30
- };
31
- name: {
32
- title: string;
33
- type: string;
34
- };
35
- version: {
36
- title: string;
37
- type: string;
38
- };
39
- };
40
- };
41
- export default _default;
@@ -1,44 +0,0 @@
1
- "use strict";
2
- /**
3
- * This file is part of Sockethub.
4
- *
5
- * Developed by Nick Jennings (https://github.com/silverbucket)
6
- *
7
- * server is licensed under the LGPL.
8
- * See the LICENSE file for details.
9
- *
10
- * The latest version of server can be found here:
11
- * git://github.com/sockethub/sockethub.git
12
- *
13
- * For more information about Sockethub visit https://sockethub.org/.
14
- *
15
- * This program is distributed in the hope that it will be useful,
16
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18
- */
19
- Object.defineProperty(exports, "__esModule", { value: true });
20
- // this schema defines the general structure of the schema object which should
21
- // be returned from platforms.
22
- exports.default = {
23
- 'type': 'object',
24
- 'required': ['name', 'version', 'messages'],
25
- 'additionalProperties': false,
26
- 'properties': {
27
- 'credentials': {
28
- 'title': 'credentials',
29
- 'type': 'object'
30
- },
31
- 'messages': {
32
- 'title': 'messages',
33
- 'type': 'object',
34
- },
35
- 'name': {
36
- 'title': 'name',
37
- 'type': 'string',
38
- },
39
- 'version': {
40
- 'title': 'version',
41
- 'type': 'string',
42
- }
43
- }
44
- };
package/dist/types.d.ts DELETED
@@ -1,20 +0,0 @@
1
- export interface IActivityStream {
2
- type: string;
3
- context: string;
4
- actor: IActivityObjectActor;
5
- object?: IActivityObjectObject;
6
- target?: IActivityObjectActor;
7
- error?: string;
8
- sessionSecret?: string;
9
- }
10
- export interface IActivityObject {
11
- id?: string;
12
- type: string;
13
- }
14
- export interface IActivityObjectActor extends IActivityObject {
15
- id: string;
16
- name?: string;
17
- }
18
- export interface IActivityObjectObject extends IActivityObject {
19
- content?: any;
20
- }
package/dist/types.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,8 +0,0 @@
1
- import { Schema } from 'ajv';
2
- import { IActivityStream } from "./types";
3
- export declare function validateActivityObject(msg: IActivityStream): string;
4
- export declare function validateActivityStream(msg: IActivityStream): string;
5
- export declare function validateCredentials(msg: IActivityStream): string;
6
- export declare function validatePlatformSchema(schema: Schema): string;
7
- export declare function addPlatformSchema(schema: Schema, platform_type: string): void;
8
- export declare function getPlatformSchema(platform_type: string): import("ajv/dist/types").AnyValidateFunction<unknown>;
package/dist/validator.js DELETED
@@ -1,78 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getPlatformSchema = exports.addPlatformSchema = exports.validatePlatformSchema = exports.validateCredentials = exports.validateActivityStream = exports.validateActivityObject = void 0;
7
- const debug_1 = __importDefault(require("debug"));
8
- const ajv_1 = __importDefault(require("ajv"));
9
- const ajv_formats_draft2019_1 = __importDefault(require("ajv-formats-draft2019"));
10
- const error_parser_1 = __importDefault(require("./helpers/error-parser"));
11
- const platform_1 = __importDefault(require("./schemas/platform"));
12
- const activity_stream_1 = __importDefault(require("./schemas/activity-stream"));
13
- const activity_object_1 = __importDefault(require("./schemas/activity-object"));
14
- const log = (0, debug_1.default)('sockethub:schemas');
15
- const ajv = new ajv_1.default({ strictTypes: false, allErrors: true });
16
- (0, ajv_formats_draft2019_1.default)(ajv);
17
- const schemaURL = 'https://sockethub.org/schemas/v0';
18
- const schemas = {};
19
- schemas[`${schemaURL}/activity-stream`] = activity_stream_1.default;
20
- schemas[`${schemaURL}/activity-object`] = activity_object_1.default;
21
- for (let uri in schemas) {
22
- log(`registering schema ${uri}`);
23
- ajv.addSchema(schemas[uri], uri);
24
- }
25
- function handleValidation(schemaRef, msg, isObject = false) {
26
- const validator = ajv.getSchema(schemaRef);
27
- let result;
28
- if (isObject) {
29
- result = validator({ object: msg });
30
- }
31
- else {
32
- result = validator(msg);
33
- }
34
- if (!result) {
35
- return (0, error_parser_1.default)(msg, validator.errors);
36
- }
37
- return "";
38
- }
39
- function validateActivityObject(msg) {
40
- return handleValidation(`${schemaURL}/activity-object`, msg, true);
41
- }
42
- exports.validateActivityObject = validateActivityObject;
43
- function validateActivityStream(msg) {
44
- return handleValidation(`${schemaURL}/activity-stream`, msg);
45
- }
46
- exports.validateActivityStream = validateActivityStream;
47
- function validateCredentials(msg) {
48
- if (!msg.context) {
49
- return 'credential activity streams must have a context set';
50
- }
51
- if (msg.type !== 'credentials') {
52
- return 'credential activity streams must have credentials set as type';
53
- }
54
- return handleValidation(`${schemaURL}/context/${msg.context}/credentials`, msg);
55
- }
56
- exports.validateCredentials = validateCredentials;
57
- function validatePlatformSchema(schema) {
58
- const validate = ajv.compile(platform_1.default);
59
- // validate schema property
60
- const err = validate(schema);
61
- if (!err) {
62
- return `platform schema failed to validate: ` +
63
- `${validate.errors[0].instancePath} ${validate.errors[0].message}`;
64
- }
65
- else {
66
- return "";
67
- }
68
- }
69
- exports.validatePlatformSchema = validatePlatformSchema;
70
- function addPlatformSchema(schema, platform_type) {
71
- log(`registering schema ${schemaURL}/context/${platform_type}`);
72
- ajv.addSchema(schema, `${schemaURL}/context/${platform_type}`);
73
- }
74
- exports.addPlatformSchema = addPlatformSchema;
75
- function getPlatformSchema(platform_type) {
76
- return ajv.getSchema(`${schemaURL}/context/${platform_type}`);
77
- }
78
- exports.getPlatformSchema = getPlatformSchema;
@@ -1,99 +0,0 @@
1
- import {ErrorObject} from "ajv";
2
- import {ObjectTypesList} from "./objects";
3
- import {IActivityStream, IActivityObject} from "../types";
4
-
5
- interface TypeBreakdown {
6
- actor: Array<string>,
7
- object: Array<string>,
8
- target: Array<string>
9
- }
10
-
11
- function parseMsg(error: ErrorObject): string {
12
- let err = `${error.instancePath ? error.instancePath : 'activity stream'}: ${error.message}`;
13
- if (error.keyword === 'additionalProperties') {
14
- err += `: ${error.params.additionalProperty}`;
15
- } else if (error.keyword === 'enum') {
16
- err += `: ${error.params.allowedValues.join(', ')}`;
17
- }
18
- return err;
19
- }
20
-
21
- function getTypeList(msg: IActivityStream | IActivityObject): Array<string> {
22
- let types = [];
23
- if ((typeof msg === 'object') && (msg.type)) {
24
- types.push(msg.type);
25
- } else {
26
- types.push(undefined);
27
- }
28
- for (let prop in msg) {
29
- if ((typeof msg[prop] === 'object') && (msg[prop].type)) {
30
- types = [...types, ...getTypeList(msg[prop])];
31
- }
32
- }
33
- return types;
34
- }
35
-
36
- function getSchemaType(error: ErrorObject): string {
37
- const schemaTypeRes = error.schemaPath.match(/#\/\w+\/\w+\/([\w-]+)\//);
38
- return schemaTypeRes ? schemaTypeRes[1] : undefined;
39
- }
40
-
41
- function getErrType(error: ErrorObject): string {
42
- const errTypeRes = error.instancePath.match(/\/([\w]+)/);
43
- return errTypeRes ? errTypeRes[1] : undefined;
44
- }
45
-
46
- function getPartsCount(error: ErrorObject, types: TypeBreakdown): number {
47
- const schemaType = getSchemaType(error);
48
- const errType = getErrType(error);
49
- if (!errType) { return -1; }
50
- if (!types[errType].includes(schemaType)) { return -1; }
51
- const parts = error.instancePath.split('/');
52
- return parts.length;
53
- }
54
-
55
- function getTypes(msg: IActivityStream): TypeBreakdown {
56
- return {
57
- actor: getTypeList(msg.actor),
58
- target: getTypeList(msg.target),
59
- object: getTypeList(msg.context ? msg.object : msg)
60
- };
61
- }
62
-
63
- /**
64
- * Traverses the errors array from ajv, and makes a series of filtering decisions to
65
- * try to arrive at the most useful error.
66
- * @param msg
67
- * @param errors
68
- * @returns {string}
69
- */
70
- export default function getErrorMessage(msg, errors: Array<ErrorObject>): string {
71
- const types = getTypes(msg);
72
- let deepest_entry = 0, highest_depth = -1;
73
-
74
- for (let i = 0; i < errors.length; i++) {
75
- const partsCount = getPartsCount(errors[i], types);
76
- if (partsCount > highest_depth) {
77
- highest_depth = partsCount;
78
- deepest_entry = i;
79
- }
80
- }
81
-
82
- return highest_depth >= 0 ?
83
- parseMsg(errors[deepest_entry]) :
84
- composeFinalError(errors[errors.length - 1]);
85
- }
86
-
87
- function composeFinalError(error) {
88
- // if we have yet to build an error message, assume this is an invalid type value (oneOf),
89
- // try to build a list of valid types
90
- let msg = "";
91
- if (error.keyword === 'oneOf') {
92
- msg = `${error.instancePath}: ${error.message}: ` +
93
- `${ObjectTypesList.join(', ')}`;
94
- } else {
95
- msg = `${error.instancePath ?
96
- error.instancePath : 'activity stream'}: ${error.message}`;
97
- }
98
- return msg;
99
- }
@@ -1,230 +0,0 @@
1
- export const validObjectRefs = [];
2
- export const validObjectDefs = {};
3
-
4
- export const ObjectTypesSchema = {
5
-
6
- "credentials": {
7
- "required": [ "type" ],
8
- "additionalProperties": true,
9
- "properties": {
10
- "type": {
11
- "enum": [ "credentials" ]
12
- }
13
- }
14
- },
15
-
16
- "feed": {
17
- "required": [ "id", "type" ],
18
- "additionalProperties": true,
19
- "properties": {
20
- "type": {
21
- "enum": [ "feed" ]
22
- },
23
- "id": {
24
- "type": "string",
25
- "format": "iri"
26
- },
27
- "name": {
28
- "type": "string"
29
- },
30
- "description": {
31
- "type": "string"
32
- },
33
- "author": {
34
- "type": "string"
35
- },
36
- "favicon": {
37
- "type": "string"
38
- }
39
- }
40
- },
41
-
42
- "message": {
43
- "required": [ "type", "content" ],
44
- "additionalProperties": true,
45
- "properties": {
46
- "type": {
47
- "enum": [ "message" ]
48
- },
49
- "id": {
50
- "type": "string",
51
- },
52
- "name": {
53
- "type": "string"
54
- },
55
- "content": {
56
- "type": "string"
57
- }
58
- }
59
- },
60
-
61
- "me": {
62
- "required": ["type", "content"],
63
- "additionalProperties": true,
64
- "properties": {
65
- "type": {
66
- "enum": ["me"]
67
- },
68
- "content": {
69
- "type": "string"
70
- }
71
- }
72
- },
73
-
74
- "person": {
75
- "required": [ "id", "type" ],
76
- "additionalProperties": true,
77
- "properties": {
78
- "id": {
79
- "type": "string"
80
- },
81
- "type": {
82
- "enum": [ "person" ]
83
- },
84
- "name": {
85
- "type": "string"
86
- }
87
- }
88
- },
89
-
90
- "room": {
91
- "required": [ "id", "type" ],
92
- "additionalProperties": true,
93
- "properties": {
94
- "id": {
95
- "type": "string"
96
- },
97
- "type": {
98
- "enum": [ "room" ]
99
- },
100
- "name": {
101
- "type": "string"
102
- }
103
- }
104
- },
105
-
106
- "service": {
107
- "required": [ "id", "type" ],
108
- "additionalProperties": true,
109
- "properties": {
110
- "id": {
111
- "type": "string"
112
- },
113
- "type": {
114
- "enum": [ "service" ]
115
- },
116
- "name": {
117
- "type": "string"
118
- }
119
- }
120
- },
121
-
122
- "website": {
123
- "required": [ "id", "type" ],
124
- "additionalProperties": true,
125
- "properties": {
126
- "id": {
127
- "type": "string",
128
- "format": "iri"
129
- },
130
- "type": {
131
- "enum": [ "website" ]
132
- },
133
- "name": {
134
- "type": "string"
135
- }
136
- }
137
- },
138
-
139
- "attendance": {
140
- "required": [ "type" ],
141
- "additionalProperties": false,
142
- "properties": {
143
- "type": {
144
- "enum": [ "attendance" ]
145
- },
146
- "members": {
147
- "type": "array",
148
- "items": {
149
- "type": "string"
150
- }
151
- }
152
- }
153
- },
154
-
155
- "presence": {
156
- "required": [ "type" ],
157
- "additionalProperties": false,
158
- "properties": {
159
- "type": {
160
- "enum": [ "presence" ]
161
- },
162
- "presence": {
163
- "enum": [ "away", "chat", "dnd", "xa", "offline", "online" ]
164
- },
165
- "role": {
166
- "enum": [ "owner", "member", "participant", "admin" ]
167
- },
168
- "content": {
169
- "type": "string"
170
- }
171
- }
172
- },
173
-
174
- // inspired by https://www.w3.org/ns/activitystreams#Relationship
175
- "relationship": {
176
- "required": [ "type", "relationship" ],
177
- "additionalProperties": false,
178
- "properties": {
179
- "type": {
180
- "enum": [ "relationship" ]
181
- },
182
- "relationship": {
183
- "enum": [ "role" ]
184
- },
185
- "subject": {
186
- "type": "object",
187
- "oneOf": [
188
- { "$ref": "#/definitions/type/presence" }
189
- ]
190
- },
191
- "object": {
192
- "type": "object",
193
- "oneOf": validObjectRefs,
194
- }
195
- }
196
- },
197
-
198
- "topic": {
199
- "required": [ "type" ],
200
- "additionalProperties": false,
201
- "properties": {
202
- "type": {
203
- "enum": [ "topic" ]
204
- },
205
- "content": {
206
- "type": "string"
207
- }
208
- }
209
- },
210
-
211
- "address": {
212
- "required": [ "type" ],
213
- "additionalProperties": false,
214
- "properties": {
215
- "type": {
216
- "enum": [ "address" ]
217
- }
218
- }
219
- }
220
- };
221
-
222
- export const ObjectTypesList = Object.keys(ObjectTypesSchema);
223
-
224
- ObjectTypesList.forEach(function (type, i) {
225
- if (type === 'credentials') {
226
- return;
227
- }
228
- validObjectRefs.push({ "$ref": "#/definitions/type/" + type });
229
- validObjectDefs[type] = ObjectTypesSchema[type];
230
- });
@@ -1,71 +0,0 @@
1
- export default [
2
- [
3
- 'credentials with no context',
4
- {
5
- type: 'credentials',
6
- object: {
7
- type: 'credentials'
8
- }
9
- },
10
- false,
11
- `credential activity streams must have a context set`
12
- ],
13
-
14
- [
15
- 'credentials with no type',
16
- {
17
- context: 'test-platform',
18
- object: {
19
- type: 'credentials'
20
- }
21
- },
22
- false,
23
- `credential activity streams must have credentials set as type`
24
- ],
25
-
26
- [
27
- 'credentials with props',
28
- {
29
- context: 'test-platform',
30
- type: 'credentials',
31
- object: {
32
- type: 'credentials',
33
- user: "foo",
34
- pass: "bar"
35
- }
36
- },
37
- false,
38
- `/object: must NOT have additional properties`
39
- ],
40
-
41
- [
42
- 'credentials with props',
43
- {
44
- context: 'test-platform',
45
- type: 'credentials',
46
- object: {
47
- type: 'credentials',
48
- username: "foo",
49
- password: "bar"
50
- }
51
- },
52
- false,
53
- `/object: must have required property 'host'`
54
- ],
55
-
56
- [
57
- 'credentials with props',
58
- {
59
- context: 'test-platform',
60
- type: 'credentials',
61
- object: {
62
- type: 'credentials',
63
- username: "foo",
64
- password: "bar",
65
- host: 'yarg'
66
- }
67
- },
68
- true,
69
- ""
70
- ],
71
- ];