schemair 0.2.0 → 0.3.0
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/README.md +140 -39
- package/dist/authoring/index.d.cts +1 -1
- package/dist/authoring/index.d.ts +1 -1
- package/dist/authoring/index.js +1 -1
- package/dist/{authoring-D3V0iyTe.js → authoring-DyJNYs5o.js} +1 -1
- package/dist/compatibility/index.cjs +43 -0
- package/dist/compatibility/index.d.cts +2 -0
- package/dist/compatibility/index.d.ts +2 -0
- package/dist/compatibility/index.js +42 -0
- package/dist/definition/index.d.cts +2 -2
- package/dist/definition/index.d.ts +2 -2
- package/dist/definition/index.js +1 -1
- package/dist/{index-DDMwVy2I.d.cts → index-1sDuqu05.d.ts} +6 -1
- package/dist/index-BFSMT_2d.d.ts +53 -0
- package/dist/{index-DM5DSLLL.d.ts → index-BGz7L_Fq.d.cts} +4 -30
- package/dist/{index-bQy-C-1l.d.ts → index-BTtdqi_M.d.ts} +9 -9
- package/dist/index-BfbClf6R.d.cts +20 -0
- package/dist/{index-CQynt7ft.d.cts → index-CK7Is7vy.d.cts} +9 -9
- package/dist/index-CUGhtuwA.d.cts +53 -0
- package/dist/{index-DXWoNYFB.d.ts → index-CV8EZQy8.d.ts} +1 -1
- package/dist/index-CzNxjqVg.d.cts +47 -0
- package/dist/{index-CIim1dG7.d.cts → index-DGHMUZ5m.d.cts} +1 -1
- package/dist/{index-CBbd4hXJ.d.ts → index-DxvfPmw6.d.ts} +4 -11
- package/dist/index-T6nFhvGQ.d.ts +20 -0
- package/dist/{index-CNjYUKvu.d.cts → index-ba_52Yu9.d.cts} +4 -11
- package/dist/index-hrcM9oJe.d.ts +47 -0
- package/dist/{index-4aMF0cTE.d.ts → index-i7Au-FQ6.d.cts} +6 -1
- package/dist/{index-DhmHCGpT.d.cts → index-mu1znI1F.d.ts} +4 -30
- package/dist/index.cjs +10 -10
- package/dist/index.d.cts +9 -7
- package/dist/index.d.ts +9 -7
- package/dist/index.js +9 -7
- package/dist/integrations/json-schema/index.d.cts +1 -1
- package/dist/integrations/json-schema/index.d.ts +1 -1
- package/dist/integrations/json-schema/index.js +1 -1
- package/dist/integrations/standard-schema/index.cjs +4 -4
- package/dist/integrations/standard-schema/index.d.cts +5 -5
- package/dist/integrations/standard-schema/index.d.ts +5 -5
- package/dist/integrations/standard-schema/index.js +5 -5
- package/dist/payload/index.cjs +44 -81
- package/dist/payload/index.d.cts +2 -2
- package/dist/payload/index.d.ts +2 -2
- package/dist/payload/index.js +45 -81
- package/dist/relations/index.cjs +2 -5
- package/dist/relations/index.d.cts +2 -2
- package/dist/relations/index.d.ts +2 -2
- package/dist/relations/index.js +2 -2
- package/dist/{relations-Cymx4iTc.cjs → relations-NUVJkAG8.cjs} +11 -232
- package/dist/{relations-ysXDmi-Z.js → relations-QtnCRW9x.js} +13 -216
- package/dist/resolve/index.cjs +308 -0
- package/dist/resolve/index.d.cts +2 -0
- package/dist/resolve/index.d.ts +2 -0
- package/dist/resolve/index.js +305 -0
- package/dist/schema-ref-key-BHVFVfj1.cjs +26 -0
- package/dist/schema-ref-key-DR__tPr-.js +14 -0
- package/dist/spec/index.d.cts +2 -2
- package/dist/spec/index.d.ts +2 -2
- package/dist/spec/index.js +1 -1
- package/dist/standard/index.d.cts +2 -2
- package/dist/standard/index.d.ts +2 -2
- package/dist/standard/index.js +2 -2
- package/dist/type-interpreter/index.cjs +2 -6
- package/dist/type-interpreter/index.d.cts +2 -2
- package/dist/type-interpreter/index.d.ts +2 -2
- package/dist/type-interpreter/index.js +2 -2
- package/dist/{type-interpreter-DnrId8aM.js → type-interpreter-BA8gcLUU.js} +57 -280
- package/dist/{type-interpreter-OLt2W2yd.cjs → type-interpreter-D8xETgq1.cjs} +55 -302
- package/package.json +23 -3
- package/dist/index-BqfGAU1K.d.ts +0 -56
- package/dist/index-D3mfwaSO.d.cts +0 -56
- /package/dist/{chunk-BgLxgFZR.js → chunk-bFfwrPtJ.js} +0 -0
- /package/dist/{definition-DmYnDmQO.js → definition-BH_LzXGc.js} +0 -0
- /package/dist/{standard-DhKsxu9U.js → standard-BGMgbbEQ.js} +0 -0
package/dist/payload/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { f as validateSchemaNodeDefinition } from "../definition-
|
|
1
|
+
import { f as validateSchemaNodeDefinition } from "../definition-BH_LzXGc.js";
|
|
2
|
+
import { n as schemaRefKey, t as indexSchemaRefs } from "../schema-ref-key-DR__tPr-.js";
|
|
2
3
|
|
|
3
4
|
//#region src/payload/validator.ts
|
|
4
5
|
function hasOwn(value, key) {
|
|
@@ -38,22 +39,19 @@ function hasAccessorOrSymbol(value) {
|
|
|
38
39
|
function isPromiseLike(value) {
|
|
39
40
|
return (typeof value === "object" && value !== null || typeof value === "function") && typeof value.then === "function";
|
|
40
41
|
}
|
|
41
|
-
function chain(previous, next) {
|
|
42
|
-
return isPromiseLike(previous) ? Promise.resolve(previous).then(() => next()) : next();
|
|
43
|
-
}
|
|
44
42
|
var PayloadValidator = class {
|
|
45
43
|
issues = [];
|
|
46
44
|
activePayload = /* @__PURE__ */ new WeakSet();
|
|
47
45
|
activeRefs = /* @__PURE__ */ new Set();
|
|
46
|
+
refIndex;
|
|
48
47
|
rootSchema;
|
|
49
|
-
constructor(context, rootSchema
|
|
48
|
+
constructor(context, rootSchema) {
|
|
50
49
|
this.context = context;
|
|
51
|
-
this.allowAsync = allowAsync;
|
|
52
50
|
this.rootSchema = rootSchema;
|
|
51
|
+
this.refIndex = indexSchemaRefs(context.refs);
|
|
53
52
|
}
|
|
54
53
|
run(value) {
|
|
55
|
-
|
|
56
|
-
if (isPromiseLike(validation)) return Promise.resolve(validation).then(() => this.result());
|
|
54
|
+
this.validateNode(value, this.rootSchema, [], this.rootSchema);
|
|
57
55
|
return this.result();
|
|
58
56
|
}
|
|
59
57
|
result() {
|
|
@@ -93,35 +91,29 @@ var PayloadValidator = class {
|
|
|
93
91
|
}
|
|
94
92
|
validateNode(value, schema, path, root) {
|
|
95
93
|
switch (schema.kind) {
|
|
96
|
-
case "primitive":
|
|
94
|
+
case "primitive":
|
|
97
95
|
if (!this.matchesPrimitive(value, schema.name)) {
|
|
98
96
|
this.reject(path, "type", `expected ${schema.name}`);
|
|
99
97
|
return;
|
|
100
98
|
}
|
|
101
|
-
|
|
102
|
-
if ((schema.name === "string" || schema.name === "number") && schema.
|
|
103
|
-
|
|
104
|
-
return pending;
|
|
105
|
-
}
|
|
99
|
+
if ((schema.name === "string" || schema.name === "number") && schema.semanticPath) this.runSemantic(schema.semanticPath, value, schema, path, root);
|
|
100
|
+
if ((schema.name === "string" || schema.name === "number") && schema.constraints) this.runConstraints(schema.constraints, value, schema, path, root);
|
|
101
|
+
return;
|
|
106
102
|
case "literal":
|
|
107
103
|
if (!isLiteral(value) || !equalLiteral(value, schema.literal)) this.reject(path, "literal", "value does not match literal");
|
|
108
104
|
return;
|
|
109
105
|
case "record": return this.validateRecord(value, schema, path, root);
|
|
110
|
-
case "array":
|
|
106
|
+
case "array":
|
|
111
107
|
if (!isDenseArray(value)) {
|
|
112
108
|
this.reject(path, "array", "expected a dense array");
|
|
113
109
|
return;
|
|
114
110
|
}
|
|
115
111
|
if (!this.enterPayload(value, path)) return;
|
|
116
|
-
let
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
});
|
|
122
|
-
return pending;
|
|
123
|
-
}
|
|
124
|
-
case "tuple": {
|
|
112
|
+
for (let index = 0; index < value.length; index += 1) this.validateNode(value[index], schema.elementSchema, [...path, index], root);
|
|
113
|
+
this.leavePayload(value);
|
|
114
|
+
if (schema.constraints) this.runConstraints(schema.constraints, value, schema, path, root);
|
|
115
|
+
return;
|
|
116
|
+
case "tuple":
|
|
125
117
|
if (!isDenseArray(value)) {
|
|
126
118
|
this.reject(path, "tuple", "expected a dense array");
|
|
127
119
|
return;
|
|
@@ -131,15 +123,12 @@ var PayloadValidator = class {
|
|
|
131
123
|
return;
|
|
132
124
|
}
|
|
133
125
|
if (!this.enterPayload(value, path)) return;
|
|
134
|
-
let pending = void 0;
|
|
135
126
|
for (let index = 0; index < schema.elementSchemas.length; index += 1) {
|
|
136
127
|
const elementSchema = schema.elementSchemas[index];
|
|
137
|
-
if (elementSchema)
|
|
128
|
+
if (elementSchema) this.validateNode(value[index], elementSchema, [...path, index], root);
|
|
138
129
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
});
|
|
142
|
-
}
|
|
130
|
+
this.leavePayload(value);
|
|
131
|
+
return;
|
|
143
132
|
case "literalUnion":
|
|
144
133
|
if (!isLiteral(value) || !schema.values.some((candidate) => equalLiteral(candidate, value))) this.reject(path, "literal-union", "value is not one of the allowed literals");
|
|
145
134
|
return;
|
|
@@ -165,26 +154,23 @@ var PayloadValidator = class {
|
|
|
165
154
|
}
|
|
166
155
|
if (!this.enterPayload(value, path)) return;
|
|
167
156
|
const fields = new Map(schema.fields.map((field) => [field.key, field]));
|
|
168
|
-
let pending = void 0;
|
|
169
157
|
for (const field of schema.fields) {
|
|
170
158
|
if (!hasOwn(value, field.key)) {
|
|
171
159
|
if (field.required === true) this.reject([...path, field.key], "required", "required field is missing");
|
|
172
160
|
continue;
|
|
173
161
|
}
|
|
174
|
-
|
|
162
|
+
this.validateNode(value[field.key], field.schema, [...path, field.key], root);
|
|
175
163
|
}
|
|
176
164
|
for (const key of Object.keys(value)) {
|
|
177
165
|
if (fields.has(key)) continue;
|
|
178
166
|
if (!hasOwn(schema, "additionalFields")) this.reject([...path, key], "additional-field", "unknown field is not allowed");
|
|
179
167
|
else if (schema.additionalFields !== true) {
|
|
180
168
|
const additionalSchema = schema.additionalFields;
|
|
181
|
-
if (additionalSchema)
|
|
169
|
+
if (additionalSchema) this.validateNode(value[key], additionalSchema, [...path, key], root);
|
|
182
170
|
} else if (!this.isJsonValue(value[key], /* @__PURE__ */ new WeakSet())) this.reject([...path, key], "additional-value", "additional field must be JSON-compatible");
|
|
183
171
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
return schema.constraints ? this.runConstraints(schema.constraints, value, schema, path, root) : void 0;
|
|
187
|
-
});
|
|
172
|
+
this.leavePayload(value);
|
|
173
|
+
if (schema.constraints) this.runConstraints(schema.constraints, value, schema, path, root);
|
|
188
174
|
}
|
|
189
175
|
validateTaggedUnion(value, schema, path, root) {
|
|
190
176
|
if (!isPlainRecord(value) || hasAccessorOrSymbol(value)) {
|
|
@@ -207,15 +193,18 @@ var PayloadValidator = class {
|
|
|
207
193
|
return this.validateNode(value, variant.schema, path, root);
|
|
208
194
|
}
|
|
209
195
|
validateRef(value, schema, path, root) {
|
|
210
|
-
|
|
211
|
-
this.unsupported(path, "ref.resolver", "refPath requires an explicit resolver");
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
const refKey = schema.refPath.join("\0");
|
|
196
|
+
const refKey = schemaRefKey(schema.refPath);
|
|
215
197
|
if (this.activeRefs.has(refKey)) {
|
|
216
198
|
this.unknown(path, "ref.cycle", "refPath resolution cycle could not be determined");
|
|
217
199
|
return;
|
|
218
200
|
}
|
|
201
|
+
const materialized = this.refIndex.get(refKey);
|
|
202
|
+
if (materialized !== void 0) return this.handleResolution(materialized, value, path, root);
|
|
203
|
+
if (!this.context.resolveRef) {
|
|
204
|
+
if (this.context.refs !== void 0) this.unknown(path, "ref.unresolved", "refPath is not present in the resolved environment");
|
|
205
|
+
else this.unsupported(path, "ref.resolver", "refPath requires an explicit resolver");
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
219
208
|
this.activeRefs.add(refKey);
|
|
220
209
|
let resolved;
|
|
221
210
|
try {
|
|
@@ -231,19 +220,15 @@ var PayloadValidator = class {
|
|
|
231
220
|
return;
|
|
232
221
|
}
|
|
233
222
|
if (isPromiseLike(resolved)) {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
223
|
+
this.unsupported(path, "ref.async", "synchronous validation requires a synchronous ref resolver");
|
|
224
|
+
this.activeRefs.delete(refKey);
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
try {
|
|
228
|
+
this.handleResolution(resolved, value, path, root);
|
|
229
|
+
} finally {
|
|
230
|
+
this.activeRefs.delete(refKey);
|
|
242
231
|
}
|
|
243
|
-
const handled = this.handleResolution(resolved, value, path, root);
|
|
244
|
-
if (isPromiseLike(handled)) return Promise.resolve(handled).finally(() => this.activeRefs.delete(refKey));
|
|
245
|
-
this.activeRefs.delete(refKey);
|
|
246
|
-
return handled;
|
|
247
232
|
}
|
|
248
233
|
handleResolution(resolved, value, path, root) {
|
|
249
234
|
if (resolved === void 0) {
|
|
@@ -279,19 +264,17 @@ var PayloadValidator = class {
|
|
|
279
264
|
}), path, "semantic");
|
|
280
265
|
}
|
|
281
266
|
runConstraints(constraints, value, schema, path, root) {
|
|
282
|
-
let pending = void 0;
|
|
283
267
|
for (const constraint of constraints) {
|
|
284
268
|
if (!this.context.constraintProvider) {
|
|
285
269
|
this.unsupported(path, "constraint.provider", `constraint path ${constraint.constraintPath.join("/")} has no provider`);
|
|
286
270
|
continue;
|
|
287
271
|
}
|
|
288
|
-
|
|
272
|
+
this.applyProvider(() => this.context.constraintProvider(constraint.constraintPath, constraint.args, value, {
|
|
289
273
|
path,
|
|
290
274
|
schema,
|
|
291
275
|
rootSchema: root
|
|
292
|
-
}), path, "constraint")
|
|
276
|
+
}), path, "constraint");
|
|
293
277
|
}
|
|
294
|
-
return pending;
|
|
295
278
|
}
|
|
296
279
|
applyProvider(call, path, kind) {
|
|
297
280
|
let result;
|
|
@@ -302,13 +285,8 @@ var PayloadValidator = class {
|
|
|
302
285
|
return;
|
|
303
286
|
}
|
|
304
287
|
if (isPromiseLike(result)) {
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
return;
|
|
308
|
-
}
|
|
309
|
-
return Promise.resolve(result).then((decision) => this.applyDecision(decision, path, kind)).catch((error) => {
|
|
310
|
-
this.unknown(path, `${kind}.error`, `${kind} provider failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
311
|
-
});
|
|
288
|
+
this.unsupported(path, `${kind}.async`, `synchronous validation requires a synchronous ${kind} provider`);
|
|
289
|
+
return;
|
|
312
290
|
}
|
|
313
291
|
return this.applyDecision(result, path, kind);
|
|
314
292
|
}
|
|
@@ -368,22 +346,8 @@ function validateDefinition(schema) {
|
|
|
368
346
|
function validatePayload(value, schema, context = {}) {
|
|
369
347
|
const validated = validateDefinition(schema);
|
|
370
348
|
if ("status" in validated) return validated;
|
|
371
|
-
|
|
372
|
-
if (isPromiseLike(result)) return {
|
|
373
|
-
status: "unknown",
|
|
374
|
-
issues: [{
|
|
375
|
-
code: "payload.unknown.async",
|
|
376
|
-
path: [],
|
|
377
|
-
message: "synchronous payload validation produced an asynchronous result"
|
|
378
|
-
}]
|
|
379
|
-
};
|
|
380
|
-
return result;
|
|
381
|
-
}
|
|
382
|
-
async function validatePayloadAsync(value, schema, context = {}) {
|
|
383
|
-
const validated = validateDefinition(schema);
|
|
384
|
-
if ("status" in validated) return validated;
|
|
385
|
-
return Promise.resolve(new PayloadValidator(context, validated, true).run(value));
|
|
349
|
+
return new PayloadValidator(context, validated).run(value);
|
|
386
350
|
}
|
|
387
351
|
|
|
388
352
|
//#endregion
|
|
389
|
-
export { validatePayload
|
|
353
|
+
export { validatePayload };
|
package/dist/relations/index.cjs
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
const require_relations = require('../relations-
|
|
1
|
+
const require_relations = require('../relations-NUVJkAG8.cjs');
|
|
2
2
|
|
|
3
3
|
exports.checkOperationRelation = require_relations.checkOperationRelation;
|
|
4
|
-
exports.checkOperationRelationAsync = require_relations.checkOperationRelationAsync;
|
|
5
4
|
exports.checkSchemaAssignability = require_relations.checkSchemaAssignability;
|
|
6
|
-
exports.
|
|
7
|
-
exports.checkSchemaRelation = require_relations.checkSchemaRelation;
|
|
8
|
-
exports.checkSchemaRelationAsync = require_relations.checkSchemaRelationAsync;
|
|
5
|
+
exports.checkSchemaRelation = require_relations.checkSchemaRelation;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { a as RelationDiagnostic, c as RelationProviderDecision, d as RelationResult, f as RelationSide, h as checkSchemaRelation, i as RelationContext, l as RelationResolution, m as checkSchemaAssignability, n as OperationRelationDirection, o as RelationPath, p as RelationStatus, r as checkOperationRelation, s as RelationProviderContext, t as OperationRelationComponent, u as RelationResolutionContext } from "../index-ba_52Yu9.cjs";
|
|
2
|
+
export { OperationRelationComponent, OperationRelationDirection, RelationContext, RelationDiagnostic, RelationPath, RelationProviderContext, RelationProviderDecision, RelationResolution, RelationResolutionContext, RelationResult, RelationSide, RelationStatus, checkOperationRelation, checkSchemaAssignability, checkSchemaRelation };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { a as RelationDiagnostic, c as RelationProviderDecision, d as RelationResult, f as RelationSide, h as checkSchemaRelation, i as RelationContext, l as RelationResolution, m as checkSchemaAssignability, n as OperationRelationDirection, o as RelationPath, p as RelationStatus, r as checkOperationRelation, s as RelationProviderContext, t as OperationRelationComponent, u as RelationResolutionContext } from "../index-DxvfPmw6.js";
|
|
2
|
+
export { OperationRelationComponent, OperationRelationDirection, RelationContext, RelationDiagnostic, RelationPath, RelationProviderContext, RelationProviderDecision, RelationResolution, RelationResolutionContext, RelationResult, RelationSide, RelationStatus, checkOperationRelation, checkSchemaAssignability, checkSchemaRelation };
|
package/dist/relations/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as checkSchemaAssignability, r as checkSchemaRelation, t as checkOperationRelation } from "../relations-QtnCRW9x.js";
|
|
2
2
|
|
|
3
|
-
export { checkOperationRelation,
|
|
3
|
+
export { checkOperationRelation, checkSchemaAssignability, checkSchemaRelation };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const require_definition = require('./definition-BLLBpLKv.cjs');
|
|
2
|
+
const require_schema_ref_key = require('./schema-ref-key-BHVFVfj1.cjs');
|
|
2
3
|
|
|
3
4
|
//#region src/relations/validator.ts
|
|
4
5
|
function equalLiteral(left, right) {
|
|
@@ -17,11 +18,13 @@ var RelationChecker = class {
|
|
|
17
18
|
activePairs = /* @__PURE__ */ new Set();
|
|
18
19
|
activeRefs = /* @__PURE__ */ new Set();
|
|
19
20
|
ids = /* @__PURE__ */ new WeakMap();
|
|
21
|
+
refIndex;
|
|
20
22
|
nextId = 1;
|
|
21
23
|
constructor(context, rootSourceSchema, rootTargetSchema) {
|
|
22
24
|
this.context = context;
|
|
23
25
|
this.rootSourceSchema = rootSourceSchema;
|
|
24
26
|
this.rootTargetSchema = rootTargetSchema;
|
|
27
|
+
this.refIndex = require_schema_ref_key.indexSchemaRefs(context.refs);
|
|
25
28
|
}
|
|
26
29
|
run() {
|
|
27
30
|
const result = this.compare(this.rootSourceSchema, this.rootTargetSchema, [], this.rootSourceSchema, this.rootTargetSchema);
|
|
@@ -251,8 +254,14 @@ var RelationChecker = class {
|
|
|
251
254
|
}
|
|
252
255
|
}
|
|
253
256
|
compareRef(ref, other, path, rootSource, rootTarget, side) {
|
|
254
|
-
|
|
255
|
-
|
|
257
|
+
const materialized = this.refIndex.get(require_schema_ref_key.schemaRefKey(ref.refPath));
|
|
258
|
+
if (materialized !== void 0) {
|
|
259
|
+
const definition$1 = require_definition.validateSchemaNodeDefinition(materialized);
|
|
260
|
+
if (!definition$1.ok) return this.rejected(path, "ref.schema", "resolved ref environment contains an invalid schema");
|
|
261
|
+
return side === "source" ? this.compare(definition$1.node, other, path, rootSource, rootTarget) : this.compare(other, definition$1.node, path, rootSource, rootTarget);
|
|
262
|
+
}
|
|
263
|
+
if (!this.context.resolveRef) return this.context.refs === void 0 ? this.unknown(path, "ref.resolver", "refPath relation requires an explicit resolver") : this.unknown(path, "ref.unresolved", "refPath is not present in the resolved environment");
|
|
264
|
+
const key = `${side}:${require_schema_ref_key.schemaRefKey(ref.refPath)}`;
|
|
256
265
|
if (this.activeRefs.has(key)) return this.unknown(path, "ref.cycle", "refPath relation cycle could not be determined");
|
|
257
266
|
this.activeRefs.add(key);
|
|
258
267
|
let resolved;
|
|
@@ -364,210 +373,6 @@ function checkSchemaRelation(source, target, context = {}) {
|
|
|
364
373
|
return new RelationChecker(context, validSource, validTarget).run();
|
|
365
374
|
}
|
|
366
375
|
const checkSchemaAssignability = checkSchemaRelation;
|
|
367
|
-
async function resolveAsyncSchema(schema, context, active, path, side, rootSourceSchema, rootTargetSchema) {
|
|
368
|
-
if (schema.kind === "ref" && context.resolveRef) {
|
|
369
|
-
const key = schema.refPath.join("\0");
|
|
370
|
-
if (active.has(key)) return schema;
|
|
371
|
-
active.add(key);
|
|
372
|
-
let resolved;
|
|
373
|
-
try {
|
|
374
|
-
resolved = await context.resolveRef(schema.refPath, {
|
|
375
|
-
path,
|
|
376
|
-
side,
|
|
377
|
-
sourceSchema: side === "source" ? schema : rootSourceSchema,
|
|
378
|
-
targetSchema: side === "source" ? rootTargetSchema : schema,
|
|
379
|
-
rootSourceSchema,
|
|
380
|
-
rootTargetSchema,
|
|
381
|
-
refPath: schema.refPath
|
|
382
|
-
});
|
|
383
|
-
} catch {
|
|
384
|
-
return schema;
|
|
385
|
-
} finally {
|
|
386
|
-
active.delete(key);
|
|
387
|
-
}
|
|
388
|
-
if (resolved && !(typeof resolved === "object" && "status" in resolved && resolved.status !== "resolved")) {
|
|
389
|
-
const definition = require_definition.validateSchemaNodeDefinition(typeof resolved === "object" && "status" in resolved ? resolved.schema : resolved);
|
|
390
|
-
if (definition.ok) return resolveAsyncSchema(definition.node, context, new Set(active), path, side, rootSourceSchema, rootTargetSchema);
|
|
391
|
-
}
|
|
392
|
-
return schema;
|
|
393
|
-
}
|
|
394
|
-
switch (schema.kind) {
|
|
395
|
-
case "record": return {
|
|
396
|
-
...schema,
|
|
397
|
-
fields: await Promise.all(schema.fields.map(async (field) => ({
|
|
398
|
-
...field,
|
|
399
|
-
schema: await resolveAsyncSchema(field.schema, context, new Set(active), [
|
|
400
|
-
...path,
|
|
401
|
-
"fields",
|
|
402
|
-
field.key
|
|
403
|
-
], side, rootSourceSchema, rootTargetSchema)
|
|
404
|
-
}))),
|
|
405
|
-
...schema.additionalFields && schema.additionalFields !== true ? { additionalFields: await resolveAsyncSchema(schema.additionalFields, context, new Set(active), [...path, "additionalFields"], side, rootSourceSchema, rootTargetSchema) } : {}
|
|
406
|
-
};
|
|
407
|
-
case "array": return {
|
|
408
|
-
...schema,
|
|
409
|
-
elementSchema: await resolveAsyncSchema(schema.elementSchema, context, new Set(active), [...path, "elementSchema"], side, rootSourceSchema, rootTargetSchema)
|
|
410
|
-
};
|
|
411
|
-
case "tuple": return {
|
|
412
|
-
...schema,
|
|
413
|
-
elementSchemas: await Promise.all(schema.elementSchemas.map((item, index) => resolveAsyncSchema(item, context, new Set(active), [
|
|
414
|
-
...path,
|
|
415
|
-
"elementSchemas",
|
|
416
|
-
index
|
|
417
|
-
], side, rootSourceSchema, rootTargetSchema)))
|
|
418
|
-
};
|
|
419
|
-
case "nullable": return {
|
|
420
|
-
...schema,
|
|
421
|
-
value: await resolveAsyncSchema(schema.value, context, new Set(active), path, side, rootSourceSchema, rootTargetSchema)
|
|
422
|
-
};
|
|
423
|
-
case "taggedUnion": return {
|
|
424
|
-
...schema,
|
|
425
|
-
variants: await Promise.all(schema.variants.map(async (variant) => ({
|
|
426
|
-
...variant,
|
|
427
|
-
schema: await resolveAsyncSchema(variant.schema, context, new Set(active), [
|
|
428
|
-
...path,
|
|
429
|
-
"variants",
|
|
430
|
-
variant.key
|
|
431
|
-
], side, rootSourceSchema, rootTargetSchema)
|
|
432
|
-
})))
|
|
433
|
-
};
|
|
434
|
-
default: return schema;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
function collectRelationPairs(source, target, path = [], result = [], seen = /* @__PURE__ */ new Set()) {
|
|
438
|
-
const key = `${JSON.stringify(path)}:${JSON.stringify(source)}:${JSON.stringify(target)}`;
|
|
439
|
-
if (seen.has(key)) return result;
|
|
440
|
-
seen.add(key);
|
|
441
|
-
result.push({
|
|
442
|
-
source,
|
|
443
|
-
target,
|
|
444
|
-
path
|
|
445
|
-
});
|
|
446
|
-
if (source.kind === "record" && target.kind === "record") {
|
|
447
|
-
const targetFields = new Map(target.fields.map((field) => [field.key, field]));
|
|
448
|
-
for (const field of source.fields) {
|
|
449
|
-
const targetField = targetFields.get(field.key);
|
|
450
|
-
if (targetField) collectRelationPairs(field.schema, targetField.schema, [
|
|
451
|
-
...path,
|
|
452
|
-
"fields",
|
|
453
|
-
field.key
|
|
454
|
-
], result, seen);
|
|
455
|
-
}
|
|
456
|
-
} else if (source.kind === "array" && target.kind === "array") collectRelationPairs(source.elementSchema, target.elementSchema, [...path, "elementSchema"], result, seen);
|
|
457
|
-
else if (source.kind === "tuple" && target.kind === "tuple") for (let index = 0; index < Math.min(source.elementSchemas.length, target.elementSchemas.length); index += 1) collectRelationPairs(source.elementSchemas[index], target.elementSchemas[index], [
|
|
458
|
-
...path,
|
|
459
|
-
"elementSchemas",
|
|
460
|
-
index
|
|
461
|
-
], result, seen);
|
|
462
|
-
else if (source.kind === "nullable" && target.kind === "nullable") collectRelationPairs(source.value, target.value, path, result, seen);
|
|
463
|
-
else if (source.kind === "taggedUnion" && target.kind === "taggedUnion" && source.discriminator === target.discriminator) {
|
|
464
|
-
const targetVariants = new Map(target.variants.map((variant) => [variant.key, variant]));
|
|
465
|
-
for (const variant of source.variants) {
|
|
466
|
-
const targetVariant = targetVariants.get(variant.key);
|
|
467
|
-
if (targetVariant) collectRelationPairs(variant.schema, targetVariant.schema, [
|
|
468
|
-
...path,
|
|
469
|
-
"variants",
|
|
470
|
-
variant.key
|
|
471
|
-
], result, seen);
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
return result;
|
|
475
|
-
}
|
|
476
|
-
async function checkSchemaRelationAsync(source, target, context = {}) {
|
|
477
|
-
try {
|
|
478
|
-
const validSource = validateSchema(source, "source");
|
|
479
|
-
if ("status" in validSource) return validSource;
|
|
480
|
-
const validTarget = validateSchema(target, "target");
|
|
481
|
-
if ("status" in validTarget) return validTarget;
|
|
482
|
-
const resolvedSource = await resolveAsyncSchema(validSource, context, /* @__PURE__ */ new Set(), [], "source", validSource, validTarget);
|
|
483
|
-
const resolvedTarget = await resolveAsyncSchema(validTarget, context, /* @__PURE__ */ new Set(), [], "target", validSource, validTarget);
|
|
484
|
-
const pairs = collectRelationPairs(resolvedSource, resolvedTarget);
|
|
485
|
-
const semanticCache = /* @__PURE__ */ new Map();
|
|
486
|
-
const constraintCache = /* @__PURE__ */ new Map();
|
|
487
|
-
if (context.semanticProvider) for (const pair of pairs) {
|
|
488
|
-
const { source: left, target: right } = pair;
|
|
489
|
-
const sourceSemantic = left.kind === "primitive" && (left.name === "string" || left.name === "number") ? left.semanticPath : void 0;
|
|
490
|
-
const targetSemantic = right.kind === "primitive" && (right.name === "string" || right.name === "number") ? right.semanticPath : void 0;
|
|
491
|
-
if (right.kind !== "primitive" || right.name !== "string" && right.name !== "number" || sourceSemantic === void 0 && targetSemantic === void 0) continue;
|
|
492
|
-
const providerContext = {
|
|
493
|
-
path: pair.path,
|
|
494
|
-
side: "source",
|
|
495
|
-
sourceSchema: left,
|
|
496
|
-
targetSchema: right,
|
|
497
|
-
rootSourceSchema: resolvedSource,
|
|
498
|
-
rootTargetSchema: resolvedTarget
|
|
499
|
-
};
|
|
500
|
-
let decision;
|
|
501
|
-
try {
|
|
502
|
-
decision = await context.semanticProvider(sourceSemantic, targetSemantic, providerContext);
|
|
503
|
-
} catch (error) {
|
|
504
|
-
decision = {
|
|
505
|
-
status: "unknown",
|
|
506
|
-
message: `semantic provider failed: ${error instanceof Error ? error.message : String(error)}`
|
|
507
|
-
};
|
|
508
|
-
}
|
|
509
|
-
semanticCache.set(JSON.stringify([
|
|
510
|
-
pair.path,
|
|
511
|
-
sourceSemantic,
|
|
512
|
-
targetSemantic
|
|
513
|
-
]), decision);
|
|
514
|
-
}
|
|
515
|
-
if (context.constraintProvider) for (const pair of pairs) {
|
|
516
|
-
const { source: left, target: right } = pair;
|
|
517
|
-
if (left.kind === "primitive" || left.kind === "array" || left.kind === "record" || left.kind === "literal" || left.kind === "literalUnion") {
|
|
518
|
-
const literalToPrimitive = (left.kind === "literal" || left.kind === "literalUnion") && right.kind === "primitive";
|
|
519
|
-
if (right.kind !== left.kind && !literalToPrimitive) continue;
|
|
520
|
-
const sourceConstraints = "constraints" in left ? left.constraints : void 0;
|
|
521
|
-
const targetConstraints = "constraints" in right ? right.constraints : void 0;
|
|
522
|
-
if (sourceConstraints === void 0 && targetConstraints === void 0) continue;
|
|
523
|
-
const providerContext = {
|
|
524
|
-
path: pair.path,
|
|
525
|
-
side: "source",
|
|
526
|
-
sourceSchema: left,
|
|
527
|
-
targetSchema: right,
|
|
528
|
-
rootSourceSchema: resolvedSource,
|
|
529
|
-
rootTargetSchema: resolvedTarget
|
|
530
|
-
};
|
|
531
|
-
let decision;
|
|
532
|
-
try {
|
|
533
|
-
decision = await context.constraintProvider(sourceConstraints, targetConstraints, providerContext);
|
|
534
|
-
} catch (error) {
|
|
535
|
-
decision = {
|
|
536
|
-
status: "unknown",
|
|
537
|
-
message: `constraint provider failed: ${error instanceof Error ? error.message : String(error)}`
|
|
538
|
-
};
|
|
539
|
-
}
|
|
540
|
-
constraintCache.set(JSON.stringify([
|
|
541
|
-
pair.path,
|
|
542
|
-
sourceConstraints,
|
|
543
|
-
targetConstraints
|
|
544
|
-
]), decision);
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
return checkSchemaRelation(resolvedSource, resolvedTarget, {
|
|
548
|
-
semanticProvider: (sourcePath, targetPath, providerContext) => semanticCache.get(JSON.stringify([
|
|
549
|
-
providerContext.path,
|
|
550
|
-
sourcePath,
|
|
551
|
-
targetPath
|
|
552
|
-
])) ?? { status: "unknown" },
|
|
553
|
-
constraintProvider: (sourceConstraints, targetConstraints, providerContext) => constraintCache.get(JSON.stringify([
|
|
554
|
-
providerContext.path,
|
|
555
|
-
sourceConstraints,
|
|
556
|
-
targetConstraints
|
|
557
|
-
])) ?? { status: "unknown" }
|
|
558
|
-
});
|
|
559
|
-
} catch (error) {
|
|
560
|
-
return {
|
|
561
|
-
status: "unknown",
|
|
562
|
-
issues: [{
|
|
563
|
-
code: "relation.unknown.async",
|
|
564
|
-
path: [],
|
|
565
|
-
message: `async relation provider failed: ${error instanceof Error ? error.message : String(error)}`
|
|
566
|
-
}]
|
|
567
|
-
};
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
const checkSchemaAssignabilityAsync = checkSchemaRelationAsync;
|
|
571
376
|
|
|
572
377
|
//#endregion
|
|
573
378
|
//#region src/relations/operation.ts
|
|
@@ -701,14 +506,6 @@ function checkOperationRelation(source, target, context = {}) {
|
|
|
701
506
|
if (!Array.isArray(work)) return work;
|
|
702
507
|
return combine(work.map((item) => "result" in item ? item.result : prefix(checkSchemaRelation(item.source, item.target, context), item.path)));
|
|
703
508
|
}
|
|
704
|
-
/** Async companion with the same substitution rules and schema-local providers. */
|
|
705
|
-
async function checkOperationRelationAsync(source, target, context = {}) {
|
|
706
|
-
const work = prepare(source, target);
|
|
707
|
-
if (!Array.isArray(work)) return work;
|
|
708
|
-
const results = [];
|
|
709
|
-
for (const item of work) results.push("result" in item ? item.result : prefix(await checkSchemaRelationAsync(item.source, item.target, context), item.path));
|
|
710
|
-
return combine(results);
|
|
711
|
-
}
|
|
712
509
|
|
|
713
510
|
//#endregion
|
|
714
511
|
Object.defineProperty(exports, 'checkOperationRelation', {
|
|
@@ -717,33 +514,15 @@ Object.defineProperty(exports, 'checkOperationRelation', {
|
|
|
717
514
|
return checkOperationRelation;
|
|
718
515
|
}
|
|
719
516
|
});
|
|
720
|
-
Object.defineProperty(exports, 'checkOperationRelationAsync', {
|
|
721
|
-
enumerable: true,
|
|
722
|
-
get: function () {
|
|
723
|
-
return checkOperationRelationAsync;
|
|
724
|
-
}
|
|
725
|
-
});
|
|
726
517
|
Object.defineProperty(exports, 'checkSchemaAssignability', {
|
|
727
518
|
enumerable: true,
|
|
728
519
|
get: function () {
|
|
729
520
|
return checkSchemaAssignability;
|
|
730
521
|
}
|
|
731
522
|
});
|
|
732
|
-
Object.defineProperty(exports, 'checkSchemaAssignabilityAsync', {
|
|
733
|
-
enumerable: true,
|
|
734
|
-
get: function () {
|
|
735
|
-
return checkSchemaAssignabilityAsync;
|
|
736
|
-
}
|
|
737
|
-
});
|
|
738
523
|
Object.defineProperty(exports, 'checkSchemaRelation', {
|
|
739
524
|
enumerable: true,
|
|
740
525
|
get: function () {
|
|
741
526
|
return checkSchemaRelation;
|
|
742
527
|
}
|
|
743
|
-
});
|
|
744
|
-
Object.defineProperty(exports, 'checkSchemaRelationAsync', {
|
|
745
|
-
enumerable: true,
|
|
746
|
-
get: function () {
|
|
747
|
-
return checkSchemaRelationAsync;
|
|
748
|
-
}
|
|
749
528
|
});
|