@xlr-lib/xlr-sdk 0.1.1--canary.9.190
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/cjs/index.cjs +905 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/index.legacy-esm.js +874 -0
- package/dist/index.mjs +874 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +36 -0
- package/src/__tests__/__snapshots__/sdk.test.ts.snap +4443 -0
- package/src/__tests__/sdk.test.ts +303 -0
- package/src/__tests__/utils.test.ts +0 -0
- package/src/index.ts +4 -0
- package/src/registry/basic-registry.ts +82 -0
- package/src/registry/index.ts +2 -0
- package/src/registry/types.ts +28 -0
- package/src/sdk.ts +351 -0
- package/src/types.ts +45 -0
- package/src/utils.ts +191 -0
- package/src/validator.ts +542 -0
- package/types/index.d.ts +5 -0
- package/types/registry/basic-registry.d.ts +18 -0
- package/types/registry/index.d.ts +3 -0
- package/types/registry/types.d.ts +23 -0
- package/types/sdk.d.ts +98 -0
- package/types/types.d.ts +30 -0
- package/types/utils.d.ts +13 -0
- package/types/validator.d.ts +27 -0
package/src/validator.ts
ADDED
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
import type { Node } from "jsonc-parser";
|
|
2
|
+
import type {
|
|
3
|
+
ArrayType,
|
|
4
|
+
NamedType,
|
|
5
|
+
NodeType,
|
|
6
|
+
ObjectType,
|
|
7
|
+
OrType,
|
|
8
|
+
PrimitiveTypes,
|
|
9
|
+
RefType,
|
|
10
|
+
TemplateLiteralType,
|
|
11
|
+
} from "@xlr-lib/xlr";
|
|
12
|
+
import {
|
|
13
|
+
makePropertyMap,
|
|
14
|
+
resolveConditional,
|
|
15
|
+
isPrimitiveTypeNode,
|
|
16
|
+
resolveReferenceNode,
|
|
17
|
+
computeEffectiveObject,
|
|
18
|
+
} from "@xlr-lib/xlr-utils";
|
|
19
|
+
import { ValidationSeverity } from "./types";
|
|
20
|
+
import type { ValidationMessage } from "./types";
|
|
21
|
+
|
|
22
|
+
export interface XLRValidatorConfig {
|
|
23
|
+
/** URL mapping for supplemental documentation */
|
|
24
|
+
urlMapping?: Record<string, string>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const MAX_VALID_SHOWN = 20;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Validator for XLRs on JSON Nodes
|
|
31
|
+
*/
|
|
32
|
+
export class XLRValidator {
|
|
33
|
+
private config: XLRValidatorConfig;
|
|
34
|
+
private resolveType: (id: string) => NamedType<NodeType> | undefined;
|
|
35
|
+
private regexCache: Map<string, RegExp>;
|
|
36
|
+
|
|
37
|
+
constructor(
|
|
38
|
+
resolveType: (id: string) => NamedType<NodeType> | undefined,
|
|
39
|
+
config?: XLRValidatorConfig,
|
|
40
|
+
) {
|
|
41
|
+
this.config = config || {};
|
|
42
|
+
this.resolveType = resolveType;
|
|
43
|
+
this.regexCache = new Map();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Main entrypoint for validation */
|
|
47
|
+
public validateType(
|
|
48
|
+
rootNode: Node,
|
|
49
|
+
xlrNode: NodeType,
|
|
50
|
+
): Array<ValidationMessage> {
|
|
51
|
+
const validationIssues = new Array<ValidationMessage>();
|
|
52
|
+
if (xlrNode.type === "object") {
|
|
53
|
+
if (rootNode.type === "object") {
|
|
54
|
+
validationIssues.push(...this.validateObject(xlrNode, rootNode));
|
|
55
|
+
} else {
|
|
56
|
+
validationIssues.push({
|
|
57
|
+
type: "type",
|
|
58
|
+
node: rootNode,
|
|
59
|
+
message: `Expected an object but got an "${rootNode.type}"`,
|
|
60
|
+
severity: ValidationSeverity.Error,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
} else if (xlrNode.type === "array") {
|
|
64
|
+
if (rootNode.type === "array") {
|
|
65
|
+
validationIssues.push(...this.validateArray(rootNode, xlrNode));
|
|
66
|
+
} else {
|
|
67
|
+
validationIssues.push({
|
|
68
|
+
type: "type",
|
|
69
|
+
node: rootNode,
|
|
70
|
+
message: `Expected an array but got an "${rootNode.type}"`,
|
|
71
|
+
severity: ValidationSeverity.Error,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
} else if (xlrNode.type === "template") {
|
|
75
|
+
const error = this.validateTemplate(rootNode, xlrNode);
|
|
76
|
+
if (error) {
|
|
77
|
+
validationIssues.push(error);
|
|
78
|
+
}
|
|
79
|
+
} else if (xlrNode.type === "or") {
|
|
80
|
+
const potentialTypeErrors: Array<{
|
|
81
|
+
type: NodeType;
|
|
82
|
+
errors: Array<ValidationMessage>;
|
|
83
|
+
}> = [];
|
|
84
|
+
|
|
85
|
+
for (const potentialType of xlrNode.or) {
|
|
86
|
+
const potentialErrors = this.validateType(rootNode, potentialType);
|
|
87
|
+
|
|
88
|
+
if (potentialErrors.length === 0) {
|
|
89
|
+
return validationIssues;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
potentialTypeErrors.push({
|
|
93
|
+
type: potentialType,
|
|
94
|
+
errors: potentialErrors,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
let message: string;
|
|
99
|
+
const expectedTypes = xlrNode.or
|
|
100
|
+
.map((node) =>
|
|
101
|
+
isPrimitiveTypeNode(node)
|
|
102
|
+
? node.type
|
|
103
|
+
: (node.name ?? node.title ?? node.type ?? "<unnamed type>"),
|
|
104
|
+
)
|
|
105
|
+
.join(" | ");
|
|
106
|
+
|
|
107
|
+
if (xlrNode.name) {
|
|
108
|
+
message = `Does not match any of the expected types for type: '${xlrNode.name}'`;
|
|
109
|
+
} else if (xlrNode.title) {
|
|
110
|
+
message = `Does not match any of the expected types for property: '${xlrNode.title}'`;
|
|
111
|
+
} else {
|
|
112
|
+
message = `Does not match any of the types: ${expectedTypes}`;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const { infoMessage } = this.generateNestedTypesInfo(
|
|
116
|
+
potentialTypeErrors,
|
|
117
|
+
xlrNode,
|
|
118
|
+
rootNode,
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
validationIssues.push({
|
|
122
|
+
type: "value",
|
|
123
|
+
node: rootNode,
|
|
124
|
+
message: message.trim(),
|
|
125
|
+
severity: ValidationSeverity.Error,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
if (infoMessage) {
|
|
129
|
+
validationIssues.push({
|
|
130
|
+
type: "value",
|
|
131
|
+
node: rootNode,
|
|
132
|
+
message: infoMessage,
|
|
133
|
+
severity: ValidationSeverity.Info,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
} else if (xlrNode.type === "and") {
|
|
137
|
+
const effectiveType = {
|
|
138
|
+
...this.computeIntersectionType(xlrNode.and),
|
|
139
|
+
...(xlrNode.name ? { name: xlrNode.name } : {}),
|
|
140
|
+
};
|
|
141
|
+
validationIssues.push(...this.validateType(rootNode, effectiveType));
|
|
142
|
+
} else if (xlrNode.type === "record") {
|
|
143
|
+
rootNode.children?.forEach((child) => {
|
|
144
|
+
validationIssues.push(
|
|
145
|
+
...this.validateType(child.children?.[0] as Node, xlrNode.keyType),
|
|
146
|
+
);
|
|
147
|
+
validationIssues.push(
|
|
148
|
+
...this.validateType(child.children?.[1] as Node, xlrNode.valueType),
|
|
149
|
+
);
|
|
150
|
+
});
|
|
151
|
+
} else if (xlrNode.type === "ref") {
|
|
152
|
+
const refType = this.getRefType(xlrNode);
|
|
153
|
+
if (refType === undefined) {
|
|
154
|
+
validationIssues.push({
|
|
155
|
+
type: "unknown",
|
|
156
|
+
node: rootNode,
|
|
157
|
+
message: `Type "${xlrNode.ref}" is not defined in provided bundles`,
|
|
158
|
+
severity: ValidationSeverity.Error,
|
|
159
|
+
});
|
|
160
|
+
} else {
|
|
161
|
+
validationIssues.push(
|
|
162
|
+
...this.validateType(rootNode, refType as NamedType),
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
} else if (isPrimitiveTypeNode(xlrNode)) {
|
|
166
|
+
if (!this.validateLiteralType(xlrNode, rootNode)) {
|
|
167
|
+
if (
|
|
168
|
+
(xlrNode.type === "string" ||
|
|
169
|
+
xlrNode.type === "number" ||
|
|
170
|
+
xlrNode.type === "boolean") &&
|
|
171
|
+
xlrNode.const
|
|
172
|
+
) {
|
|
173
|
+
validationIssues.push({
|
|
174
|
+
type: "type",
|
|
175
|
+
node: rootNode.parent as Node,
|
|
176
|
+
message: `Expected "${xlrNode.const}" but got "${rootNode.value}"`,
|
|
177
|
+
expected: xlrNode.const,
|
|
178
|
+
severity: ValidationSeverity.Error,
|
|
179
|
+
});
|
|
180
|
+
} else {
|
|
181
|
+
validationIssues.push({
|
|
182
|
+
type: "type",
|
|
183
|
+
node: rootNode.parent as Node,
|
|
184
|
+
message: `Expected type "${xlrNode.type}" but got "${rootNode.type}"`,
|
|
185
|
+
expected: xlrNode.type,
|
|
186
|
+
severity: ValidationSeverity.Error,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
} else if (xlrNode.type === "conditional") {
|
|
191
|
+
// Resolve RefNodes in check conditions if needed
|
|
192
|
+
let { right, left } = xlrNode.check;
|
|
193
|
+
|
|
194
|
+
if (right.type === "ref") {
|
|
195
|
+
right = this.getRefType(right);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (left.type === "ref") {
|
|
199
|
+
left = this.getRefType(left);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const resolvedXLRNode = {
|
|
203
|
+
...xlrNode,
|
|
204
|
+
check: {
|
|
205
|
+
left,
|
|
206
|
+
right,
|
|
207
|
+
},
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const resolvedConditional = resolveConditional(resolvedXLRNode);
|
|
211
|
+
if (resolvedConditional === resolvedXLRNode) {
|
|
212
|
+
throw Error(
|
|
213
|
+
`Unable to resolve conditional type at runtime: ${xlrNode.name}`,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
validationIssues.push(
|
|
218
|
+
...this.validateType(rootNode, resolvedConditional),
|
|
219
|
+
);
|
|
220
|
+
} else {
|
|
221
|
+
throw Error(`Unknown type ${xlrNode.type}`);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return validationIssues;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private generateNestedTypesInfo(
|
|
228
|
+
potentialTypeErrors: Array<{
|
|
229
|
+
type: NodeType;
|
|
230
|
+
errors: Array<ValidationMessage>;
|
|
231
|
+
}>,
|
|
232
|
+
xlrNode: OrType,
|
|
233
|
+
rootNode: Node,
|
|
234
|
+
): { nestedTypesList: string; infoMessage?: string } {
|
|
235
|
+
const nestedTypes = new Set<string>();
|
|
236
|
+
|
|
237
|
+
// TODO: Create a recursive function that returns value or xlrNode info
|
|
238
|
+
// First, try to extract types from potential type errors
|
|
239
|
+
potentialTypeErrors.forEach((typeError) => {
|
|
240
|
+
if (typeError.type.type !== "template") {
|
|
241
|
+
typeError.errors.forEach((error) => {
|
|
242
|
+
if (error.type === "type" && error.expected) {
|
|
243
|
+
// Split by separate types if union
|
|
244
|
+
String(error.expected)
|
|
245
|
+
.split(" | ")
|
|
246
|
+
.forEach((val) => nestedTypes.add(val.trim()));
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
// If no types found from errors, try using type from xlrNode
|
|
253
|
+
if (nestedTypes.size === 0) {
|
|
254
|
+
xlrNode.or.forEach((type) => {
|
|
255
|
+
const typeName =
|
|
256
|
+
type.name ?? type.title ?? type.type ?? "<unnamed type>";
|
|
257
|
+
nestedTypes.add(typeName);
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const nestedTypesArray = [...nestedTypes];
|
|
262
|
+
|
|
263
|
+
// Display list of expected types as a union
|
|
264
|
+
let nestedTypesList =
|
|
265
|
+
nestedTypesArray.slice(0, MAX_VALID_SHOWN).join(" | ") +
|
|
266
|
+
(nestedTypesArray.length > MAX_VALID_SHOWN
|
|
267
|
+
? ` | +${
|
|
268
|
+
nestedTypesArray.length - MAX_VALID_SHOWN
|
|
269
|
+
} ... ${nestedTypesArray.pop()}`
|
|
270
|
+
: "");
|
|
271
|
+
|
|
272
|
+
// TODO: Be able to pass the validator's config to the SDK
|
|
273
|
+
const docsURL = this.config.urlMapping;
|
|
274
|
+
|
|
275
|
+
// Support passing in a URL for matching type
|
|
276
|
+
if (docsURL && xlrNode.name && docsURL[xlrNode.name]) {
|
|
277
|
+
nestedTypesList = docsURL[xlrNode.name];
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Support supplemental info message
|
|
281
|
+
let infoMessage;
|
|
282
|
+
|
|
283
|
+
if (rootNode.value !== undefined) {
|
|
284
|
+
infoMessage = `Got: ${rootNode.value} and expected: ${nestedTypesList}`;
|
|
285
|
+
} else if (nestedTypesList) {
|
|
286
|
+
infoMessage = `Expected: ${nestedTypesList}`;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return { nestedTypesList, infoMessage };
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
private validateTemplate(
|
|
293
|
+
node: Node,
|
|
294
|
+
xlrNode: TemplateLiteralType,
|
|
295
|
+
): ValidationMessage | undefined {
|
|
296
|
+
if (node.type !== "string") {
|
|
297
|
+
return {
|
|
298
|
+
type: "type",
|
|
299
|
+
node: node.parent as Node,
|
|
300
|
+
message: `Expected type "${xlrNode.type}" but got "${typeof node}"`,
|
|
301
|
+
expected: xlrNode.type,
|
|
302
|
+
severity: ValidationSeverity.Error,
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const regex = this.getRegex(xlrNode.format);
|
|
307
|
+
const valid = regex.exec(node.value);
|
|
308
|
+
if (!valid) {
|
|
309
|
+
return {
|
|
310
|
+
type: "value",
|
|
311
|
+
node: node.parent as Node,
|
|
312
|
+
message: `Does not match expected format: ${xlrNode.format}`,
|
|
313
|
+
expected: xlrNode.format,
|
|
314
|
+
severity: ValidationSeverity.Error,
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
private validateArray(rootNode: Node, xlrNode: ArrayType) {
|
|
320
|
+
const issues: Array<ValidationMessage> = [];
|
|
321
|
+
rootNode.children?.forEach((child) =>
|
|
322
|
+
issues.push(...this.validateType(child, xlrNode.elementType)),
|
|
323
|
+
);
|
|
324
|
+
return issues;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
private validateObject(xlrNode: ObjectType, node: Node) {
|
|
328
|
+
const issues: Array<ValidationMessage> = [];
|
|
329
|
+
const objectProps = makePropertyMap(node);
|
|
330
|
+
|
|
331
|
+
for (const prop in xlrNode.properties) {
|
|
332
|
+
const expectedType = xlrNode.properties[prop];
|
|
333
|
+
const valueNode = objectProps.get(prop);
|
|
334
|
+
if (expectedType.required && valueNode === undefined) {
|
|
335
|
+
issues.push({
|
|
336
|
+
type: "missing",
|
|
337
|
+
node,
|
|
338
|
+
message: `Property "${prop}" missing from type "${xlrNode.name}"`,
|
|
339
|
+
severity: ValidationSeverity.Error,
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (valueNode) {
|
|
344
|
+
issues.push(
|
|
345
|
+
...this.validateType(valueNode, expectedType.node as NamedType),
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// Check if unknown keys are allowed and if they are - do the violate the constraint
|
|
351
|
+
const extraKeys = Array.from(objectProps.keys()).filter(
|
|
352
|
+
(key) => xlrNode.properties[key] === undefined,
|
|
353
|
+
);
|
|
354
|
+
if (xlrNode.additionalProperties === false && extraKeys.length > 0) {
|
|
355
|
+
issues.push({
|
|
356
|
+
type: "value",
|
|
357
|
+
node,
|
|
358
|
+
message: `Unexpected properties on "${xlrNode.name}": ${extraKeys.join(
|
|
359
|
+
", ",
|
|
360
|
+
)}`,
|
|
361
|
+
severity: ValidationSeverity.Error,
|
|
362
|
+
});
|
|
363
|
+
} else {
|
|
364
|
+
issues.push(
|
|
365
|
+
...extraKeys.flatMap((key) =>
|
|
366
|
+
this.validateType(
|
|
367
|
+
objectProps.get(key) as Node,
|
|
368
|
+
xlrNode.additionalProperties as NodeType,
|
|
369
|
+
),
|
|
370
|
+
),
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
return issues;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
private validateLiteralType(expectedType: PrimitiveTypes, literalType: Node) {
|
|
378
|
+
switch (expectedType.type) {
|
|
379
|
+
case "boolean":
|
|
380
|
+
if (expectedType.const) {
|
|
381
|
+
return expectedType.const === literalType.value;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return typeof literalType.value === "boolean";
|
|
385
|
+
case "number":
|
|
386
|
+
if (expectedType.const) {
|
|
387
|
+
return expectedType.const === literalType.value;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
return typeof literalType.value === "number";
|
|
391
|
+
case "string":
|
|
392
|
+
if (expectedType.const) {
|
|
393
|
+
return expectedType.const === literalType.value;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
return typeof literalType.value === "string";
|
|
397
|
+
case "null":
|
|
398
|
+
return literalType.value === null;
|
|
399
|
+
case "never":
|
|
400
|
+
return literalType === undefined;
|
|
401
|
+
case "any":
|
|
402
|
+
return literalType !== undefined;
|
|
403
|
+
case "unknown":
|
|
404
|
+
return literalType !== undefined;
|
|
405
|
+
case "undefined":
|
|
406
|
+
return true;
|
|
407
|
+
default:
|
|
408
|
+
return false;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
public getRefType(ref: RefType): NodeType {
|
|
413
|
+
let refName = ref.ref;
|
|
414
|
+
if (refName.indexOf("<") > 0) {
|
|
415
|
+
[refName] = refName.split("<");
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const actualType = this.resolveType(refName);
|
|
419
|
+
if (!actualType) {
|
|
420
|
+
throw new Error(`Error: can't resolve type reference ${refName}`);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
return resolveReferenceNode(ref, actualType);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
private getRegex(expString: string): RegExp {
|
|
427
|
+
if (this.regexCache.has(expString)) {
|
|
428
|
+
return this.regexCache.get(expString) as RegExp;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const exp = new RegExp(expString);
|
|
432
|
+
this.regexCache.set(expString, exp);
|
|
433
|
+
return exp;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
public computeIntersectionType(types: Array<NodeType>): ObjectType | OrType {
|
|
437
|
+
let firstElement = types[0];
|
|
438
|
+
let effectiveType: ObjectType | OrType;
|
|
439
|
+
|
|
440
|
+
// Capture the original top-level type name if exists
|
|
441
|
+
const topLevelTypeName = types[0].name;
|
|
442
|
+
|
|
443
|
+
if (firstElement.type === "ref") {
|
|
444
|
+
firstElement = this.getRefType(firstElement);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
if (firstElement.type === "and") {
|
|
448
|
+
effectiveType = this.computeIntersectionType(firstElement.and);
|
|
449
|
+
} else if (firstElement.type === "record") {
|
|
450
|
+
effectiveType = {
|
|
451
|
+
type: "object",
|
|
452
|
+
properties: {},
|
|
453
|
+
additionalProperties: firstElement.valueType,
|
|
454
|
+
};
|
|
455
|
+
} else if (firstElement.type !== "or" && firstElement.type !== "object") {
|
|
456
|
+
throw new Error(
|
|
457
|
+
`Can't compute a union with a non-object type ${firstElement.type} (${firstElement.name})`,
|
|
458
|
+
);
|
|
459
|
+
} else {
|
|
460
|
+
effectiveType = firstElement;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
types.slice(1).forEach((type) => {
|
|
464
|
+
let typeToApply = type;
|
|
465
|
+
|
|
466
|
+
if (typeToApply.type === "record") {
|
|
467
|
+
typeToApply = {
|
|
468
|
+
type: "object",
|
|
469
|
+
properties: {},
|
|
470
|
+
additionalProperties: typeToApply.valueType,
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
if (type.type === "ref") {
|
|
475
|
+
typeToApply = this.getRefType(type);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
if (typeToApply.type === "and") {
|
|
479
|
+
typeToApply = this.computeIntersectionType([type, effectiveType]);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
if (typeToApply.type === "object") {
|
|
483
|
+
if (effectiveType.type === "object") {
|
|
484
|
+
effectiveType = computeEffectiveObject(effectiveType, typeToApply);
|
|
485
|
+
} else {
|
|
486
|
+
effectiveType = {
|
|
487
|
+
...effectiveType,
|
|
488
|
+
or: effectiveType.or.map((y) => {
|
|
489
|
+
const intersectedType = this.computeIntersectionType([
|
|
490
|
+
y,
|
|
491
|
+
typeToApply,
|
|
492
|
+
]);
|
|
493
|
+
|
|
494
|
+
// If the intersected type doesn't have a name, use the top-level type name
|
|
495
|
+
if (!intersectedType.name && topLevelTypeName) {
|
|
496
|
+
intersectedType.name = topLevelTypeName;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
return intersectedType;
|
|
500
|
+
}),
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
} else if (typeToApply.type === "or") {
|
|
504
|
+
if (effectiveType.type === "object") {
|
|
505
|
+
effectiveType = {
|
|
506
|
+
...typeToApply,
|
|
507
|
+
or: typeToApply.or.map((y) => {
|
|
508
|
+
const intersectedType = this.computeIntersectionType([
|
|
509
|
+
y,
|
|
510
|
+
effectiveType,
|
|
511
|
+
]);
|
|
512
|
+
|
|
513
|
+
// If the intersected type doesn't have a name, use the top-level type name
|
|
514
|
+
if (!intersectedType.name && topLevelTypeName) {
|
|
515
|
+
intersectedType.name = topLevelTypeName;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
return intersectedType;
|
|
519
|
+
}),
|
|
520
|
+
};
|
|
521
|
+
} else {
|
|
522
|
+
throw new Error("unimplemented operation or x or projection");
|
|
523
|
+
}
|
|
524
|
+
} else {
|
|
525
|
+
throw new Error(
|
|
526
|
+
`Can't compute a union with a non-object type ${typeToApply.type} (${typeToApply.name})`,
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
});
|
|
530
|
+
|
|
531
|
+
// If the final effective type is an or type and doesn't have a name, use the top-level type name
|
|
532
|
+
if (
|
|
533
|
+
effectiveType.type === "or" &&
|
|
534
|
+
!effectiveType.name &&
|
|
535
|
+
topLevelTypeName
|
|
536
|
+
) {
|
|
537
|
+
effectiveType.name = topLevelTypeName;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
return effectiveType;
|
|
541
|
+
}
|
|
542
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { NamedType, NodeType } from "@xlr-lib/xlr";
|
|
2
|
+
import type { XLRRegistry, Filters, TypeMetadata } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Basic example of a XLRs Registry
|
|
5
|
+
*/
|
|
6
|
+
export declare class BasicXLRRegistry implements XLRRegistry {
|
|
7
|
+
private typeMap;
|
|
8
|
+
private pluginMap;
|
|
9
|
+
private infoMap;
|
|
10
|
+
constructor();
|
|
11
|
+
/** Returns a copy of the XLR to guard against unexpected type modification */
|
|
12
|
+
get(id: string): NamedType<NodeType> | undefined;
|
|
13
|
+
add(type: NamedType<NodeType>, plugin: string, capability: string): void;
|
|
14
|
+
has(id: string): boolean;
|
|
15
|
+
list(filterArgs?: Filters): NamedType<NodeType>[];
|
|
16
|
+
info(id: string): TypeMetadata | undefined;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=basic-registry.d.ts.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { NamedType } from "@xlr-lib/xlr";
|
|
2
|
+
export interface Filters {
|
|
3
|
+
/** filter based on plugin name */
|
|
4
|
+
pluginFilter?: string | RegExp;
|
|
5
|
+
/** filter based on capability name */
|
|
6
|
+
capabilityFilter?: string | RegExp;
|
|
7
|
+
/** filter based on type name */
|
|
8
|
+
typeFilter?: string | RegExp;
|
|
9
|
+
}
|
|
10
|
+
export interface TypeMetadata {
|
|
11
|
+
/** The Plugin the Type comes from */
|
|
12
|
+
plugin: string;
|
|
13
|
+
/** The Capability of the Type */
|
|
14
|
+
capability: string;
|
|
15
|
+
}
|
|
16
|
+
export interface XLRRegistry {
|
|
17
|
+
get(id: string): NamedType | undefined;
|
|
18
|
+
add(type: NamedType, from: string, capability: string): void;
|
|
19
|
+
has(id: string): boolean;
|
|
20
|
+
list(filterArgs?: Filters): Array<NamedType>;
|
|
21
|
+
info(id: string): TypeMetadata | undefined;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=types.d.ts.map
|
package/types/sdk.d.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { NamedType, NodeType, TransformFunction, TSManifest } from "@xlr-lib/xlr";
|
|
2
|
+
import type { Node } from "jsonc-parser";
|
|
3
|
+
import type { XLRRegistry, Filters } from "./registry";
|
|
4
|
+
export interface GetTypeOptions {
|
|
5
|
+
/** Resolves `extends` fields in objects */
|
|
6
|
+
getRawType?: boolean;
|
|
7
|
+
/** Perform optimizations to resolve all references, type intersections, and conditionals */
|
|
8
|
+
optimize?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Abstraction for interfacing with XLRs making it more approachable to use without understanding the inner workings of the types and how they are packaged
|
|
12
|
+
*/
|
|
13
|
+
export declare class XLRSDK {
|
|
14
|
+
private registry;
|
|
15
|
+
private validator;
|
|
16
|
+
private computedNodeCache;
|
|
17
|
+
private externalTransformFunctions;
|
|
18
|
+
constructor(customRegistry?: XLRRegistry);
|
|
19
|
+
/**
|
|
20
|
+
* Loads definitions from a path on the filesystem
|
|
21
|
+
*
|
|
22
|
+
* @param inputPath - path to the directory to load (above the xlr folder)
|
|
23
|
+
* @param filters - Any filters to apply when loading the types (a positive match will omit)
|
|
24
|
+
* @param transforms - any transforms to apply to the types being loaded
|
|
25
|
+
*/
|
|
26
|
+
loadDefinitionsFromDisk(inputPath: string, filters?: Omit<Filters, "pluginFilter">, transforms?: Array<TransformFunction>): void;
|
|
27
|
+
/**
|
|
28
|
+
* Load definitions from a js/ts file in memory
|
|
29
|
+
*
|
|
30
|
+
* @param manifest - The imported XLR manifest module
|
|
31
|
+
* @param filters - Any filters to apply when loading the types (a positive match will omit)
|
|
32
|
+
* @param transforms - any transforms to apply to the types being loaded
|
|
33
|
+
*/
|
|
34
|
+
loadDefinitionsFromModule(manifest: TSManifest, filters?: Omit<Filters, "pluginFilter">, transforms?: Array<TransformFunction>): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Statically load transform function that should be applied to every XLR bundle that is imported
|
|
37
|
+
*/
|
|
38
|
+
addTransformFunction(name: string, fn: TransformFunction): void;
|
|
39
|
+
/**
|
|
40
|
+
* Remove any transform function loaded via the `addTransformFunction` method by name
|
|
41
|
+
*/
|
|
42
|
+
removeTransformFunction(name: string): void;
|
|
43
|
+
/**
|
|
44
|
+
* Returns a Type that has been previously loaded
|
|
45
|
+
*
|
|
46
|
+
* @param id - Type to retrieve
|
|
47
|
+
* @param options - `GetTypeOptions`
|
|
48
|
+
* @returns `NamedType<NodeType>` | `undefined`
|
|
49
|
+
*/
|
|
50
|
+
getType(id: string, options?: GetTypeOptions): NamedType<NodeType> | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Returns if a Type with `id` has been loaded into the DSK
|
|
53
|
+
*
|
|
54
|
+
* @param id - Type to retrieve
|
|
55
|
+
* @returns `boolean`
|
|
56
|
+
*/
|
|
57
|
+
hasType(id: string): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Lists types that have been loaded into the SDK
|
|
60
|
+
*
|
|
61
|
+
* @param filters - Any filters to apply to the types returned (a positive match will omit)
|
|
62
|
+
* @returns `Array<NamedTypes>`
|
|
63
|
+
*/
|
|
64
|
+
listTypes(filters?: Filters): NamedType[];
|
|
65
|
+
/**
|
|
66
|
+
* Returns meta information around a registered type
|
|
67
|
+
*
|
|
68
|
+
* @param id - Name of Type to retrieve
|
|
69
|
+
* @returns `TypeMetaData` | `undefined`
|
|
70
|
+
*/
|
|
71
|
+
getTypeInfo(id: string): import("./registry").TypeMetadata | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* Validates if a JSONC Node follows the XLR Type registered under the `typeName` specified
|
|
74
|
+
*
|
|
75
|
+
* @param typeName - Registered XLR Type to use for validation
|
|
76
|
+
* @param rootNode - Node to validate
|
|
77
|
+
* @returns `Array<ValidationErrors>`
|
|
78
|
+
*/
|
|
79
|
+
validateByName(typeName: string, rootNode: Node): import("./types").ValidationMessage[];
|
|
80
|
+
/**
|
|
81
|
+
* Validates if a JSONC Node follows the supplied XLR Type
|
|
82
|
+
*
|
|
83
|
+
* @param type - Type to validate against
|
|
84
|
+
* @param rootNode - Node to validate
|
|
85
|
+
* @returns `Array<ValidationErrors>`
|
|
86
|
+
*/
|
|
87
|
+
validateByType(type: NodeType, rootNode: Node): import("./types").ValidationMessage[];
|
|
88
|
+
/**
|
|
89
|
+
* Transforms a generated XLR node into its final representation by resolving all `extends` properties.
|
|
90
|
+
* If `optimize` is set to true the following operations are also performed:
|
|
91
|
+
* - Solving any conditional types
|
|
92
|
+
* - Computing the effective types of any union elements
|
|
93
|
+
* - Resolving any ref nodes
|
|
94
|
+
* - filing in any remaining generics with their default value
|
|
95
|
+
*/
|
|
96
|
+
private resolveType;
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=sdk.d.ts.map
|