@burnsred/entity-sync 0.0.0-stage → 3.0.0-rc.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/.claude-plugin/marketplace.json +14 -0
- package/.claude-plugin/plugin.json +11 -0
- package/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +240 -2
- package/agents/entity-editor.md +16 -0
- package/bin/entity-sync.mjs +36 -0
- package/dist/cli.d.mts +20 -0
- package/dist/commands/asserts.d.mts +4 -0
- package/dist/commands/check.d.mts +6 -0
- package/dist/commands/inspect.d.mts +30 -0
- package/dist/commands/plan.d.mts +28 -0
- package/dist/commands/schema.d.mts +39 -0
- package/dist/config.d.mts +175 -0
- package/dist/index.d.mts +5 -0
- package/dist/lib/cache.d.mts +120 -0
- package/dist/lib/diff.d.mts +228 -0
- package/dist/lib/field-map.d.mts +93 -0
- package/dist/lib/gen-asserts.d.mts +70 -0
- package/dist/lib/graph.d.mts +90 -0
- package/dist/lib/parse-entities.d.mts +257 -0
- package/dist/lib/parse-schema.d.mts +98 -0
- package/dist/lib/resolve.d.mts +135 -0
- package/dist/project.d.mts +60 -0
- package/hooks/editor-guard.mjs +86 -0
- package/hooks/hooks.json +15 -0
- package/package.json +54 -4
- package/skills/entity/SKILL.md +105 -0
- package/skills/entity/reference/create.md +74 -0
- package/skills/entity/reference/editor.md +80 -0
- package/skills/entity/reference/enums.md +89 -0
- package/skills/entity/reference/fields.md +101 -0
- package/skills/entity/reference/sync.md +191 -0
- package/src/cli.mjs +110 -0
- package/src/commands/asserts.mjs +52 -0
- package/src/commands/check.mjs +88 -0
- package/src/commands/inspect.mjs +290 -0
- package/src/commands/plan.mjs +542 -0
- package/src/commands/schema.mjs +206 -0
- package/src/config.mjs +205 -0
- package/src/index.mjs +19 -0
- package/src/lib/cache.mjs +323 -0
- package/src/lib/diff.mjs +706 -0
- package/src/lib/field-map.mjs +438 -0
- package/src/lib/gen-asserts.mjs +478 -0
- package/src/lib/graph.mjs +284 -0
- package/src/lib/parse-entities.mjs +935 -0
- package/src/lib/parse-schema.mjs +667 -0
- package/src/lib/resolve.mjs +455 -0
- package/src/project.mjs +115 -0
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Generate compile-time assertions tying each entity to its schema component.
|
|
4
|
+
*
|
|
5
|
+
* This is what makes `tsc` the authority instead of the report. The report says
|
|
6
|
+
* what drifted at the moment it ran; the assertions keep saying it on every
|
|
7
|
+
* build, including in CI, until someone fixes it.
|
|
8
|
+
*
|
|
9
|
+
* ## What is asserted, and how strictly
|
|
10
|
+
*
|
|
11
|
+
* | field kind | assertion | why |
|
|
12
|
+
* | ---------- | -------------------------------------------- | --- |
|
|
13
|
+
* | key set | `SameKeys<Fields, Wire>` | catches missing, extra and renamed fields |
|
|
14
|
+
* | scalar | `Exact<Fields[k], Wire[k]>` | compared against the schema type directly, not a reconstructed string |
|
|
15
|
+
* | enum | `Extends<Wire[k], Fields[k]>` | one-directional, so a plain `string` or a named alias is accepted |
|
|
16
|
+
* | nested | `Exact<Fields[k], ChildEntityFields>` | one level only |
|
|
17
|
+
* | record | `Exact<RecordFieldsOf<R>, Omit<…> & { … }>` | the Immutable substitution the convention requires |
|
|
18
|
+
*
|
|
19
|
+
* Two of those choices matter:
|
|
20
|
+
*
|
|
21
|
+
* **Nested fields are asserted one level deep**, against the child's own Fields
|
|
22
|
+
* type - never by recursing into the child's structure. The deep guarantee comes
|
|
23
|
+
* from composition, since the child carries its own assertion. A recursive
|
|
24
|
+
* `DeepNorm<T>` over a mutually recursive serializer graph would hit the
|
|
25
|
+
* instantiation-depth limit, and would report errors at the wrong place.
|
|
26
|
+
*
|
|
27
|
+
* **Enum fields are one-directional on purpose**, so the assertion is exactly as
|
|
28
|
+
* tolerant as `diff.mjs`. The report accepts a literal union, a plain `string`,
|
|
29
|
+
* or a hand-maintained alias such as `ApplicationStatusValue`; if the assertion
|
|
30
|
+
* demanded the exact union, the build would stay red on something the report
|
|
31
|
+
* calls fine.
|
|
32
|
+
*
|
|
33
|
+
* `Norm<T>` strips `?` before comparing, because DRF marks almost every field
|
|
34
|
+
* optional on write while the entity types them as always present on read.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
import { posix } from 'node:path';
|
|
38
|
+
|
|
39
|
+
import { expectedField } from './field-map.mjs';
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @typedef {import('./parse-entities.mjs').ParsedEntity} ParsedEntity
|
|
43
|
+
* @typedef {import('./parse-schema.mjs').ParsedSchema} ParsedSchema
|
|
44
|
+
*/
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @typedef {object} GeneratedAsserts
|
|
48
|
+
* @property {string} source the file contents
|
|
49
|
+
* @property {{ className: string, reason: string }[]} skipped
|
|
50
|
+
* @property {number} entityCount
|
|
51
|
+
* @property {number} assertionCount
|
|
52
|
+
*/
|
|
53
|
+
|
|
54
|
+
/** Where the generated file lives by convention, repo-relative. */
|
|
55
|
+
export const DEFAULT_ASSERTS_DIR = 'src/entities/__generated__';
|
|
56
|
+
|
|
57
|
+
/** How the generated file imports the schema types by convention. */
|
|
58
|
+
export const DEFAULT_SCHEMA_SPECIFIER = '@/api/schema';
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The shared type machinery, emitted once at the top of the file.
|
|
62
|
+
*
|
|
63
|
+
* @param {string} schemaSpecifier
|
|
64
|
+
*/
|
|
65
|
+
const prelude = (schemaSpecifier) => `/**
|
|
66
|
+
* GENERATED by \`npm run entity:asserts\` - do not edit.
|
|
67
|
+
*
|
|
68
|
+
* Each assertion ties an entity's types to the schema component it models. A
|
|
69
|
+
* failure names the entity, the field, and both types involved.
|
|
70
|
+
*
|
|
71
|
+
* To regenerate: \`npm run entity:asserts\`
|
|
72
|
+
* To see the drift as a report instead: \`npm run entity:plan\`
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
/* eslint-disable */
|
|
76
|
+
import type { List } from 'immutable';
|
|
77
|
+
import type { Moment } from 'moment';
|
|
78
|
+
|
|
79
|
+
import type { EntityRecord } from '@burnsred/entity';
|
|
80
|
+
|
|
81
|
+
import type { components } from '${schemaSpecifier}';
|
|
82
|
+
|
|
83
|
+
/** Strip \`?\` (and the \`| undefined\` it implies) without touching \`| null\`. */
|
|
84
|
+
type Norm<T> = { [K in keyof T]-?: T[K] };
|
|
85
|
+
|
|
86
|
+
/** Both key sets must match; the failure names the offending keys. */
|
|
87
|
+
type SameKeys<A, B> = [Exclude<keyof B, keyof A>] extends [never]
|
|
88
|
+
? [Exclude<keyof A, keyof B>] extends [never]
|
|
89
|
+
? true
|
|
90
|
+
: { extraInEntity: Exclude<keyof A, keyof B> }
|
|
91
|
+
: { missingFromEntity: Exclude<keyof B, keyof A> };
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* For an entity marked \`@schemaSubset\`: every field it declares must exist
|
|
95
|
+
* upstream, but it need not declare all of them. Catches extra and misnamed
|
|
96
|
+
* fields, which is the part that matters for a deliberate projection.
|
|
97
|
+
*/
|
|
98
|
+
type SubsetKeys<A, B> = [Exclude<keyof A, keyof B>] extends [never]
|
|
99
|
+
? true
|
|
100
|
+
: { notInSchema: Exclude<keyof A, keyof B> };
|
|
101
|
+
|
|
102
|
+
/** Mutual assignability. The failure carries both sides. */
|
|
103
|
+
type Exact<A, B> = [A] extends [B]
|
|
104
|
+
? [B] extends [A]
|
|
105
|
+
? true
|
|
106
|
+
: { expected: B; got: A }
|
|
107
|
+
: { expected: B; got: A };
|
|
108
|
+
|
|
109
|
+
/** One-directional: every value the API can send must fit the entity's type. */
|
|
110
|
+
type Extends<Wire, Field> = [Wire] extends [Field]
|
|
111
|
+
? true
|
|
112
|
+
: { mustAccept: Wire; got: Field };
|
|
113
|
+
|
|
114
|
+
/** Recover the field map from an \`EntityRecord<T>\`. */
|
|
115
|
+
type RecordFieldsOf<R> = R extends EntityRecord<infer T> ? T : never;
|
|
116
|
+
`;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Derive the module specifier to import an entity's types from, relative to the
|
|
120
|
+
* directory the generated file is written to.
|
|
121
|
+
*
|
|
122
|
+
* @param {string} file repo-relative, e.g. 'src/entities/filter/Foo.ts'
|
|
123
|
+
* @param {string} [assertsDir] repo-relative directory of the generated file
|
|
124
|
+
* @returns {string} e.g. '../filter/Foo'
|
|
125
|
+
*/
|
|
126
|
+
export function moduleSpecifier(file, assertsDir = DEFAULT_ASSERTS_DIR) {
|
|
127
|
+
const rel = posix.relative(assertsDir, file.replace(/\.tsx?$/, ''));
|
|
128
|
+
return rel.startsWith('.') ? rel : `./${rel}`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* A valid TypeScript identifier suffix for an entity, used in const names.
|
|
133
|
+
*
|
|
134
|
+
* @param {string} className
|
|
135
|
+
*/
|
|
136
|
+
function assertName(className) {
|
|
137
|
+
return `assert${className}`;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Quote a property name only when it is not a plain identifier.
|
|
142
|
+
*
|
|
143
|
+
* @param {string} name
|
|
144
|
+
*/
|
|
145
|
+
function propertyKey(name) {
|
|
146
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name) ? name : `'${name}'`;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Generate the assertions file.
|
|
151
|
+
*
|
|
152
|
+
* @param {object} input
|
|
153
|
+
* @param {ParsedEntity[]} input.entities
|
|
154
|
+
* @param {import('./resolve.mjs').Resolution[]} input.resolutions
|
|
155
|
+
* @param {ParsedSchema} input.schema
|
|
156
|
+
* @param {Record<string, { className: string, fieldsTypeName: string, recordTypeName: string }>} input.entityByComponent
|
|
157
|
+
* @param {import('./resolve.mjs').EnumResolution[]} [input.enumResolutions]
|
|
158
|
+
* @param {Record<string, { name: string, typeName: string, file: string }>} [input.enumByComponent]
|
|
159
|
+
* @param {string} [input.assertsDir] repo-relative directory the file is written to
|
|
160
|
+
* @param {string} [input.schemaSpecifier] module the schema `components` type is imported from
|
|
161
|
+
* @returns {GeneratedAsserts}
|
|
162
|
+
*/
|
|
163
|
+
export function generateAsserts({
|
|
164
|
+
entities,
|
|
165
|
+
resolutions,
|
|
166
|
+
schema,
|
|
167
|
+
entityByComponent,
|
|
168
|
+
enumResolutions = [],
|
|
169
|
+
enumByComponent = {},
|
|
170
|
+
assertsDir = DEFAULT_ASSERTS_DIR,
|
|
171
|
+
schemaSpecifier = DEFAULT_SCHEMA_SPECIFIER,
|
|
172
|
+
}) {
|
|
173
|
+
const byClassName = new Map(entities.map((e) => [e.className, e]));
|
|
174
|
+
|
|
175
|
+
/** @type {{ className: string, reason: string }[]} */
|
|
176
|
+
const skipped = [];
|
|
177
|
+
/** module specifier -> the type names to import from it */
|
|
178
|
+
/** @type {Map<string, Set<string>>} */
|
|
179
|
+
const imports = new Map();
|
|
180
|
+
/** @type {string[]} */
|
|
181
|
+
const blocks = [];
|
|
182
|
+
|
|
183
|
+
let assertionCount = 0;
|
|
184
|
+
let entityCount = 0;
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* @param {string} specifier
|
|
188
|
+
* @param {string} typeName
|
|
189
|
+
*/
|
|
190
|
+
const addImport = (specifier, typeName) => {
|
|
191
|
+
const names = imports.get(specifier) ?? new Set();
|
|
192
|
+
names.add(typeName);
|
|
193
|
+
imports.set(specifier, names);
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
for (const resolution of resolutions) {
|
|
197
|
+
const entity = byClassName.get(resolution.className);
|
|
198
|
+
if (!entity) continue;
|
|
199
|
+
|
|
200
|
+
if (!resolution.component) {
|
|
201
|
+
// Already reported by resolve.mjs; noted here so the file itself records
|
|
202
|
+
// what it does not cover.
|
|
203
|
+
if (!resolution.excluded) {
|
|
204
|
+
skipped.push({
|
|
205
|
+
className: resolution.className,
|
|
206
|
+
reason: resolution.note ?? 'unresolved',
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const component = schema.components[resolution.component];
|
|
213
|
+
if (component?.kind !== 'object') {
|
|
214
|
+
skipped.push({
|
|
215
|
+
className: resolution.className,
|
|
216
|
+
reason: `component ${resolution.component} is not an object type`,
|
|
217
|
+
});
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (!entity.fieldsTypeName || !entity.fieldsTypeExported) {
|
|
222
|
+
skipped.push({
|
|
223
|
+
className: resolution.className,
|
|
224
|
+
reason: `${entity.fieldsTypeName ?? 'Fields type'} is not exported, so it cannot be imported here`,
|
|
225
|
+
});
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const specifier = moduleSpecifier(entity.file, assertsDir);
|
|
230
|
+
addImport(specifier, entity.fieldsTypeName);
|
|
231
|
+
|
|
232
|
+
const wireType = `components['schemas']['${component.name}']`;
|
|
233
|
+
|
|
234
|
+
/** @type {string[]} */
|
|
235
|
+
const properties = [];
|
|
236
|
+
/** Nested fields, for the Record assertion. */
|
|
237
|
+
/** @type {{ name: string, recordTypeText: string }[]} */
|
|
238
|
+
const nested = [];
|
|
239
|
+
|
|
240
|
+
const declaredMembers = new Set(
|
|
241
|
+
entity.fieldsTypeMembers.map((member) => member.name),
|
|
242
|
+
);
|
|
243
|
+
const ignoredFields = new Set(Object.keys(entity.schemaIgnores));
|
|
244
|
+
|
|
245
|
+
for (const schemaField of component.fields) {
|
|
246
|
+
const expected = expectedField(schemaField, {
|
|
247
|
+
components: schema.components,
|
|
248
|
+
entityByComponent,
|
|
249
|
+
enumByComponent,
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
// A shape the mapper declined to model cannot be asserted either.
|
|
253
|
+
if (expected.note) continue;
|
|
254
|
+
|
|
255
|
+
// A field absent from the entity's Fields type is already reported by
|
|
256
|
+
// `__keys`. Emitting `Norm<Fields>['x']` for it as well would add a
|
|
257
|
+
// second, worse error (TS2339 "property does not exist") that says
|
|
258
|
+
// nothing new - 42 of them on the first real run.
|
|
259
|
+
if (!declaredMembers.has(schemaField.name)) continue;
|
|
260
|
+
|
|
261
|
+
// Suppressed because the schema, not the entity, is wrong here.
|
|
262
|
+
if (ignoredFields.has(schemaField.name)) continue;
|
|
263
|
+
|
|
264
|
+
const key = propertyKey(schemaField.name);
|
|
265
|
+
const fieldsAccess = `Norm<${entity.fieldsTypeName}>[${JSON.stringify(schemaField.name)}]`;
|
|
266
|
+
const wireAccess = `Norm<${wireType}>[${JSON.stringify(schemaField.name)}]`;
|
|
267
|
+
|
|
268
|
+
if (expected.entityRef) {
|
|
269
|
+
const child = byClassName.get(expected.entityRef);
|
|
270
|
+
if (!child?.fieldsTypeName || !child.fieldsTypeExported) {
|
|
271
|
+
// Cannot name the child's type, so skip this field rather than emit
|
|
272
|
+
// something that will not compile.
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
addImport(
|
|
277
|
+
moduleSpecifier(child.file, assertsDir),
|
|
278
|
+
child.fieldsTypeName,
|
|
279
|
+
);
|
|
280
|
+
properties.push(
|
|
281
|
+
` ${key}: Exact<${fieldsAccess}, ${expected.typeText}>;`,
|
|
282
|
+
);
|
|
283
|
+
assertionCount += 1;
|
|
284
|
+
|
|
285
|
+
if (expected.recordTypeText && child.recordTypeExported) {
|
|
286
|
+
addImport(
|
|
287
|
+
moduleSpecifier(child.file, assertsDir),
|
|
288
|
+
/** @type {string} */ (child.recordTypeName),
|
|
289
|
+
);
|
|
290
|
+
nested.push({
|
|
291
|
+
name: schemaField.name,
|
|
292
|
+
recordTypeText: expected.recordTypeText,
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if (expected.enumValues.length) {
|
|
299
|
+
// One-directional, matching the report's tolerance.
|
|
300
|
+
properties.push(` ${key}: Extends<${wireAccess}, ${fieldsAccess}>;`);
|
|
301
|
+
assertionCount += 1;
|
|
302
|
+
} else {
|
|
303
|
+
properties.push(` ${key}: Exact<${fieldsAccess}, ${wireAccess}>;`);
|
|
304
|
+
assertionCount += 1;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// A field that converts on the way in carries a different Record member:
|
|
308
|
+
// a date arrives as a Moment, and any `many: true` field arrives as a
|
|
309
|
+
// List. That second case is why this sits below the enum branch rather
|
|
310
|
+
// than inside the primitive one - a `many: true` choices field is both an
|
|
311
|
+
// enum and an array, and an early `continue` here used to drop its
|
|
312
|
+
// substitution on the floor.
|
|
313
|
+
if (expected.recordTypeText) {
|
|
314
|
+
nested.push({
|
|
315
|
+
name: schemaField.name,
|
|
316
|
+
recordTypeText: expected.recordTypeText,
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const name = assertName(entity.className);
|
|
322
|
+
|
|
323
|
+
// `@schemaIgnore` means "do not check this field at all", so an ignored field
|
|
324
|
+
// is dropped from the key-set comparison too. Otherwise suppressing a field's
|
|
325
|
+
// type would still leave `__keys` failing on its presence, and the
|
|
326
|
+
// suppression would be useless.
|
|
327
|
+
// Both sides are omitted, not just the wire: dropping the field only from
|
|
328
|
+
// the schema side would turn the entity's own declaration of it into an
|
|
329
|
+
// `extraInEntity` failure.
|
|
330
|
+
const ignoredList = [...ignoredFields]
|
|
331
|
+
.sort()
|
|
332
|
+
.map((field) => `'${field}'`)
|
|
333
|
+
.join(' | ');
|
|
334
|
+
const keysFieldsType = ignoredList
|
|
335
|
+
? `Omit<${entity.fieldsTypeName}, ${ignoredList}>`
|
|
336
|
+
: entity.fieldsTypeName;
|
|
337
|
+
const keysWireType = ignoredList
|
|
338
|
+
? `Omit<${wireType}, ${ignoredList}>`
|
|
339
|
+
: wireType;
|
|
340
|
+
|
|
341
|
+
const keysCheck = entity.schemaSubset === null ? 'SameKeys' : 'SubsetKeys';
|
|
342
|
+
|
|
343
|
+
/** @type {string[]} */
|
|
344
|
+
const block = [
|
|
345
|
+
`// ${entity.className} -> ${component.name} (${entity.file})`,
|
|
346
|
+
...(entity.schemaSubset !== null
|
|
347
|
+
? [`// @schemaSubset: ${entity.schemaSubset || 'declared'}`]
|
|
348
|
+
: []),
|
|
349
|
+
// The reason is carried across too. A bare field list tells a reader that
|
|
350
|
+
// the field is not checked but not why, which is the one thing needed to
|
|
351
|
+
// judge whether the suppression is still earning its place.
|
|
352
|
+
...[...ignoredFields].sort().map((field) => {
|
|
353
|
+
const reason = entity.schemaIgnores[field];
|
|
354
|
+
return `// @schemaIgnore: ${field}${reason ? ` - ${reason}` : ''}`;
|
|
355
|
+
}),
|
|
356
|
+
`export const ${name}: {`,
|
|
357
|
+
` __keys: ${keysCheck}<${keysFieldsType}, ${keysWireType}>;`,
|
|
358
|
+
...properties,
|
|
359
|
+
`} = {`,
|
|
360
|
+
` __keys: true,`,
|
|
361
|
+
...properties.map((property) => {
|
|
362
|
+
const propertyName = /** @type {string} */ (
|
|
363
|
+
property.trim().split(':')[0]
|
|
364
|
+
);
|
|
365
|
+
return ` ${propertyName}: true,`;
|
|
366
|
+
}),
|
|
367
|
+
`};`,
|
|
368
|
+
];
|
|
369
|
+
assertionCount += 1;
|
|
370
|
+
|
|
371
|
+
// ── Record type ───────────────────────────────────────────────────────────
|
|
372
|
+
if (entity.recordTypeName && entity.recordTypeExported) {
|
|
373
|
+
addImport(specifier, entity.recordTypeName);
|
|
374
|
+
|
|
375
|
+
// The expected Record is the Fields type with each converted member
|
|
376
|
+
// substituted. An ignored field has no expectation to substitute - the
|
|
377
|
+
// generator skipped it - so comparing it here would re-impose the very
|
|
378
|
+
// check `@schemaIgnore` waived, and an entity that ignores a nested
|
|
379
|
+
// field could never declare a Record for it. Drop it from both sides.
|
|
380
|
+
const substituted = nested.map((field) => `'${field.name}'`);
|
|
381
|
+
const omitted = [...substituted, ...ignoredList.split(' | ')].filter(
|
|
382
|
+
Boolean,
|
|
383
|
+
);
|
|
384
|
+
|
|
385
|
+
const body = nested.length
|
|
386
|
+
? ` & {\n${nested
|
|
387
|
+
.map(
|
|
388
|
+
(field) =>
|
|
389
|
+
` ${propertyKey(field.name)}: ${field.recordTypeText};`,
|
|
390
|
+
)
|
|
391
|
+
.join('\n')}\n }`
|
|
392
|
+
: '';
|
|
393
|
+
const expectedRecord = omitted.length
|
|
394
|
+
? `Omit<${entity.fieldsTypeName}, ${omitted.join(' | ')}>${body}`
|
|
395
|
+
: entity.fieldsTypeName;
|
|
396
|
+
|
|
397
|
+
const actualRecord = ignoredList
|
|
398
|
+
? `Omit<RecordFieldsOf<${entity.recordTypeName}>, ${ignoredList}>`
|
|
399
|
+
: `RecordFieldsOf<${entity.recordTypeName}>`;
|
|
400
|
+
|
|
401
|
+
block.push(
|
|
402
|
+
'',
|
|
403
|
+
`export const ${name}Record: Exact<`,
|
|
404
|
+
` ${actualRecord},`,
|
|
405
|
+
` ${expectedRecord}`,
|
|
406
|
+
`> = true;`,
|
|
407
|
+
);
|
|
408
|
+
assertionCount += 1;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
blocks.push(block.join('\n'));
|
|
412
|
+
entityCount += 1;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// ── Choices dicts ───────────────────────────────────────────────────────────
|
|
416
|
+
// Asserted against the component directly rather than through the field that
|
|
417
|
+
// uses them. A field-level check cannot be bidirectional: the broker widens a
|
|
418
|
+
// blank-able field to `StateEnum | BlankEnum`, so `Exact` there would demand
|
|
419
|
+
// the dict carry `''`. Comparing the dict with its own component instead pins
|
|
420
|
+
// the values in both directions - a value dropped upstream and a value the app
|
|
421
|
+
// invented both fail - and keeps working for `SupplierStatus`, whose field is
|
|
422
|
+
// deliberately `@schemaIgnore`d.
|
|
423
|
+
for (const resolution of enumResolutions) {
|
|
424
|
+
if (!resolution.component) continue;
|
|
425
|
+
|
|
426
|
+
if (!resolution.typeName) {
|
|
427
|
+
skipped.push({
|
|
428
|
+
className: resolution.name,
|
|
429
|
+
reason: `no exported type alias to assert against`,
|
|
430
|
+
});
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
addImport(
|
|
435
|
+
moduleSpecifier(resolution.file, assertsDir),
|
|
436
|
+
resolution.typeName,
|
|
437
|
+
);
|
|
438
|
+
|
|
439
|
+
blocks.push(
|
|
440
|
+
[
|
|
441
|
+
`// ${resolution.name} -> ${resolution.component} (${resolution.file})`,
|
|
442
|
+
`export const assert${resolution.name}: Exact<`,
|
|
443
|
+
` ${resolution.typeName},`,
|
|
444
|
+
` components['schemas']['${resolution.component}']`,
|
|
445
|
+
`> = true;`,
|
|
446
|
+
].join('\n'),
|
|
447
|
+
);
|
|
448
|
+
assertionCount += 1;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
const importLines = [...imports.entries()]
|
|
452
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
453
|
+
.map(
|
|
454
|
+
([specifier, names]) =>
|
|
455
|
+
`import type { ${[...names].sort().join(', ')} } from '${specifier}';`,
|
|
456
|
+
);
|
|
457
|
+
|
|
458
|
+
const skippedComment = skipped.length
|
|
459
|
+
? [
|
|
460
|
+
'',
|
|
461
|
+
'/*',
|
|
462
|
+
' * Not asserted:',
|
|
463
|
+
...skipped.map((entry) => ` * - ${entry.className}: ${entry.reason}`),
|
|
464
|
+
' */',
|
|
465
|
+
].join('\n')
|
|
466
|
+
: '';
|
|
467
|
+
|
|
468
|
+
const source = [
|
|
469
|
+
prelude(schemaSpecifier),
|
|
470
|
+
importLines.join('\n'),
|
|
471
|
+
skippedComment,
|
|
472
|
+
'',
|
|
473
|
+
blocks.join('\n\n'),
|
|
474
|
+
'',
|
|
475
|
+
].join('\n');
|
|
476
|
+
|
|
477
|
+
return { source, skipped, entityCount, assertionCount };
|
|
478
|
+
}
|