@sockethub/schemas 3.0.0-alpha.12 → 3.0.0-alpha.13
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/dist/activity-stream-helper.d.ts +5 -0
- package/dist/activity-stream-helper.js +36 -0
- package/dist/activity-stream-helper.js.map +10 -0
- package/dist/context.d.ts +56 -0
- package/dist/context.js +20 -41
- package/dist/context.js.map +3 -3
- package/dist/helpers/error-parser.d.ts +13 -0
- package/dist/helpers/objects.d.ts +122 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +180 -218
- package/dist/index.js.map +12 -12
- package/dist/maps/context-map.json +2 -2
- package/dist/object-types.d.ts +2 -0
- package/dist/object-types.js +296 -0
- package/dist/object-types.js.map +12 -0
- package/dist/schemas/activity-stream.d.ts +161 -0
- package/dist/schemas/platform/feeds/v1/messages.schema.json +8 -17
- package/dist/schemas/platform/irc/v1/credentials.schema.json +71 -1
- package/dist/schemas/platform/irc/v1/messages.schema.json +84 -0
- package/dist/schemas/platform/metadata/v1/messages.schema.json +1 -16
- package/dist/schemas/platform/xmpp/v1/credentials.schema.json +2 -2
- package/dist/schemas/platform/xmpp/v1/messages.schema.json +108 -0
- package/dist/schemas/platform.d.ts +62 -0
- package/dist/schemas/sockethub-config.d.ts +261 -0
- package/{src/types.ts → dist/types.d.ts} +17 -45
- package/dist/validator.d.ts +26 -0
- package/package.json +13 -8
- package/src/context.ts +0 -125
- package/src/helpers/error-parser.ts +0 -160
- package/src/helpers/objects.ts +0 -283
- package/src/index.test.data.credentials.ts +0 -103
- package/src/index.test.data.objects.ts +0 -241
- package/src/index.test.data.platform.ts +0 -48
- package/src/index.test.data.streams.ts +0 -590
- package/src/index.test.ts +0 -127
- package/src/index.ts +0 -56
- package/src/schemas/activity-object.ts +0 -19
- package/src/schemas/activity-stream.ts +0 -75
- package/src/schemas/json/activity-object.json +0 -405
- package/src/schemas/json/activity-stream.json +0 -502
- package/src/schemas/json/platform.json +0 -48
- package/src/schemas/json/sockethub-config.json +0 -236
- package/src/schemas/platform.ts +0 -71
- package/src/schemas/sockethub-config.ts +0 -220
- package/src/types/ajv-formats-draft2019.d.ts +0 -5
- package/src/validator.ts +0 -207
package/src/validator.ts
DELETED
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
import Ajv, { type Schema } from "ajv";
|
|
2
|
-
import addFormats from "ajv-formats";
|
|
3
|
-
import additionsFormats2019 from "ajv-formats-draft2019";
|
|
4
|
-
import {
|
|
5
|
-
ERROR_PLATFORM_CONTEXT_URL,
|
|
6
|
-
ERROR_PLATFORM_ID,
|
|
7
|
-
getCredentialsSchemaIdByContextUrl,
|
|
8
|
-
getPlatformIdByContextUrl,
|
|
9
|
-
getSchemaIdByContextUrl,
|
|
10
|
-
INTERNAL_PLATFORM_CONTEXT_URL,
|
|
11
|
-
INTERNAL_PLATFORM_ID,
|
|
12
|
-
registerPlatformContext,
|
|
13
|
-
resolvePlatformContextUrl,
|
|
14
|
-
} from "./context.js";
|
|
15
|
-
import getErrorMessage, {
|
|
16
|
-
type ValidationErrorOptions,
|
|
17
|
-
} from "./helpers/error-parser.js";
|
|
18
|
-
import { ActivityObjectSchema } from "./schemas/activity-object.js";
|
|
19
|
-
import { ActivityStreamSchema } from "./schemas/activity-stream.js";
|
|
20
|
-
import { PlatformSchema } from "./schemas/platform.js";
|
|
21
|
-
import type { ActivityObject, ActivityStream } from "./types.js";
|
|
22
|
-
|
|
23
|
-
const ajv = new Ajv({ strictTypes: false, allErrors: true });
|
|
24
|
-
addFormats(ajv as unknown as Parameters<typeof addFormats>[0]);
|
|
25
|
-
additionsFormats2019(ajv as unknown as Ajv);
|
|
26
|
-
|
|
27
|
-
type SchemasDict = Record<string, Schema>;
|
|
28
|
-
|
|
29
|
-
const schemaURL = "https://sockethub.org/schemas/v0";
|
|
30
|
-
const schemas: SchemasDict = {};
|
|
31
|
-
let validationErrorOptions: ValidationErrorOptions = {};
|
|
32
|
-
let systemContextsRegistered = false;
|
|
33
|
-
|
|
34
|
-
schemas[`${schemaURL}/activity-stream`] = ActivityStreamSchema;
|
|
35
|
-
schemas[`${schemaURL}/activity-object`] = ActivityObjectSchema;
|
|
36
|
-
|
|
37
|
-
for (const uri in schemas) {
|
|
38
|
-
ajv.addSchema(schemas[uri], uri);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function registerSystemContext(platformId: string, contextUrl: string): void {
|
|
42
|
-
registerPlatformContext(
|
|
43
|
-
platformId,
|
|
44
|
-
contextUrl,
|
|
45
|
-
`${schemaURL}/context/${platformId}/messages`,
|
|
46
|
-
`${schemaURL}/context/${platformId}/credentials`,
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
const messageSchemaRef = `${schemaURL}/context/${platformId}/messages`;
|
|
50
|
-
if (!ajv.getSchema(messageSchemaRef)) {
|
|
51
|
-
ajv.addSchema({}, messageSchemaRef);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const credentialsSchemaRef = `${schemaURL}/context/${platformId}/credentials`;
|
|
55
|
-
if (!ajv.getSchema(credentialsSchemaRef)) {
|
|
56
|
-
ajv.addSchema({}, credentialsSchemaRef);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function registerSystemPlatformContexts(): void {
|
|
61
|
-
if (systemContextsRegistered) {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
registerSystemContext(ERROR_PLATFORM_ID, ERROR_PLATFORM_CONTEXT_URL);
|
|
65
|
-
registerSystemContext(INTERNAL_PLATFORM_ID, INTERNAL_PLATFORM_CONTEXT_URL);
|
|
66
|
-
systemContextsRegistered = true;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
registerSystemPlatformContexts();
|
|
70
|
-
|
|
71
|
-
function handleValidation(
|
|
72
|
-
schemaRef: string,
|
|
73
|
-
msg: ActivityStream | ActivityObject,
|
|
74
|
-
isObject = false,
|
|
75
|
-
): string {
|
|
76
|
-
const validator = ajv.getSchema(schemaRef);
|
|
77
|
-
if (!validator) {
|
|
78
|
-
return `schema ${schemaRef} not found`;
|
|
79
|
-
}
|
|
80
|
-
let result: boolean | Promise<unknown>;
|
|
81
|
-
if (isObject) {
|
|
82
|
-
result = validator({ object: msg });
|
|
83
|
-
} else {
|
|
84
|
-
result = validator(msg);
|
|
85
|
-
}
|
|
86
|
-
if (!result) {
|
|
87
|
-
let errorMessage = getErrorMessage(
|
|
88
|
-
msg,
|
|
89
|
-
validator.errors,
|
|
90
|
-
validationErrorOptions,
|
|
91
|
-
);
|
|
92
|
-
if (Array.isArray((msg as ActivityStream)["@context"])) {
|
|
93
|
-
const platformContextUrl = resolvePlatformContextUrl(
|
|
94
|
-
msg as ActivityStream,
|
|
95
|
-
);
|
|
96
|
-
if (platformContextUrl) {
|
|
97
|
-
const platformId =
|
|
98
|
-
getPlatformIdByContextUrl(platformContextUrl);
|
|
99
|
-
if (platformId) {
|
|
100
|
-
errorMessage = `[${platformId}] ${errorMessage}`;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
return errorMessage;
|
|
105
|
-
}
|
|
106
|
-
return "";
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export function setValidationErrorOptions(
|
|
110
|
-
options: ValidationErrorOptions,
|
|
111
|
-
): void {
|
|
112
|
-
validationErrorOptions = { ...validationErrorOptions, ...options };
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export function validateActivityObject(msg: ActivityObject): string {
|
|
116
|
-
return handleValidation(`${schemaURL}/activity-object`, msg, true);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export function validateActivityStream(msg: ActivityStream): string {
|
|
120
|
-
const streamValidationError = handleValidation(
|
|
121
|
-
`${schemaURL}/activity-stream`,
|
|
122
|
-
msg,
|
|
123
|
-
);
|
|
124
|
-
if (streamValidationError) {
|
|
125
|
-
return streamValidationError;
|
|
126
|
-
}
|
|
127
|
-
const platformContextUrl = resolvePlatformContextUrl(msg);
|
|
128
|
-
if (!platformContextUrl) {
|
|
129
|
-
return "activity streams must have exactly one registered platform context URL in @context";
|
|
130
|
-
}
|
|
131
|
-
const schemaId = getSchemaIdByContextUrl(platformContextUrl);
|
|
132
|
-
if (!schemaId) {
|
|
133
|
-
return `platform context URL ${platformContextUrl} does not have a registered message schema`;
|
|
134
|
-
}
|
|
135
|
-
return handleValidation(schemaId, msg);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function validateCredentials(msg: ActivityStream): string {
|
|
139
|
-
if (!Array.isArray(msg["@context"])) {
|
|
140
|
-
return "credential activity streams must have an @context set";
|
|
141
|
-
}
|
|
142
|
-
if (msg.type !== "credentials") {
|
|
143
|
-
return "credential activity streams must have credentials set as type";
|
|
144
|
-
}
|
|
145
|
-
const streamValidationError = handleValidation(
|
|
146
|
-
`${schemaURL}/activity-stream`,
|
|
147
|
-
msg,
|
|
148
|
-
);
|
|
149
|
-
if (streamValidationError) {
|
|
150
|
-
return streamValidationError;
|
|
151
|
-
}
|
|
152
|
-
const platformContextUrl = resolvePlatformContextUrl(msg);
|
|
153
|
-
if (!platformContextUrl) {
|
|
154
|
-
return "credential activity streams must have exactly one registered platform context URL in @context";
|
|
155
|
-
}
|
|
156
|
-
const credentialsSchemaId =
|
|
157
|
-
getCredentialsSchemaIdByContextUrl(platformContextUrl);
|
|
158
|
-
if (!credentialsSchemaId) {
|
|
159
|
-
return `platform context URL ${platformContextUrl} does not have a registered credentials schema`;
|
|
160
|
-
}
|
|
161
|
-
return handleValidation(credentialsSchemaId, msg);
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
export function validatePlatformSchema(schema: Schema): string {
|
|
165
|
-
const validate = ajv.compile(PlatformSchema);
|
|
166
|
-
// validate schema property
|
|
167
|
-
const err = validate(schema);
|
|
168
|
-
if (!err) {
|
|
169
|
-
return `platform schema failed to validate: ${validate.errors[0].instancePath} ${validate.errors[0].message}`;
|
|
170
|
-
}
|
|
171
|
-
return "";
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export function addPlatformSchema(schema: Schema, platform_type: string) {
|
|
175
|
-
const schemaRef = `${schemaURL}/context/${platform_type}`;
|
|
176
|
-
if (!ajv.getSchema(schemaRef)) {
|
|
177
|
-
ajv.addSchema(schema, schemaRef);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
export function getPlatformSchema(platform_type: string) {
|
|
182
|
-
return ajv.getSchema(`${schemaURL}/context/${platform_type}`);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export function addPlatformContext(
|
|
186
|
-
platformId: string,
|
|
187
|
-
platformContextUrl: string,
|
|
188
|
-
) {
|
|
189
|
-
registerPlatformContext(
|
|
190
|
-
platformId,
|
|
191
|
-
platformContextUrl,
|
|
192
|
-
`${schemaURL}/context/${platformId}/messages`,
|
|
193
|
-
`${schemaURL}/context/${platformId}/credentials`,
|
|
194
|
-
);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
export function resolvePlatformId(msg: ActivityStream): string | null {
|
|
198
|
-
const platformContextUrl = resolvePlatformContextUrl(msg);
|
|
199
|
-
if (platformContextUrl) {
|
|
200
|
-
const id = getPlatformIdByContextUrl(platformContextUrl);
|
|
201
|
-
if (id) return id;
|
|
202
|
-
}
|
|
203
|
-
if (typeof msg.platform === "string" && msg.platform) {
|
|
204
|
-
return msg.platform;
|
|
205
|
-
}
|
|
206
|
-
return null;
|
|
207
|
-
}
|