@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.
- package/LICENSE +22 -0
- package/README.md +11 -0
- package/dist/helpers/error-parser.d.ts +9 -0
- package/dist/helpers/error-parser.js +91 -0
- package/dist/helpers/objects.d.ts +206 -0
- package/dist/helpers/objects.js +217 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +22 -0
- package/dist/index.test.data.credentials.d.ts +37 -0
- package/dist/index.test.data.credentials.js +69 -0
- package/dist/index.test.data.objects.d.ts +50 -0
- package/dist/index.test.data.objects.js +214 -0
- package/dist/index.test.data.platform.d.ts +44 -0
- package/dist/index.test.data.platform.js +47 -0
- package/dist/index.test.data.streams.d.ts +256 -0
- package/dist/index.test.data.streams.js +449 -0
- package/dist/schemas/activity-object.d.ts +16 -0
- package/dist/schemas/activity-object.js +18 -0
- package/dist/schemas/activity-stream.d.ts +235 -0
- package/dist/schemas/activity-stream.js +49 -0
- package/dist/schemas/json/activity-object.json +356 -0
- package/dist/schemas/json/activity-stream.json +465 -0
- package/dist/schemas/json/platform.json +27 -0
- package/dist/schemas/platform.d.ts +41 -0
- package/dist/schemas/platform.js +44 -0
- package/dist/types.d.ts +20 -0
- package/dist/types.js +2 -0
- package/dist/validator.d.ts +8 -0
- package/dist/validator.js +78 -0
- package/package.json +58 -0
- package/src/helpers/error-parser.ts +99 -0
- package/src/helpers/objects.ts +230 -0
- package/src/index.test.data.credentials.ts +71 -0
- package/src/index.test.data.objects.ts +235 -0
- package/src/index.test.data.platform.ts +45 -0
- package/src/index.test.data.streams.ts +470 -0
- package/src/index.test.ts +84 -0
- package/src/index.ts +26 -0
- package/src/schemas/activity-object.ts +19 -0
- package/src/schemas/activity-stream.ts +51 -0
- package/src/schemas/platform.ts +47 -0
- package/src/types.ts +23 -0
- package/src/validator.ts +79 -0
|
@@ -0,0 +1,47 @@
|
|
|
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
|
+
|
|
19
|
+
// this schema defines the general structure of the schema object which should
|
|
20
|
+
// be returned from platforms.
|
|
21
|
+
export default {
|
|
22
|
+
'type' : 'object',
|
|
23
|
+
'required': [ 'name', 'version', 'messages' ],
|
|
24
|
+
'additionalProperties': false,
|
|
25
|
+
'properties' : {
|
|
26
|
+
|
|
27
|
+
'credentials' : {
|
|
28
|
+
'title': 'credentials',
|
|
29
|
+
'type': 'object'
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
'messages' : {
|
|
33
|
+
'title': 'messages',
|
|
34
|
+
'type': 'object',
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
'name' : {
|
|
38
|
+
'title': 'name',
|
|
39
|
+
'type': 'string',
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
'version' : {
|
|
43
|
+
'title': 'version',
|
|
44
|
+
'type': 'string',
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
|
|
11
|
+
export interface IActivityObject {
|
|
12
|
+
id?: string;
|
|
13
|
+
type: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface IActivityObjectActor extends IActivityObject {
|
|
17
|
+
id: string;
|
|
18
|
+
name?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface IActivityObjectObject extends IActivityObject {
|
|
22
|
+
content?: any;
|
|
23
|
+
}
|
package/src/validator.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import debug from 'debug';
|
|
2
|
+
import Ajv, {Schema} from 'ajv';
|
|
3
|
+
import ajvFormat2019 from 'ajv-formats-draft2019';
|
|
4
|
+
import getErrorMessage from "./helpers/error-parser";
|
|
5
|
+
import {IActivityStream} from "./types";
|
|
6
|
+
import PlatformSchema from './schemas/platform';
|
|
7
|
+
import ActivityStreamsSchema from './schemas/activity-stream';
|
|
8
|
+
import ActivityObjectSchema from './schemas/activity-object';
|
|
9
|
+
const log = debug('sockethub:schemas');
|
|
10
|
+
const ajv = new Ajv({strictTypes: false, allErrors: true});
|
|
11
|
+
ajvFormat2019(ajv);
|
|
12
|
+
|
|
13
|
+
interface SchemasDict {
|
|
14
|
+
string?: Schema
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const schemaURL = 'https://sockethub.org/schemas/v0';
|
|
18
|
+
const schemas: SchemasDict = {};
|
|
19
|
+
|
|
20
|
+
schemas[`${schemaURL}/activity-stream`] = ActivityStreamsSchema;
|
|
21
|
+
schemas[`${schemaURL}/activity-object`] = ActivityObjectSchema;
|
|
22
|
+
|
|
23
|
+
for (let uri in schemas) {
|
|
24
|
+
log(`registering schema ${uri}`);
|
|
25
|
+
ajv.addSchema(schemas[uri], uri);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function handleValidation(schemaRef: string, msg: IActivityStream, isObject=false): string {
|
|
29
|
+
const validator = ajv.getSchema(schemaRef);
|
|
30
|
+
let result: boolean | Promise<unknown>;
|
|
31
|
+
if (isObject) {
|
|
32
|
+
result = validator({ object: msg });
|
|
33
|
+
} else {
|
|
34
|
+
result = validator(msg);
|
|
35
|
+
}
|
|
36
|
+
if (! result) {
|
|
37
|
+
return getErrorMessage(msg, validator.errors);
|
|
38
|
+
}
|
|
39
|
+
return "";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function validateActivityObject(msg: IActivityStream): string {
|
|
43
|
+
return handleValidation(`${schemaURL}/activity-object`, msg, true);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function validateActivityStream(msg: IActivityStream): string {
|
|
47
|
+
return handleValidation(`${schemaURL}/activity-stream`, msg);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function validateCredentials(msg: IActivityStream): string {
|
|
51
|
+
if (!msg.context) {
|
|
52
|
+
return 'credential activity streams must have a context set';
|
|
53
|
+
}
|
|
54
|
+
if (msg.type !== 'credentials') {
|
|
55
|
+
return 'credential activity streams must have credentials set as type';
|
|
56
|
+
}
|
|
57
|
+
return handleValidation(`${schemaURL}/context/${msg.context}/credentials`, msg);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function validatePlatformSchema(schema: Schema): string {
|
|
61
|
+
const validate = ajv.compile(PlatformSchema);
|
|
62
|
+
// validate schema property
|
|
63
|
+
const err = validate(schema);
|
|
64
|
+
if (! err) {
|
|
65
|
+
return `platform schema failed to validate: ` +
|
|
66
|
+
`${validate.errors[0].instancePath} ${validate.errors[0].message}`;
|
|
67
|
+
} else {
|
|
68
|
+
return "";
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function addPlatformSchema(schema: Schema, platform_type: string) {
|
|
73
|
+
log(`registering schema ${schemaURL}/context/${platform_type}`);
|
|
74
|
+
ajv.addSchema(schema, `${schemaURL}/context/${platform_type}`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function getPlatformSchema(platform_type: string) {
|
|
78
|
+
return ajv.getSchema(`${schemaURL}/context/${platform_type}`);
|
|
79
|
+
}
|