@agentshouse/mdmodel 0.1.0-c0bdbbc92d020 → 0.1.0-c1b026eddaa7c
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/API.md +299 -211
- package/README.md +21 -22
- package/dist/blocks.d.ts.map +1 -1
- package/dist/blocks.js +53 -28
- package/dist/definition.d.ts +25 -1
- package/dist/definition.d.ts.map +1 -1
- package/dist/definition.js +378 -49
- package/dist/divide.d.ts.map +1 -1
- package/dist/divide.js +27 -17
- package/dist/document.d.ts +10 -10
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +366 -174
- package/dist/event.d.ts +78 -0
- package/dist/event.d.ts.map +1 -0
- package/dist/event.js +201 -0
- package/dist/facts.d.ts +15 -0
- package/dist/facts.d.ts.map +1 -0
- package/dist/facts.js +38 -0
- package/dist/fields.d.ts +2 -0
- package/dist/fields.d.ts.map +1 -1
- package/dist/fields.js +7 -0
- package/dist/index.d.ts +28 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -6
- package/dist/kinds.d.ts +19 -0
- package/dist/kinds.d.ts.map +1 -0
- package/dist/kinds.js +161 -0
- package/dist/links.d.ts +3 -0
- package/dist/links.d.ts.map +1 -1
- package/dist/links.js +113 -66
- package/dist/paths.d.ts +10 -1
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +118 -12
- package/dist/pattern.d.ts +10 -0
- package/dist/pattern.d.ts.map +1 -0
- package/dist/pattern.js +356 -0
- package/dist/project.d.ts +1 -0
- package/dist/project.d.ts.map +1 -1
- package/dist/project.js +36 -15
- package/dist/query/language.d.ts +254 -0
- package/dist/query/language.d.ts.map +1 -0
- package/dist/query/language.js +620 -0
- package/dist/query/match.d.ts +14 -0
- package/dist/query/match.d.ts.map +1 -0
- package/dist/query/match.js +150 -0
- package/dist/query/rows.d.ts +32 -0
- package/dist/query/rows.d.ts.map +1 -0
- package/dist/query/rows.js +161 -0
- package/dist/query/sql.d.ts +39 -0
- package/dist/query/sql.d.ts.map +1 -0
- package/dist/query/sql.js +285 -0
- package/dist/references.d.ts +2 -2
- package/dist/references.d.ts.map +1 -1
- package/dist/references.js +6 -1
- package/dist/reserved.d.ts +5 -0
- package/dist/reserved.d.ts.map +1 -0
- package/dist/reserved.js +20 -0
- package/dist/schema.d.ts +1 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +57 -6
- package/dist/types.d.ts +68 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/validate.d.ts +32 -2
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +438 -126
- package/dist/yaml.d.ts +7 -3
- package/dist/yaml.d.ts.map +1 -1
- package/dist/yaml.js +110 -55
- package/fixtures/README.md +27 -7
- package/fixtures/cases/conditional-section/context.json +1 -0
- package/fixtures/cases/conditional-section/expected.json +6 -0
- package/fixtures/cases/conditional-section/library/questions/hosting.md +9 -0
- package/fixtures/cases/conditional-section/library/questions/payments.md +12 -0
- package/fixtures/cases/conditional-section/schema.yaml +14 -0
- package/fixtures/cases/cross-room-link/context.json +1 -0
- package/fixtures/cases/cross-room-link/expected.json +1 -0
- package/fixtures/cases/cross-room-link/library/clients/acme.md +6 -0
- package/fixtures/cases/cross-room-link/schema.yaml +3 -0
- package/fixtures/cases/cross-room-reference/context.json +1 -0
- package/fixtures/cases/cross-room-reference/expected.json +1 -0
- package/fixtures/cases/cross-room-reference/library/clients/acme.md +9 -0
- package/fixtures/cases/cross-room-reference/schema.yaml +9 -0
- package/fixtures/cases/link-into-a-reserved-name/context.json +1 -0
- package/fixtures/cases/link-into-a-reserved-name/expected.json +5 -0
- package/fixtures/cases/link-into-a-reserved-name/library/notes/a.md +6 -0
- package/fixtures/cases/link-into-a-reserved-name/schema.yaml +3 -0
- package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/context.json +1 -0
- package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/expected.json +5 -0
- package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/library/notes/b.md +5 -0
- package/fixtures/cases/member-on-an-untyped-document/context.json +1 -0
- package/fixtures/cases/member-on-an-untyped-document/expected.json +6 -0
- package/fixtures/cases/member-on-an-untyped-document/library/note.md +4 -0
- package/fixtures/cases/member-on-another-type/context.json +1 -0
- package/fixtures/cases/member-on-another-type/expected.json +6 -0
- package/fixtures/cases/member-on-another-type/library/note.md +4 -0
- package/fixtures/cases/member-on-another-type/schema.yaml +7 -0
- package/fixtures/cases/reference-into-a-reserved-name/context.json +1 -0
- package/fixtures/cases/reference-into-a-reserved-name/expected.json +6 -0
- package/fixtures/cases/reference-into-a-reserved-name/library/notes/a.md +4 -0
- package/fixtures/cases/reference-into-a-reserved-name/library/settings/brief.md +1 -0
- package/fixtures/cases/reference-into-a-reserved-name/schema.yaml +3 -0
- package/fixtures/cases/room-md-declares-a-type/context.json +1 -0
- package/fixtures/cases/room-md-declares-a-type/expected.json +6 -0
- package/fixtures/cases/room-md-declares-a-type/library/ROOM.md +5 -0
- package/fixtures/cases/typed-path-names-another-type/context.json +11 -0
- package/fixtures/cases/typed-path-names-another-type/expected.json +6 -0
- package/fixtures/cases/typed-path-names-another-type/library/settings/prefs/one.md +3 -0
- package/fixtures/cases/typed-path-names-another-type/schema.yaml +6 -0
- package/fixtures/cases/typed-path-names-its-type/context.json +11 -0
- package/fixtures/cases/typed-path-names-its-type/expected.json +3 -0
- package/fixtures/cases/typed-path-names-its-type/library/settings/prefs/deeper/two.md +1 -0
- package/fixtures/cases/typed-path-names-its-type/library/settings/prefs/one.md +4 -0
- package/fixtures/cases/typed-path-names-its-type/schema.yaml +6 -0
- package/fixtures/cases/typed-path-names-no-type/context.json +11 -0
- package/fixtures/cases/typed-path-names-no-type/expected.json +6 -0
- package/fixtures/cases/typed-path-names-no-type/library/settings/prefs/one.md +1 -0
- package/fixtures/cases/typed-path-names-no-type/schema.yaml +6 -0
- package/fixtures/cases/unnamed-typed-path-checks-its-fields/context.json +11 -0
- package/fixtures/cases/unnamed-typed-path-checks-its-fields/expected.json +6 -0
- package/fixtures/cases/unnamed-typed-path-checks-its-fields/library/rooms/team/tasks/plan.md +4 -0
- package/fixtures/cases/unnamed-typed-path-checks-its-fields/schema.yaml +5 -0
- package/fixtures/cases/unnamed-typed-path-names-a-type/context.json +11 -0
- package/fixtures/cases/unnamed-typed-path-names-a-type/expected.json +6 -0
- package/fixtures/cases/unnamed-typed-path-names-a-type/library/rooms/team/tasks/plan.md +6 -0
- package/fixtures/cases/unnamed-typed-path-names-a-type/schema.yaml +5 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/context.json +11 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/expected.json +3 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/library/rooms/team/plan.md +1 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/library/rooms/team/tasks/plan.md +5 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/schema.yaml +5 -0
- package/fixtures/cases/unnamed-typed-path-without-a-body/context.json +11 -0
- package/fixtures/cases/unnamed-typed-path-without-a-body/expected.json +5 -0
- package/fixtures/cases/unnamed-typed-path-without-a-body/library/rooms/team/tasks/plan.md +5 -0
- package/fixtures/cases/unnamed-typed-path-without-a-body/schema.yaml +5 -0
- package/fixtures/division.json +26 -26
- package/fixtures/queries.json +964 -0
- package/package.json +10 -3
- package/dist/condition.d.ts +0 -137
- package/dist/condition.d.ts.map +0 -1
- package/dist/condition.js +0 -394
package/dist/definition.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { MEMBER_FIELD, RECEIVED_FIELD, SITE_FIELD } from './fields.js';
|
|
1
|
+
import { MEMBER_FIELD, RECEIVED_FIELD, SITE_FIELD, TYPE_FIELD } from './fields.js';
|
|
2
|
+
import { linearPattern } from './pattern.js';
|
|
2
3
|
import { equalValue, isRecord, jsValue, parseYamlMapping, YamlError } from './yaml.js';
|
|
3
4
|
export const JSON_TYPES = [
|
|
4
5
|
'string',
|
|
@@ -12,16 +13,32 @@ export const JSON_TYPES = [
|
|
|
12
13
|
export const BUILT_IN_TYPE = 'any';
|
|
13
14
|
export const ATTACHMENT = 'attachment';
|
|
14
15
|
const SCALAR_TYPES = new Set(['string', 'number', 'integer', 'boolean']);
|
|
16
|
+
const COMPOSITES = ['array', 'object'];
|
|
17
|
+
const ANNOTATIONS = new Set([
|
|
18
|
+
'$comment',
|
|
19
|
+
'title',
|
|
20
|
+
'description',
|
|
21
|
+
'default',
|
|
22
|
+
'examples',
|
|
23
|
+
'deprecated',
|
|
24
|
+
'readOnly',
|
|
25
|
+
'writeOnly',
|
|
26
|
+
]);
|
|
27
|
+
const FIELD_RULES = ['change_roles', 'initial_roles', 'transitions'];
|
|
28
|
+
const PROVIDED_KEYWORDS = ['create_roles', 'delete_roles'];
|
|
29
|
+
const NO_TYPES = Object.freeze({ types: Object.freeze({}) });
|
|
15
30
|
const DEFS_REFERENCE = '#/$defs/';
|
|
31
|
+
const UNBOUNDED_FORMAT = 'url';
|
|
16
32
|
const UNRESOLVED = /[\u0000-\u001f\u007f%[\]\\^{|}]|\s$/u;
|
|
17
33
|
export const ARRAY_INDEX = /^(?:0|[1-9][0-9]*)$/u;
|
|
34
|
+
export const JSON_POINTER = /^(?:\/(?:[^~/]|~[01])*)*$/u;
|
|
18
35
|
const HOUSE_KEYS = {
|
|
19
|
-
|
|
36
|
+
[TYPE_FIELD]: { prose: false },
|
|
20
37
|
[SITE_FIELD]: { prose: false },
|
|
21
38
|
[RECEIVED_FIELD]: { prose: false },
|
|
22
39
|
};
|
|
23
40
|
function refuse(rule, location) {
|
|
24
|
-
return
|
|
41
|
+
return { rule, location };
|
|
25
42
|
}
|
|
26
43
|
export function escapePointer(key) {
|
|
27
44
|
return key.replaceAll('~', '~0').replaceAll('/', '~1');
|
|
@@ -33,12 +50,12 @@ export function pointerSegments(pointer) {
|
|
|
33
50
|
return pointer.split('/').slice(1).map(unescapePointer);
|
|
34
51
|
}
|
|
35
52
|
export function refTarget(reference, defs) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (segments.some((segment) => escapePointer(unescapePointer(segment)) !== segment))
|
|
53
|
+
const pointer = reference.slice('#'.length);
|
|
54
|
+
if (!reference.startsWith(DEFS_REFERENCE) ||
|
|
55
|
+
UNRESOLVED.test(reference) ||
|
|
56
|
+
!JSON_POINTER.test(pointer))
|
|
41
57
|
return undefined;
|
|
58
|
+
const [, name = '', ...rest] = pointerSegments(pointer);
|
|
42
59
|
if (!Object.hasOwn(defs, name))
|
|
43
60
|
return undefined;
|
|
44
61
|
let node = defs[name];
|
|
@@ -69,15 +86,7 @@ export function refTarget(reference, defs) {
|
|
|
69
86
|
return { name, node };
|
|
70
87
|
}
|
|
71
88
|
function compiles(pattern) {
|
|
72
|
-
|
|
73
|
-
return false;
|
|
74
|
-
try {
|
|
75
|
-
new RegExp(pattern, 'u');
|
|
76
|
-
return true;
|
|
77
|
-
}
|
|
78
|
-
catch {
|
|
79
|
-
return false;
|
|
80
|
-
}
|
|
89
|
+
return typeof pattern === 'string' && linearPattern(pattern) !== null;
|
|
81
90
|
}
|
|
82
91
|
function finite(value) {
|
|
83
92
|
return typeof value === 'number' && Number.isFinite(value);
|
|
@@ -91,7 +100,7 @@ function strings(value) {
|
|
|
91
100
|
new Set(value).size === value.length);
|
|
92
101
|
}
|
|
93
102
|
function schemaFailure(value, at, context) {
|
|
94
|
-
if (typeof value === 'boolean')
|
|
103
|
+
if (!context.checked || typeof value === 'boolean')
|
|
95
104
|
return undefined;
|
|
96
105
|
if (!isRecord(value))
|
|
97
106
|
return refuse('schema', at);
|
|
@@ -187,7 +196,7 @@ const KEYWORDS = {
|
|
|
187
196
|
uniqueItems: FLAG,
|
|
188
197
|
required: strings,
|
|
189
198
|
dependentRequired: (value) => isRecord(value) && Object.values(value).every(strings),
|
|
190
|
-
format:
|
|
199
|
+
format: (value) => typeof value === 'string' && value !== UNBOUNDED_FORMAT,
|
|
191
200
|
contentEncoding: TEXT,
|
|
192
201
|
contentMediaType: TEXT,
|
|
193
202
|
title: TEXT,
|
|
@@ -204,8 +213,12 @@ const KEYWORDS = {
|
|
|
204
213
|
return (Array.isArray(named) &&
|
|
205
214
|
named.length > 0 &&
|
|
206
215
|
strings(named) &&
|
|
207
|
-
named.every((name) => name === BUILT_IN_TYPE ||
|
|
216
|
+
named.every((name) => name === BUILT_IN_TYPE ||
|
|
217
|
+
name === ATTACHMENT ||
|
|
218
|
+
context.types.has(name) ||
|
|
219
|
+
node.cross_room === true));
|
|
208
220
|
},
|
|
221
|
+
cross_room: (value, _at, node) => typeof value === 'boolean' && node.reference !== undefined,
|
|
209
222
|
};
|
|
210
223
|
function sharedType(values) {
|
|
211
224
|
if (values.length === 0)
|
|
@@ -270,24 +283,45 @@ function united(left, right) {
|
|
|
270
283
|
return undefined;
|
|
271
284
|
return [...left, ...right.filter((value) => !left.some((held) => equalValue(held, value)))];
|
|
272
285
|
}
|
|
286
|
+
function both(left, right) {
|
|
287
|
+
return left !== undefined && right !== undefined ? joined(left, right) : undefined;
|
|
288
|
+
}
|
|
273
289
|
function joined(left, right) {
|
|
274
290
|
const type = left.type === right.type ? left.type : undefined;
|
|
275
291
|
const format = left.format === right.format ? left.format : undefined;
|
|
276
292
|
const values = united(left.values, right.values);
|
|
277
293
|
const reference = united(left.reference, right.reference);
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
|
|
294
|
+
const crossRoom = left.crossRoom === true || right.crossRoom === true;
|
|
295
|
+
const items = both(left.items, right.items);
|
|
296
|
+
const prefix = left.prefix === undefined && right.prefix === undefined
|
|
297
|
+
? undefined
|
|
298
|
+
: Array.from({ length: Math.max(left.prefix?.length ?? 0, right.prefix?.length ?? 0) }, (_, index) => both(itemFacts(left, index), itemFacts(right, index)) ?? { prose: false });
|
|
299
|
+
const properties = Object.create(null);
|
|
300
|
+
for (const [name, facts] of Object.entries(left.properties ?? {})) {
|
|
301
|
+
const shared = both(facts, right.properties?.[name]);
|
|
302
|
+
if (shared !== undefined)
|
|
303
|
+
properties[name] = shared;
|
|
304
|
+
}
|
|
305
|
+
const patterns = (left.patterns ?? []).flatMap(({ pattern, facts }) => {
|
|
306
|
+
const shared = both(facts, right.patterns?.find((one) => one.pattern === pattern)?.facts);
|
|
307
|
+
return shared === undefined ? [] : [{ pattern, facts: shared }];
|
|
308
|
+
});
|
|
309
|
+
const additional = both(left.additional, right.additional);
|
|
281
310
|
return {
|
|
282
311
|
...(type === undefined ? {} : { type }),
|
|
283
312
|
...(format === undefined ? {} : { format }),
|
|
284
313
|
...(values === undefined ? {} : { values }),
|
|
285
314
|
...(reference === undefined ? {} : { reference }),
|
|
315
|
+
...(reference !== undefined && crossRoom ? { crossRoom: true } : {}),
|
|
286
316
|
prose: left.prose && right.prose,
|
|
317
|
+
...(prefix === undefined ? {} : { prefix }),
|
|
287
318
|
...(items === undefined ? {} : { items }),
|
|
319
|
+
...(Object.keys(properties).length === 0 ? {} : { properties }),
|
|
320
|
+
...(patterns.length === 0 ? {} : { patterns }),
|
|
321
|
+
...(additional === undefined ? {} : { additional }),
|
|
288
322
|
};
|
|
289
323
|
}
|
|
290
|
-
function propertiesOf(held, defs, ancestry) {
|
|
324
|
+
function propertiesOf(held, variants, defs, ancestry) {
|
|
291
325
|
const declared = new Map();
|
|
292
326
|
for (const node of held) {
|
|
293
327
|
if (!isRecord(node.properties))
|
|
@@ -298,46 +332,88 @@ function propertiesOf(held, defs, ancestry) {
|
|
|
298
332
|
const properties = Object.create(null);
|
|
299
333
|
for (const [name, schemas] of declared)
|
|
300
334
|
properties[name] = factsOf(schemas, defs, ancestry);
|
|
301
|
-
const
|
|
302
|
-
for (const branch of
|
|
303
|
-
for (const [name, facts] of Object.entries(
|
|
335
|
+
const conditional = Object.create(null);
|
|
336
|
+
for (const branch of variants.values()) {
|
|
337
|
+
for (const [name, facts] of Object.entries(branch.properties ?? {})) {
|
|
304
338
|
if (Object.hasOwn(properties, name))
|
|
305
339
|
continue;
|
|
306
|
-
|
|
340
|
+
conditional[name] = Object.hasOwn(conditional, name)
|
|
341
|
+
? joined(conditional[name], facts)
|
|
342
|
+
: facts;
|
|
307
343
|
}
|
|
308
344
|
}
|
|
309
|
-
for (const [name, facts] of Object.entries(
|
|
345
|
+
for (const [name, facts] of Object.entries(conditional))
|
|
310
346
|
properties[name] = { ...facts, conditional: true };
|
|
311
347
|
return Object.keys(properties).length === 0 ? undefined : properties;
|
|
312
348
|
}
|
|
349
|
+
function nullOnly(facts) {
|
|
350
|
+
return facts.type === 'null' || (facts.values?.every((value) => value === null) ?? false);
|
|
351
|
+
}
|
|
352
|
+
function alternativesOf(held, variants) {
|
|
353
|
+
return held.flatMap((node) => [node.anyOf, node.oneOf].flatMap((branches) => {
|
|
354
|
+
if (!Array.isArray(branches))
|
|
355
|
+
return [];
|
|
356
|
+
const kept = branches
|
|
357
|
+
.flatMap((branch) => {
|
|
358
|
+
const facts = isRecord(branch) ? variants.get(branch) : undefined;
|
|
359
|
+
return facts === undefined ? [] : [facts];
|
|
360
|
+
})
|
|
361
|
+
.filter((facts) => !nullOnly(facts));
|
|
362
|
+
return kept.length === 0 ? [] : [kept];
|
|
363
|
+
}));
|
|
364
|
+
}
|
|
365
|
+
function inheritedOf(held) {
|
|
366
|
+
return held.flatMap((node) => {
|
|
367
|
+
const kept = Object.fromEntries(['type', 'items', 'prefixItems'].flatMap((key) => node[key] === undefined ? [] : [[key, node[key]]]));
|
|
368
|
+
return Object.keys(kept).length === 0 ? [] : [kept];
|
|
369
|
+
});
|
|
370
|
+
}
|
|
313
371
|
function factsOf(nodes, defs, ancestry) {
|
|
314
372
|
const { held, ancestry: reached } = conjoined(nodes, defs, ancestry);
|
|
315
|
-
const
|
|
373
|
+
const inherited = inheritedOf(held);
|
|
374
|
+
const variants = new Map(held
|
|
375
|
+
.flatMap(variantsOf)
|
|
376
|
+
.map((branch) => [branch, factsOf([branch, ...inherited], defs, reached)]));
|
|
377
|
+
const unions = alternativesOf(held, variants);
|
|
378
|
+
const alternatives = unions.map((kept) => kept.reduce(joined));
|
|
379
|
+
const listed = [
|
|
380
|
+
...held.flatMap((node) => Array.isArray(node.enum) ? [node.enum] : node.const !== undefined ? [[node.const]] : []),
|
|
381
|
+
...alternatives.flatMap((facts) => (facts.values === undefined ? [] : [facts.values])),
|
|
382
|
+
];
|
|
316
383
|
const values = listed.length === 0
|
|
317
384
|
? undefined
|
|
318
385
|
: listed.reduce((kept, list) => kept.filter((value) => list.some((other) => equalValue(other, value))));
|
|
319
386
|
const type = firstOf(held, (node) => singleType(node.type)) ??
|
|
320
|
-
(values === undefined ? undefined : sharedType(values))
|
|
387
|
+
(values === undefined ? undefined : sharedType(values)) ??
|
|
388
|
+
alternatives.find((facts) => facts.type !== undefined)?.type;
|
|
321
389
|
const reference = firstOf(held, (node) => typeof node.reference === 'string'
|
|
322
390
|
? [node.reference]
|
|
323
391
|
: Array.isArray(node.reference)
|
|
324
392
|
? node.reference
|
|
325
|
-
: undefined);
|
|
326
|
-
const
|
|
393
|
+
: undefined) ?? alternatives.find((facts) => facts.reference !== undefined)?.reference;
|
|
394
|
+
const crossRoom = firstOf(held, (node) => node.reference === undefined ? undefined : node.cross_room === true) ?? alternatives.find((facts) => facts.reference !== undefined)?.crossRoom === true;
|
|
395
|
+
const format = firstOf(held, (node) => (typeof node.format === 'string' ? node.format : undefined)) ??
|
|
396
|
+
alternatives.find((facts) => facts.format !== undefined)?.format;
|
|
327
397
|
const prose = type === 'string' &&
|
|
328
398
|
values === undefined &&
|
|
329
399
|
format === undefined &&
|
|
330
400
|
reference === undefined &&
|
|
331
|
-
held.every((node) => node.pattern === undefined)
|
|
401
|
+
held.every((node) => node.pattern === undefined) &&
|
|
402
|
+
unions.every((kept) => kept.every((facts) => facts.type !== 'string' || facts.prose));
|
|
332
403
|
const listedItems = held.flatMap((node) => (node.items === undefined ? [] : [node.items]));
|
|
333
|
-
const items = type
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
404
|
+
const items = type !== 'array'
|
|
405
|
+
? undefined
|
|
406
|
+
: (alternatives.find((facts) => facts.items !== undefined)?.items ??
|
|
407
|
+
(listedItems.length > 0 ? factsOf(listedItems, defs, reached) : undefined));
|
|
408
|
+
const tuples = held.flatMap((node) => Array.isArray(node.prefixItems) ? [node.prefixItems] : []);
|
|
409
|
+
const prefix = type !== 'array'
|
|
410
|
+
? undefined
|
|
411
|
+
: (alternatives.find((facts) => facts.prefix !== undefined)?.prefix ??
|
|
412
|
+
(tuples.length === 0
|
|
413
|
+
? undefined
|
|
414
|
+
: Array.from({ length: Math.max(...tuples.map((tuple) => tuple.length)) }, (_, index) => factsOf(tuples.flatMap((tuple) => (index < tuple.length ? [tuple[index]] : [])), defs, reached))));
|
|
339
415
|
const mapped = type === 'object' || type === undefined;
|
|
340
|
-
const properties = mapped ? propertiesOf(held, defs, reached) : undefined;
|
|
416
|
+
const properties = mapped ? propertiesOf(held, variants, defs, reached) : undefined;
|
|
341
417
|
const patterns = mapped
|
|
342
418
|
? held.flatMap((node) => Object.entries(isRecord(node.patternProperties) ? node.patternProperties : {}).map(([pattern, schema]) => ({ pattern, facts: factsOf([schema], defs, reached) })))
|
|
343
419
|
: [];
|
|
@@ -350,6 +426,7 @@ function factsOf(nodes, defs, ancestry) {
|
|
|
350
426
|
...(format === undefined ? {} : { format }),
|
|
351
427
|
...(values === undefined ? {} : { values }),
|
|
352
428
|
...(reference === undefined ? {} : { reference }),
|
|
429
|
+
...(reference !== undefined && crossRoom ? { crossRoom: true } : {}),
|
|
353
430
|
prose,
|
|
354
431
|
...(prefix === undefined ? {} : { prefix }),
|
|
355
432
|
...(items === undefined ? {} : { items }),
|
|
@@ -367,9 +444,57 @@ export function itemFacts(facts, index) {
|
|
|
367
444
|
export function memberFacts(facts, key) {
|
|
368
445
|
if (facts.properties !== undefined && Object.hasOwn(facts.properties, key))
|
|
369
446
|
return facts.properties[key];
|
|
370
|
-
const pattern = facts.patterns?.find((one) =>
|
|
447
|
+
const pattern = facts.patterns?.find((one) => linearPattern(one.pattern).test(key));
|
|
371
448
|
return pattern === undefined ? facts.additional : pattern.facts;
|
|
372
449
|
}
|
|
450
|
+
export function crossedFacts(facts) {
|
|
451
|
+
if (facts.type !== 'array')
|
|
452
|
+
return [facts];
|
|
453
|
+
return [...(facts.prefix ?? []), ...(facts.items === undefined ? [] : [facts.items])].flatMap(crossedFacts);
|
|
454
|
+
}
|
|
455
|
+
export function fieldFacts(definition, key) {
|
|
456
|
+
if (Object.hasOwn(definition.fields, key))
|
|
457
|
+
return definition.fields[key];
|
|
458
|
+
if (Object.hasOwn(HOUSE_KEYS, key))
|
|
459
|
+
return undefined;
|
|
460
|
+
return memberFacts(definition.frontmatter, key);
|
|
461
|
+
}
|
|
462
|
+
function stepped({ facts, through, conditional }, token) {
|
|
463
|
+
if (facts.type === 'array' && ARRAY_INDEX.test(token)) {
|
|
464
|
+
const item = itemFacts(facts, Number(token));
|
|
465
|
+
return item === undefined ? [] : [{ facts: item, through, conditional }];
|
|
466
|
+
}
|
|
467
|
+
return crossedFacts(facts).flatMap((crossed) => {
|
|
468
|
+
const member = memberFacts(crossed, token);
|
|
469
|
+
return member === undefined
|
|
470
|
+
? []
|
|
471
|
+
: [
|
|
472
|
+
{
|
|
473
|
+
facts: member,
|
|
474
|
+
through: through || facts.type === 'array',
|
|
475
|
+
conditional: conditional || member.conditional === true,
|
|
476
|
+
},
|
|
477
|
+
];
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
export function pointerFacts(definition, pointer) {
|
|
481
|
+
const [field, ...rest] = pointerSegments(pointer);
|
|
482
|
+
const declared = field === undefined ? undefined : fieldFacts(definition, field);
|
|
483
|
+
if (declared === undefined)
|
|
484
|
+
return undefined;
|
|
485
|
+
let reached = [
|
|
486
|
+
{ facts: declared, through: false, conditional: declared.conditional === true },
|
|
487
|
+
];
|
|
488
|
+
for (const token of rest)
|
|
489
|
+
reached = reached.flatMap((held) => stepped(held, token));
|
|
490
|
+
if (reached.length === 0)
|
|
491
|
+
return undefined;
|
|
492
|
+
return reached.reduce((kept, held) => ({
|
|
493
|
+
facts: joined(kept.facts, held.facts),
|
|
494
|
+
through: kept.through || held.through,
|
|
495
|
+
conditional: kept.conditional || held.conditional,
|
|
496
|
+
}));
|
|
497
|
+
}
|
|
373
498
|
function reachedDefs(value, defs, reached) {
|
|
374
499
|
if (Array.isArray(value)) {
|
|
375
500
|
for (const item of value)
|
|
@@ -422,6 +547,56 @@ function readSections(value, at, context) {
|
|
|
422
547
|
}
|
|
423
548
|
return { sections };
|
|
424
549
|
}
|
|
550
|
+
function branchSectionsFailure(sections, declared, where) {
|
|
551
|
+
if (!isRecord(sections))
|
|
552
|
+
return refuse('sections', where);
|
|
553
|
+
for (const [name, spec] of Object.entries(sections)) {
|
|
554
|
+
const place = `${where}/${escapePointer(name)}`;
|
|
555
|
+
const own = isRecord(declared) && Object.hasOwn(declared, name) ? declared[name] : undefined;
|
|
556
|
+
if (!isRecord(spec) || !isRecord(own))
|
|
557
|
+
return refuse('sections', place);
|
|
558
|
+
const { required, ...kind } = spec;
|
|
559
|
+
const { required: _required, ...ownKind } = own;
|
|
560
|
+
if (required !== undefined && typeof required !== 'boolean')
|
|
561
|
+
return refuse('sections', `${place}/required`);
|
|
562
|
+
if (!equalValue(kind, ownKind))
|
|
563
|
+
return refuse('sections', place);
|
|
564
|
+
}
|
|
565
|
+
return undefined;
|
|
566
|
+
}
|
|
567
|
+
function withoutBranchSections(schema, declared, at) {
|
|
568
|
+
const kept = { ...schema };
|
|
569
|
+
if (Array.isArray(schema.allOf)) {
|
|
570
|
+
const entries = [];
|
|
571
|
+
for (const [index, entry] of schema.allOf.entries()) {
|
|
572
|
+
const read = isRecord(entry)
|
|
573
|
+
? withoutBranchSections(entry, declared, `${at}/allOf/${index}`)
|
|
574
|
+
: { schema: entry };
|
|
575
|
+
if ('failure' in read)
|
|
576
|
+
return read;
|
|
577
|
+
entries.push(read.schema);
|
|
578
|
+
}
|
|
579
|
+
kept.allOf = entries;
|
|
580
|
+
}
|
|
581
|
+
if (schema.if === undefined)
|
|
582
|
+
return { schema: kept };
|
|
583
|
+
for (const branch of ['then', 'else']) {
|
|
584
|
+
const held = schema[branch];
|
|
585
|
+
if (!isRecord(held))
|
|
586
|
+
continue;
|
|
587
|
+
const { sections, ...rest } = held;
|
|
588
|
+
const failure = sections === undefined
|
|
589
|
+
? undefined
|
|
590
|
+
: branchSectionsFailure(sections, declared, `${at}/${branch}/sections`);
|
|
591
|
+
if (failure !== undefined)
|
|
592
|
+
return { failure };
|
|
593
|
+
const read = withoutBranchSections(rest, declared, `${at}/${branch}`);
|
|
594
|
+
if ('failure' in read)
|
|
595
|
+
return read;
|
|
596
|
+
kept[branch] = read.schema;
|
|
597
|
+
}
|
|
598
|
+
return { schema: kept };
|
|
599
|
+
}
|
|
425
600
|
function deepFreeze(value) {
|
|
426
601
|
if (value !== null && typeof value === 'object' && !Object.isFrozen(value)) {
|
|
427
602
|
Object.freeze(value);
|
|
@@ -430,21 +605,158 @@ function deepFreeze(value) {
|
|
|
430
605
|
}
|
|
431
606
|
return value;
|
|
432
607
|
}
|
|
608
|
+
function ruleValue(value) {
|
|
609
|
+
return value === null || typeof value === 'string' || typeof value === 'boolean' || finite(value);
|
|
610
|
+
}
|
|
611
|
+
function roleNames(value) {
|
|
612
|
+
return strings(value) && value.every((name) => name !== '');
|
|
613
|
+
}
|
|
614
|
+
function compositesOf(value) {
|
|
615
|
+
return Array.isArray(value) ? ['array'] : isRecord(value) ? ['object'] : [];
|
|
616
|
+
}
|
|
617
|
+
function typed(type) {
|
|
618
|
+
const types = typeof type === 'string' ? [type] : type;
|
|
619
|
+
return Array.isArray(types) ? COMPOSITES.filter((kind) => types.includes(kind)) : undefined;
|
|
620
|
+
}
|
|
621
|
+
function admittedWhole(node) {
|
|
622
|
+
if (node === true)
|
|
623
|
+
return COMPOSITES;
|
|
624
|
+
if (!isRecord(node) || Object.keys(node).some((key) => key !== 'type' && !ANNOTATIONS.has(key)))
|
|
625
|
+
return [];
|
|
626
|
+
return typed(node.type) ?? COMPOSITES;
|
|
627
|
+
}
|
|
628
|
+
function composites(node, defs, seen) {
|
|
629
|
+
if (node === false)
|
|
630
|
+
return [];
|
|
631
|
+
if (!isRecord(node))
|
|
632
|
+
return COMPOSITES;
|
|
633
|
+
const each = (nodes) => Array.isArray(nodes) ? nodes.map((one) => composites(one, defs, seen)) : [];
|
|
634
|
+
const target = typeof node.$ref === 'string' ? refTarget(node.$ref, defs)?.node : undefined;
|
|
635
|
+
const conditioned = () => {
|
|
636
|
+
const met = composites(node.then ?? true, defs, seen);
|
|
637
|
+
const condition = composites(node.if, defs, seen);
|
|
638
|
+
const otherwise = composites(node.else ?? true, defs, seen);
|
|
639
|
+
return COMPOSITES.filter((kind) => (condition.includes(kind) && met.includes(kind)) || otherwise.includes(kind));
|
|
640
|
+
};
|
|
641
|
+
const bounds = [
|
|
642
|
+
typed(node.type),
|
|
643
|
+
Array.isArray(node.enum) ? node.enum.flatMap(compositesOf) : undefined,
|
|
644
|
+
Object.hasOwn(node, 'const') ? compositesOf(node.const) : undefined,
|
|
645
|
+
target === undefined || seen.has(target)
|
|
646
|
+
? undefined
|
|
647
|
+
: composites(target, defs, new Set([...seen, target])),
|
|
648
|
+
...each(node.allOf),
|
|
649
|
+
...[node.anyOf, node.oneOf].filter(Array.isArray).map((branches) => each(branches).flat()),
|
|
650
|
+
Object.hasOwn(node, 'if') ? conditioned() : undefined,
|
|
651
|
+
Object.hasOwn(node, 'not')
|
|
652
|
+
? COMPOSITES.filter((kind) => !admittedWhole(node.not).includes(kind))
|
|
653
|
+
: undefined,
|
|
654
|
+
];
|
|
655
|
+
return COMPOSITES.filter((kind) => bounds.every((bound) => bound?.includes(kind) ?? true));
|
|
656
|
+
}
|
|
657
|
+
function transitionOf(value) {
|
|
658
|
+
if (!isRecord(value) || !Object.hasOwn(value, 'from') || !Object.hasOwn(value, 'to'))
|
|
659
|
+
return undefined;
|
|
660
|
+
const { from, to, roles, ...rest } = value;
|
|
661
|
+
if (Object.keys(rest).length > 0 || !ruleValue(from) || !ruleValue(to))
|
|
662
|
+
return undefined;
|
|
663
|
+
if (roles === undefined)
|
|
664
|
+
return { from, to };
|
|
665
|
+
return roleNames(roles) ? { from, to, roles } : undefined;
|
|
666
|
+
}
|
|
667
|
+
function fieldRules(written, field, at, defs) {
|
|
668
|
+
const { change_roles: changeRoles, initial_roles: initialRoles, transitions } = written;
|
|
669
|
+
if (changeRoles !== undefined && !roleNames(changeRoles))
|
|
670
|
+
return refuse('change_roles', `${at}/change_roles`);
|
|
671
|
+
if (initialRoles !== undefined && !roleNames(initialRoles))
|
|
672
|
+
return refuse('initial_roles', `${at}/initial_roles`);
|
|
673
|
+
if (transitions === undefined)
|
|
674
|
+
return {
|
|
675
|
+
...(changeRoles === undefined ? {} : { changeRoles }),
|
|
676
|
+
...(initialRoles === undefined ? {} : { initialRoles }),
|
|
677
|
+
};
|
|
678
|
+
if (!Array.isArray(transitions) || composites(field, defs, new Set()).length > 0)
|
|
679
|
+
return refuse('transitions', `${at}/transitions`);
|
|
680
|
+
const listed = [];
|
|
681
|
+
for (const [index, held] of transitions.entries()) {
|
|
682
|
+
const transition = transitionOf(held);
|
|
683
|
+
if (transition === undefined)
|
|
684
|
+
return refuse('transitions', `${at}/transitions/${index}`);
|
|
685
|
+
listed.push(transition);
|
|
686
|
+
}
|
|
687
|
+
return {
|
|
688
|
+
...(changeRoles === undefined ? {} : { changeRoles }),
|
|
689
|
+
...(initialRoles === undefined ? {} : { initialRoles }),
|
|
690
|
+
transitions: listed,
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
function unruled(schema) {
|
|
694
|
+
const written = new Map();
|
|
695
|
+
if (!isRecord(schema.properties))
|
|
696
|
+
return { schema, written };
|
|
697
|
+
const properties = Object.create(null);
|
|
698
|
+
for (const [field, node] of Object.entries(schema.properties)) {
|
|
699
|
+
if (!isRecord(node) || !FIELD_RULES.some((keyword) => Object.hasOwn(node, keyword))) {
|
|
700
|
+
properties[field] = node;
|
|
701
|
+
continue;
|
|
702
|
+
}
|
|
703
|
+
written.set(field, node);
|
|
704
|
+
properties[field] = Object.fromEntries(Object.entries(node).filter(([keyword]) => !FIELD_RULES.includes(keyword)));
|
|
705
|
+
}
|
|
706
|
+
return { schema: written.size === 0 ? schema : { ...schema, properties }, written };
|
|
707
|
+
}
|
|
708
|
+
function providedType(name, value, provided, checked) {
|
|
709
|
+
const at = `/${escapePointer(name)}`;
|
|
710
|
+
if (checked && provided.nameable?.includes(name) !== true)
|
|
711
|
+
return refuse('provided-type', at);
|
|
712
|
+
if (!isRecord(value))
|
|
713
|
+
return refuse('schema', at);
|
|
714
|
+
const declared = Object.keys(value).find((key) => !PROVIDED_KEYWORDS.includes(key));
|
|
715
|
+
if (checked && declared !== undefined)
|
|
716
|
+
return refuse('provided-type', `${at}/${escapePointer(declared)}`);
|
|
717
|
+
const { create_roles: createRoles, delete_roles: deleteRoles } = value;
|
|
718
|
+
if (createRoles !== undefined && !roleNames(createRoles))
|
|
719
|
+
return refuse('create_roles', `${at}/create_roles`);
|
|
720
|
+
if (deleteRoles !== undefined && !roleNames(deleteRoles))
|
|
721
|
+
return refuse('delete_roles', `${at}/delete_roles`);
|
|
722
|
+
return {
|
|
723
|
+
...provided.types[name],
|
|
724
|
+
...(createRoles === undefined ? {} : { createRoles }),
|
|
725
|
+
...(deleteRoles === undefined ? {} : { deleteRoles }),
|
|
726
|
+
};
|
|
727
|
+
}
|
|
433
728
|
function readType(name, value, context) {
|
|
434
729
|
const at = `/${escapePointer(name)}`;
|
|
435
730
|
if (!isRecord(value))
|
|
436
731
|
return refuse('schema', at);
|
|
437
|
-
const { sections: declared, member_document: mark, ...
|
|
732
|
+
const { sections: declared, member_document: mark, create_roles: createRoles, delete_roles: deleteRoles, ...ruled } = value;
|
|
438
733
|
if (mark !== undefined && typeof mark !== 'boolean')
|
|
439
734
|
return refuse('member_document', `${at}/member_document`);
|
|
735
|
+
if (createRoles !== undefined && !roleNames(createRoles))
|
|
736
|
+
return refuse('create_roles', `${at}/create_roles`);
|
|
737
|
+
if (deleteRoles !== undefined && !roleNames(deleteRoles))
|
|
738
|
+
return refuse('delete_roles', `${at}/delete_roles`);
|
|
440
739
|
const read = declared === undefined
|
|
441
740
|
? { sections: Object.create(null) }
|
|
442
741
|
: readSections(declared, `${at}/sections`, context);
|
|
443
742
|
if ('failure' in read)
|
|
444
743
|
return read.failure;
|
|
744
|
+
const unbranched = withoutBranchSections(ruled, declared, at);
|
|
745
|
+
if ('failure' in unbranched)
|
|
746
|
+
return unbranched.failure;
|
|
747
|
+
const { schema, written } = unruled(unbranched.schema);
|
|
445
748
|
const failure = schemaFailure(schema, at, context);
|
|
446
749
|
if (failure !== undefined)
|
|
447
750
|
return failure;
|
|
751
|
+
const rules = Object.create(null);
|
|
752
|
+
for (const [field, keywords] of written) {
|
|
753
|
+
const properties = schema.properties;
|
|
754
|
+
const where = `${at}/properties/${escapePointer(field)}`;
|
|
755
|
+
const ruling = fieldRules(keywords, properties[field], where, context.defs);
|
|
756
|
+
if ('rule' in ruling)
|
|
757
|
+
return ruling;
|
|
758
|
+
rules[field] = ruling;
|
|
759
|
+
}
|
|
448
760
|
const memberDocument = mark === true;
|
|
449
761
|
const root = valueFacts(schema, context.defs);
|
|
450
762
|
const fields = Object.assign(Object.create(null), root.properties);
|
|
@@ -452,19 +764,26 @@ function readType(name, value, context) {
|
|
|
452
764
|
fields[MEMBER_FIELD] = { type: 'string', prose: false };
|
|
453
765
|
const defs = Object.create(null);
|
|
454
766
|
reachedDefs(value, context.defs, defs);
|
|
767
|
+
const { create_roles: _create, delete_roles: _delete, ...shaped } = value;
|
|
455
768
|
return {
|
|
456
769
|
name,
|
|
457
770
|
memberDocument,
|
|
458
771
|
frontmatter: { ...root, properties: { ...HOUSE_KEYS, ...fields } },
|
|
459
772
|
fields,
|
|
460
773
|
sections: read.sections,
|
|
461
|
-
|
|
774
|
+
rules,
|
|
775
|
+
...(createRoles === undefined ? {} : { createRoles }),
|
|
776
|
+
...(deleteRoles === undefined ? {} : { deleteRoles }),
|
|
777
|
+
shape: {
|
|
778
|
+
schema: written.size === 0 ? shaped : { ...shaped, properties: schema.properties },
|
|
779
|
+
defs,
|
|
780
|
+
},
|
|
462
781
|
};
|
|
463
782
|
}
|
|
464
783
|
function lineAt(text, offset) {
|
|
465
784
|
return text.slice(0, offset).split(/\r\n|\r|\n/).length;
|
|
466
785
|
}
|
|
467
|
-
export function readSchema(text) {
|
|
786
|
+
export function readSchema(text, provided = NO_TYPES, checked = true) {
|
|
468
787
|
let root;
|
|
469
788
|
try {
|
|
470
789
|
root = jsValue(parseYamlMapping(text, 0, text.length));
|
|
@@ -478,11 +797,15 @@ export function readSchema(text) {
|
|
|
478
797
|
if (!isRecord(defs))
|
|
479
798
|
return refuse('$defs', '/$defs');
|
|
480
799
|
const names = Object.keys(root).filter((key) => key !== '$defs');
|
|
481
|
-
for (const name of names) {
|
|
800
|
+
for (const name of checked ? names : []) {
|
|
482
801
|
if (name === '' || name.startsWith('$') || name === BUILT_IN_TYPE || name === ATTACHMENT)
|
|
483
802
|
return refuse('schema-type-name', `/${escapePointer(name)}`);
|
|
484
803
|
}
|
|
485
|
-
const context = {
|
|
804
|
+
const context = {
|
|
805
|
+
defs,
|
|
806
|
+
types: new Set([...names, ...Object.keys(provided.types)]),
|
|
807
|
+
checked,
|
|
808
|
+
};
|
|
486
809
|
for (const [name, def] of Object.entries(defs)) {
|
|
487
810
|
const failure = schemaFailure(def, `/$defs/${escapePointer(name)}`, context);
|
|
488
811
|
if (failure !== undefined)
|
|
@@ -491,15 +814,21 @@ export function readSchema(text) {
|
|
|
491
814
|
const types = Object.create(null);
|
|
492
815
|
let marked = false;
|
|
493
816
|
for (const name of names) {
|
|
494
|
-
const definition =
|
|
817
|
+
const definition = Object.hasOwn(provided.types, name)
|
|
818
|
+
? providedType(name, root[name], provided, checked)
|
|
819
|
+
: readType(name, root[name], context);
|
|
495
820
|
if ('rule' in definition)
|
|
496
821
|
return definition;
|
|
497
|
-
if (definition.memberDocument) {
|
|
822
|
+
if (checked && definition.memberDocument) {
|
|
498
823
|
if (marked)
|
|
499
824
|
return refuse('member_document', `/${escapePointer(name)}/member_document`);
|
|
500
825
|
marked = true;
|
|
501
826
|
}
|
|
502
827
|
types[name] = deepFreeze(definition);
|
|
503
828
|
}
|
|
829
|
+
for (const [name, definition] of Object.entries(provided.types)) {
|
|
830
|
+
if (!Object.hasOwn(types, name))
|
|
831
|
+
types[name] = definition;
|
|
832
|
+
}
|
|
504
833
|
return Object.freeze({ types: Object.freeze(types) });
|
|
505
834
|
}
|
package/dist/divide.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"divide.d.ts","sourceRoot":"","sources":["../src/divide.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"divide.d.ts","sourceRoot":"","sources":["../src/divide.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,QAAQ,EAAE,eAAe,EAAE,QAAQ,EAAgB,MAAM,YAAY,CAAC;AAwXpF,wBAAgB,MAAM,CAAC,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,GAAG,QAAQ,CAc3F"}
|