@open-captable/cli 0.0.0-stage → 0.2.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/LICENSE.md +60 -0
- package/README.md +85 -2
- package/build/ObjectTypeSchemaMap.js +146 -0
- package/build/SchemaComposer.js +835 -0
- package/build/SchemaDereferencer.js +207 -0
- package/build/schemas.json +1 -0
- package/cli.mjs +97 -0
- package/lib/export.mjs +148 -0
- package/lib/input.mjs +29 -0
- package/lib/remote.mjs +102 -0
- package/lib/schema.mjs +147 -0
- package/package.json +38 -4
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SchemaDereferencer
|
|
3
|
+
*
|
|
4
|
+
* Builds on `SchemaComposer` to produce a *fully self-contained* view of an
|
|
5
|
+
* OCF schema. Where `composeSchema` flattens the `allOf` ancestor chain but
|
|
6
|
+
* leaves every leaf `$ref` (to types, enums, nested objects) intact,
|
|
7
|
+
* `dereferenceSchema` goes one step further and recursively inlines those
|
|
8
|
+
* `$ref`s as well, so the result has **no external references** at all.
|
|
9
|
+
*
|
|
10
|
+
* composeSchema -> flat properties/required, `$ref`s preserved
|
|
11
|
+
* dereferenceSchema -> flat properties/required, `$ref`s replaced inline
|
|
12
|
+
*
|
|
13
|
+
* Useful for standalone validation, isolated codegen inputs, and any consumer
|
|
14
|
+
* that can't resolve OCF's URL-based `$ref`s on its own.
|
|
15
|
+
*
|
|
16
|
+
* Like the composer, this module is framework-free: plain JSON in, plain JSON
|
|
17
|
+
* out, driven by an `$id` -> raw-schema registry.
|
|
18
|
+
*
|
|
19
|
+
* Cycles: OCF's schema graph is acyclic, but to stay safe a `$ref` that would
|
|
20
|
+
* reference a schema already being expanded on the current path is left as a
|
|
21
|
+
* `{ $ref }` rather than inlined infinitely.
|
|
22
|
+
*/
|
|
23
|
+
import { buildSchemaRegistry, composeSchema, isBackwardsCompatibleWrapper, } from "./SchemaComposer.js";
|
|
24
|
+
/**
|
|
25
|
+
* Identity / meta keys that are preserved on a top-level dereferenced schema
|
|
26
|
+
* (it remains a named schema document) but stripped when a schema is inlined
|
|
27
|
+
* into a referring property — there we only want the structural constraints,
|
|
28
|
+
* not the source schema's identity. `allOf` is stripped everywhere because the
|
|
29
|
+
* chain has already been flattened into `properties`/`required`.
|
|
30
|
+
*/
|
|
31
|
+
const INLINE_STRIP_KEYS = ["$id", "$schema", "$comment", "title", "allOf"];
|
|
32
|
+
const MISSING_REF_MESSAGE = "unknown $ref";
|
|
33
|
+
function dedupe(items) {
|
|
34
|
+
return Array.from(new Set(items));
|
|
35
|
+
}
|
|
36
|
+
function isPlaceholder(value) {
|
|
37
|
+
return (value &&
|
|
38
|
+
typeof value === "object" &&
|
|
39
|
+
!Array.isArray(value) &&
|
|
40
|
+
Object.keys(value).length === 0);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Flatten a schema's `allOf` chain into merged `properties`/`required`.
|
|
44
|
+
*
|
|
45
|
+
* Non-wrapper schemas are flattened by the composer directly. Wrapper schemas
|
|
46
|
+
* (single `allOf` + a lone `object_type`) are deliberately *not* flattened by
|
|
47
|
+
* the composer, but a self-contained view needs them flattened too — so here
|
|
48
|
+
* we additionally pull the wrapper's parent properties in and let the
|
|
49
|
+
* wrapper's own `object_type` win.
|
|
50
|
+
*/
|
|
51
|
+
function flatten(raw, registry, composeCache) {
|
|
52
|
+
const composed = composeSchema(raw, registry, composeCache);
|
|
53
|
+
if (!isBackwardsCompatibleWrapper(raw) || !raw.allOf?.length) {
|
|
54
|
+
return composed;
|
|
55
|
+
}
|
|
56
|
+
const mergedProperties = {};
|
|
57
|
+
const required = [];
|
|
58
|
+
for (const ref of raw.allOf) {
|
|
59
|
+
const parentRaw = registry[ref.$ref];
|
|
60
|
+
if (!parentRaw) {
|
|
61
|
+
throw new Error(`Cannot dereference ${raw.$id}: ${MISSING_REF_MESSAGE} '${ref.$ref}'`);
|
|
62
|
+
}
|
|
63
|
+
const parent = composeSchema(parentRaw, registry, composeCache);
|
|
64
|
+
Object.assign(mergedProperties, parent.properties ?? {});
|
|
65
|
+
required.push(...(parent.required ?? []));
|
|
66
|
+
}
|
|
67
|
+
for (const [name, value] of Object.entries(composed.properties ?? {})) {
|
|
68
|
+
if (!isPlaceholder(value))
|
|
69
|
+
mergedProperties[name] = value;
|
|
70
|
+
}
|
|
71
|
+
required.push(...(composed.required ?? []));
|
|
72
|
+
return { ...composed, properties: mergedProperties, required };
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Resolve a `$ref` target to its fully-dereferenced, meta-stripped body so it
|
|
76
|
+
* can be embedded into a referring property. Returns `null` only when the ref
|
|
77
|
+
* is unresolved and `onMissingRef === "keep"`.
|
|
78
|
+
*/
|
|
79
|
+
function inlineRef(refId, registry, onMissingRef, composeCache, inlineCache, stack) {
|
|
80
|
+
// A ref back into something already on the current expansion path would
|
|
81
|
+
// recurse forever; leave it as a ref instead.
|
|
82
|
+
if (stack.has(refId))
|
|
83
|
+
return { $ref: refId };
|
|
84
|
+
const cached = inlineCache.get(refId);
|
|
85
|
+
if (cached)
|
|
86
|
+
return cached;
|
|
87
|
+
const targetRaw = registry[refId];
|
|
88
|
+
if (!targetRaw) {
|
|
89
|
+
if (onMissingRef === "keep")
|
|
90
|
+
return null;
|
|
91
|
+
throw new Error(`Cannot dereference ${MISSING_REF_MESSAGE} '${refId}'`);
|
|
92
|
+
}
|
|
93
|
+
const nextStack = new Set(stack);
|
|
94
|
+
nextStack.add(refId);
|
|
95
|
+
const composed = flatten(targetRaw, registry, composeCache);
|
|
96
|
+
const body = {};
|
|
97
|
+
for (const [key, value] of Object.entries(composed)) {
|
|
98
|
+
if (INLINE_STRIP_KEYS.includes(key))
|
|
99
|
+
continue;
|
|
100
|
+
body[key] = derefNode(value, registry, onMissingRef, composeCache, inlineCache, nextStack);
|
|
101
|
+
}
|
|
102
|
+
// The composer defaults every schema to `properties: {}` / `required: []`;
|
|
103
|
+
// drop those when vacuous so inlined scalar types (Date, Numeric, ...) stay
|
|
104
|
+
// clean (a string-typed property shouldn't carry empty object machinery).
|
|
105
|
+
if (Array.isArray(body.required)) {
|
|
106
|
+
const deduped = dedupe(body.required);
|
|
107
|
+
if (deduped.length)
|
|
108
|
+
body.required = deduped;
|
|
109
|
+
else
|
|
110
|
+
delete body.required;
|
|
111
|
+
}
|
|
112
|
+
if (body.properties &&
|
|
113
|
+
typeof body.properties === "object" &&
|
|
114
|
+
!Array.isArray(body.properties) &&
|
|
115
|
+
Object.keys(body.properties).length === 0) {
|
|
116
|
+
delete body.properties;
|
|
117
|
+
}
|
|
118
|
+
inlineCache.set(refId, body);
|
|
119
|
+
return body;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Recursively dereference any JSON node. The traversal is structural and
|
|
123
|
+
* generic: `$ref` nodes are inlined (local sibling keys win over the target's
|
|
124
|
+
* fields), and every other object/array is walked so refs nested inside
|
|
125
|
+
* `items`, `oneOf`, nested `properties`, etc. are handled uniformly. Scalars
|
|
126
|
+
* and string arrays (`required`, `enum`) pass through unchanged.
|
|
127
|
+
*/
|
|
128
|
+
function derefNode(node, registry, onMissingRef, composeCache, inlineCache, stack) {
|
|
129
|
+
if (Array.isArray(node)) {
|
|
130
|
+
return node.map((item) => derefNode(item, registry, onMissingRef, composeCache, inlineCache, stack));
|
|
131
|
+
}
|
|
132
|
+
if (!node || typeof node !== "object")
|
|
133
|
+
return node;
|
|
134
|
+
if (typeof node.$ref === "string") {
|
|
135
|
+
const body = inlineRef(node.$ref, registry, onMissingRef, composeCache, inlineCache, stack);
|
|
136
|
+
if (body === null)
|
|
137
|
+
return { ...node }; // unresolved + onMissingRef "keep"
|
|
138
|
+
const siblings = {};
|
|
139
|
+
for (const [key, value] of Object.entries(node)) {
|
|
140
|
+
if (key === "$ref")
|
|
141
|
+
continue;
|
|
142
|
+
siblings[key] = derefNode(value, registry, onMissingRef, composeCache, inlineCache, stack);
|
|
143
|
+
}
|
|
144
|
+
// Local annotations (e.g. a property's own `description`) take precedence
|
|
145
|
+
// over the inlined target's fields.
|
|
146
|
+
return { ...body, ...siblings };
|
|
147
|
+
}
|
|
148
|
+
const out = {};
|
|
149
|
+
for (const [key, value] of Object.entries(node)) {
|
|
150
|
+
out[key] = derefNode(value, registry, onMissingRef, composeCache, inlineCache, stack);
|
|
151
|
+
}
|
|
152
|
+
return out;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Produce a fully self-contained schema: the `allOf` chain is flattened and
|
|
156
|
+
* every `$ref` (recursively) is inlined. The original `$id`, `$schema`,
|
|
157
|
+
* `$comment`, `title`, and `description` are preserved so the result is still
|
|
158
|
+
* an identifiable schema document; the top-level `allOf` is dropped because
|
|
159
|
+
* its contributions now live inline in `properties`/`required`.
|
|
160
|
+
*/
|
|
161
|
+
export function dereferenceSchema(raw, registry, options = {}, caches = {}) {
|
|
162
|
+
const { onMissingRef = "throw" } = options;
|
|
163
|
+
const composeCache = caches.compose ?? new Map();
|
|
164
|
+
const inlineCache = caches.inline ?? new Map();
|
|
165
|
+
const composed = flatten(raw, registry, composeCache);
|
|
166
|
+
const stack = new Set([raw.$id]);
|
|
167
|
+
const out = {};
|
|
168
|
+
for (const [key, value] of Object.entries(composed)) {
|
|
169
|
+
if (key === "allOf")
|
|
170
|
+
continue; // external chain, already flattened in
|
|
171
|
+
out[key] = derefNode(value, registry, onMissingRef, composeCache, inlineCache, stack);
|
|
172
|
+
}
|
|
173
|
+
out.properties = out.properties ?? {};
|
|
174
|
+
out.required = dedupe(out.required ?? []);
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Dereference every schema in `rawSchemas`. Returns the raw registry, the
|
|
179
|
+
* dereferenced list (input order), and an `$id` -> dereferenced index. Shares
|
|
180
|
+
* one set of caches across the whole pass so common leaf types are inlined
|
|
181
|
+
* once.
|
|
182
|
+
*/
|
|
183
|
+
export function dereferenceAll(rawSchemas, options = {}) {
|
|
184
|
+
const { onMissingRef = "throw" } = options;
|
|
185
|
+
const registry = buildSchemaRegistry(rawSchemas);
|
|
186
|
+
const caches = { compose: new Map(), inline: new Map() };
|
|
187
|
+
const dereferenced = rawSchemas.map((raw) => {
|
|
188
|
+
try {
|
|
189
|
+
return dereferenceSchema(raw, registry, options, caches);
|
|
190
|
+
}
|
|
191
|
+
catch (err) {
|
|
192
|
+
const isMissingRef = err instanceof Error && err.message.includes(MISSING_REF_MESSAGE);
|
|
193
|
+
if (onMissingRef === "keep" && isMissingRef) {
|
|
194
|
+
return {
|
|
195
|
+
...raw,
|
|
196
|
+
properties: raw.properties ?? {},
|
|
197
|
+
required: raw.required ? dedupe(raw.required) : [],
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
throw err;
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
const dereferencedById = {};
|
|
204
|
+
for (const schema of dereferenced)
|
|
205
|
+
dereferencedById[schema.$id] = schema;
|
|
206
|
+
return { registry, dereferenced, dereferencedById };
|
|
207
|
+
}
|