oas-truth 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 +23 -0
- package/dist/index.d.ts +43 -1
- package/dist/index.js +45 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,6 +28,29 @@ if (result.ok) {
|
|
|
28
28
|
- TypeSpec files (`.tsp`) are compiled with `@typespec/compiler` and then bundled
|
|
29
29
|
- Returns `{ ok: true, value } | { ok: false, error }` (never throws)
|
|
30
30
|
|
|
31
|
+
## Component schemas
|
|
32
|
+
|
|
33
|
+
`makeSchemasCode` emits one file: the library import and every `components.schemas` declaration, in
|
|
34
|
+
dependency order. `makeSchemaDeclarations` returns the same declarations one by one (`name`,
|
|
35
|
+
`varName`, `fileName`, `importLine`, `code`) so a host can split a file per schema.
|
|
36
|
+
|
|
37
|
+
Recursive `$ref` cycles keep a helper type so the const can be annotated (`z.ZodType<NodeType>`,
|
|
38
|
+
`v.GenericSchema<NodeType>`, `Schema.Codec<NodeType>`). That helper matches what the library
|
|
39
|
+
infers — `| undefined` on Valibot and Effect optionals, `readonly` arrays on Effect — so it
|
|
40
|
+
type-checks under `exactOptionalPropertyTypes`. Arktype cycles become
|
|
41
|
+
`scope({...}).export().Member`; that declaration's `importLine` is then
|
|
42
|
+
`import { type, scope } from 'arktype'`. `schemaImportLine` is the same function
|
|
43
|
+
`makeSchemasCode` uses when the declarations stay in one file.
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { makeAdapter, makeSchemaDeclarations } from 'oas-truth'
|
|
47
|
+
|
|
48
|
+
const declarations = makeSchemaDeclarations(schemas, makeAdapter('arktype'))
|
|
49
|
+
// a cyclic file: import { type, scope } from 'arktype'
|
|
50
|
+
// a non-cyclic file: import { type } from 'arktype'
|
|
51
|
+
const source = `${declarations[0]?.importLine}\n\n${declarations[0]?.code}`
|
|
52
|
+
```
|
|
53
|
+
|
|
31
54
|
## License
|
|
32
55
|
|
|
33
56
|
[MIT](https://github.com/nakita628/oas-truth/blob/main/LICENSE)
|
package/dist/index.d.ts
CHANGED
|
@@ -502,6 +502,24 @@ export type Callbacks = {
|
|
|
502
502
|
//#region src/adapter/index.d.ts
|
|
503
503
|
export type ParamIn = 'query' | 'path';
|
|
504
504
|
export type SchemaLib = 'zod' | 'valibot' | 'arktype' | 'effect' | 'typebox';
|
|
505
|
+
/**
|
|
506
|
+
* Flags `renderImport` uses when the schemas builder needs more than the
|
|
507
|
+
* library's default line — Arktype `scope` for a `$ref` cycle, TypeBox
|
|
508
|
+
* `Static` when type aliases are exported.
|
|
509
|
+
*/
|
|
510
|
+
export type RenderImportOptions = {
|
|
511
|
+
readonly cyclic?: boolean;
|
|
512
|
+
readonly exportTypes?: boolean;
|
|
513
|
+
};
|
|
514
|
+
/**
|
|
515
|
+
* How this library's inferred object type differs from a Zod-shaped
|
|
516
|
+
* `prop?: T` literal, so the TS7022 helper matches `cyclicAnnotation` under
|
|
517
|
+
* `exactOptionalPropertyTypes`. Omitted properties keep the Zod form.
|
|
518
|
+
*/
|
|
519
|
+
export type CyclicTypeStyle = {
|
|
520
|
+
readonly optionalUndefined?: boolean;
|
|
521
|
+
readonly readonlyArrays?: boolean;
|
|
522
|
+
};
|
|
505
523
|
/**
|
|
506
524
|
* The minimal surface the component generators consume from a validator
|
|
507
525
|
* adapter. Each library's full adapter (carrying `toNamedExport`,
|
|
@@ -518,16 +536,18 @@ export type SchemaLib = 'zod' | 'valibot' | 'arktype' | 'effect' | 'typebox';
|
|
|
518
536
|
* Cycle handling lives next to each library: `wrapLazy` re-wraps a `$ref`
|
|
519
537
|
* inside a strongly connected component (Zod, Valibot, Effect);
|
|
520
538
|
* `cyclicAnnotation` is the TS7022 annotation on that declaration;
|
|
539
|
+
* `cyclicTypeStyle` is the helper type that annotation names;
|
|
521
540
|
* `renderCyclic` post-processes the `$defs` container TypeBox and Arktype
|
|
522
541
|
* need (`Type.Cyclic` is already the expression; Arktype extracts `scope`).
|
|
523
542
|
*/
|
|
524
543
|
export type ComponentAdapter = {
|
|
525
|
-
readonly renderImport: () => string;
|
|
544
|
+
readonly renderImport: (options?: RenderImportOptions) => string;
|
|
526
545
|
readonly toExpression: (schema: Schema, paramIn?: ParamIn) => string;
|
|
527
546
|
readonly renderTypeInfer: (constName: string) => string;
|
|
528
547
|
readonly wrapSchema?: (expr: string) => string;
|
|
529
548
|
readonly wrapLazy?: (varName: string) => string;
|
|
530
549
|
readonly cyclicAnnotation?: (typeName: string) => string;
|
|
550
|
+
readonly cyclicTypeStyle?: CyclicTypeStyle;
|
|
531
551
|
readonly renderCyclic?: (varName: string, containerExpr: string, externals: readonly string[]) => string;
|
|
532
552
|
readonly reservedTypeNames?: readonly string[];
|
|
533
553
|
};
|
|
@@ -598,6 +618,13 @@ type SchemasOptions = {
|
|
|
598
618
|
* builders (`UserSchema`); `key` uses the OpenAPI key (`User`).
|
|
599
619
|
*/
|
|
600
620
|
readonly typeAlias?: 'const' | 'key';
|
|
621
|
+
/**
|
|
622
|
+
* Shape of the TS7022 helper type. `'library'` (default) matches what the
|
|
623
|
+
* validator infers, so `v.GenericSchema<Helper>` / `Schema.Codec<Helper>`
|
|
624
|
+
* type-check under `exactOptionalPropertyTypes`. `'literal'` is the
|
|
625
|
+
* Zod-shaped `prop?: T` form for every library.
|
|
626
|
+
*/
|
|
627
|
+
readonly cyclicTypeStyle?: 'library' | 'literal';
|
|
601
628
|
/**
|
|
602
629
|
* Host hook around each declaration expression — e.g. hono-openapi ref
|
|
603
630
|
* registration. Applied after cycle handling.
|
|
@@ -608,8 +635,22 @@ type SchemaDeclaration = {
|
|
|
608
635
|
readonly name: string;
|
|
609
636
|
readonly varName: string;
|
|
610
637
|
readonly fileName: string;
|
|
638
|
+
/**
|
|
639
|
+
* The library import this declaration needs when written to its own file.
|
|
640
|
+
* An Arktype cycle that is only `scope({...}).export().Member` gets
|
|
641
|
+
* `import { type, scope } from 'arktype'`; a non-cyclic file stays
|
|
642
|
+
* `import { type } from 'arktype'`.
|
|
643
|
+
*/
|
|
644
|
+
readonly importLine: string;
|
|
611
645
|
readonly code: string;
|
|
612
646
|
};
|
|
647
|
+
/**
|
|
648
|
+
* The import line a schemas file needs. `cyclic` is true when this file (or
|
|
649
|
+
* the bundled document) contains an Arktype `scope` / TypeBox container
|
|
650
|
+
* cycle — split files that are only `scope({...}).export().Member` still
|
|
651
|
+
* have to import `scope`.
|
|
652
|
+
*/
|
|
653
|
+
export declare function schemaImportLine(adapter: ComponentAdapter, options?: SchemasOptions, cyclic?: boolean): string;
|
|
613
654
|
/**
|
|
614
655
|
* One `export const <X>Schema=...` declaration per `components.schemas` entry,
|
|
615
656
|
* in dependency-first order. Only references inside a `$ref` cycle are lazy
|
|
@@ -622,6 +663,7 @@ export declare function makeSchemaDeclarations(schemas: {
|
|
|
622
663
|
name: string;
|
|
623
664
|
varName: string;
|
|
624
665
|
fileName: string;
|
|
666
|
+
importLine: string;
|
|
625
667
|
code: string;
|
|
626
668
|
}[];
|
|
627
669
|
/**
|
package/dist/index.js
CHANGED
|
@@ -129,7 +129,8 @@ function makeValibotAdapter() {
|
|
|
129
129
|
},
|
|
130
130
|
cyclicAnnotation(typeName) {
|
|
131
131
|
return `v.GenericSchema<${typeName}>`;
|
|
132
|
-
}
|
|
132
|
+
},
|
|
133
|
+
cyclicTypeStyle: { optionalUndefined: true }
|
|
133
134
|
};
|
|
134
135
|
}
|
|
135
136
|
function makeArktypeAdapter() {
|
|
@@ -142,8 +143,8 @@ function makeArktypeAdapter() {
|
|
|
142
143
|
...paramIn && { paramIn }
|
|
143
144
|
}));
|
|
144
145
|
},
|
|
145
|
-
renderImport() {
|
|
146
|
-
return "import { type } from 'arktype'";
|
|
146
|
+
renderImport(options) {
|
|
147
|
+
return options?.cyclic === true ? "import { type, scope } from 'arktype'" : "import { type } from 'arktype'";
|
|
147
148
|
},
|
|
148
149
|
renderTypeInfer(constName) {
|
|
149
150
|
return `export type ${constName}=typeof ${constName}.infer`;
|
|
@@ -162,8 +163,8 @@ function makeTypeboxAdapter() {
|
|
|
162
163
|
...paramIn && { paramIn }
|
|
163
164
|
}));
|
|
164
165
|
},
|
|
165
|
-
renderImport() {
|
|
166
|
-
return "import { Type } from '@sinclair/typebox'";
|
|
166
|
+
renderImport(options) {
|
|
167
|
+
return options?.exportTypes === true ? "import { Type, type Static } from '@sinclair/typebox'" : "import { Type } from '@sinclair/typebox'";
|
|
167
168
|
},
|
|
168
169
|
renderTypeInfer(constName) {
|
|
169
170
|
return `export type ${constName}=Static<typeof ${constName}>`;
|
|
@@ -194,8 +195,12 @@ function makeEffectAdapter() {
|
|
|
194
195
|
wrapLazy(varName) {
|
|
195
196
|
return `Schema.suspend(() => ${varName})`;
|
|
196
197
|
},
|
|
197
|
-
cyclicAnnotation() {
|
|
198
|
-
return
|
|
198
|
+
cyclicAnnotation(typeName) {
|
|
199
|
+
return `Schema.Codec<${typeName}>`;
|
|
200
|
+
},
|
|
201
|
+
cyclicTypeStyle: {
|
|
202
|
+
optionalUndefined: true,
|
|
203
|
+
readonlyArrays: true
|
|
199
204
|
},
|
|
200
205
|
reservedTypeNames: ["Schema", "Effect"]
|
|
201
206
|
};
|
|
@@ -487,8 +492,8 @@ function analyzeSchemas(schemas) {
|
|
|
487
492
|
cycles
|
|
488
493
|
};
|
|
489
494
|
}
|
|
490
|
-
function makeTypeString(schema, self, infer, readonly) {
|
|
491
|
-
const recurse = (s) => makeTypeString(s, self, infer, readonly);
|
|
495
|
+
function makeTypeString(schema, self, infer, readonly, style) {
|
|
496
|
+
const recurse = (s) => makeTypeString(s, self, infer, readonly, style);
|
|
492
497
|
const ro = readonly ? "readonly " : "";
|
|
493
498
|
if (schema.$ref) {
|
|
494
499
|
const name = schemaRefToName(schema.$ref);
|
|
@@ -501,6 +506,8 @@ function makeTypeString(schema, self, infer, readonly) {
|
|
|
501
506
|
if (schema.const !== void 0) return JSON.stringify(schema.const);
|
|
502
507
|
const types = schema.type === void 0 ? ["object"] : [schema.type].flat();
|
|
503
508
|
const nullable = schema.nullable === true || types.includes("null");
|
|
509
|
+
const arrayRo = readonly || style.readonlyArrays === true ? "readonly " : "";
|
|
510
|
+
const optionalUndef = style.optionalUndefined === true ? "|undefined" : "";
|
|
504
511
|
const base = types.filter((type) => type !== "null").map((type) => {
|
|
505
512
|
if (type === "string") return "string";
|
|
506
513
|
if (type === "number" || type === "integer") return "number";
|
|
@@ -508,10 +515,13 @@ function makeTypeString(schema, self, infer, readonly) {
|
|
|
508
515
|
if (type === "array") {
|
|
509
516
|
const items = schema.items;
|
|
510
517
|
const item = isRecord(items) ? recurse(items) : "unknown";
|
|
511
|
-
return `${
|
|
518
|
+
return `${arrayRo}(${item})[]`;
|
|
512
519
|
}
|
|
513
520
|
if (type === "object") {
|
|
514
|
-
const props = Object.entries(schema.properties ?? {}).map(([key, prop]) =>
|
|
521
|
+
const props = Object.entries(schema.properties ?? {}).map(([key, prop]) => {
|
|
522
|
+
const required = schema.required?.includes(key) === true;
|
|
523
|
+
return `${ro}${JSON.stringify(key)}${required ? "" : "?"}:${recurse(prop)}${required ? "" : optionalUndef}`;
|
|
524
|
+
});
|
|
515
525
|
const extra = isRecord(schema.additionalProperties) ? [`${ro}[key:string]:${recurse(schema.additionalProperties)}`] : props.length === 0 ? [`${ro}[key:string]:unknown`] : [];
|
|
516
526
|
return `{${[...props, ...extra].join(";")}}`;
|
|
517
527
|
}
|
|
@@ -523,13 +533,15 @@ function makeTypeString(schema, self, infer, readonly) {
|
|
|
523
533
|
/**
|
|
524
534
|
* The TypeScript shape of a recursive schema, so its declaration can carry an
|
|
525
535
|
* explicit `z.ZodType<XType>` / `v.GenericSchema<XType>` annotation (TS7022).
|
|
526
|
-
* References to other schemas go through their inferred types.
|
|
536
|
+
* References to other schemas go through their inferred types. `style` is the
|
|
537
|
+
* library's inferred object type (Valibot `| undefined`, Effect `readonly`
|
|
538
|
+
* arrays) so the annotation type-checks under `exactOptionalPropertyTypes`.
|
|
527
539
|
*/
|
|
528
|
-
function makeCyclicType(name, typeName, schema, infer, readonly) {
|
|
540
|
+
function makeCyclicType(name, typeName, schema, infer, readonly, style = {}) {
|
|
529
541
|
return `type ${typeName}=${makeTypeString(schema, {
|
|
530
542
|
name,
|
|
531
543
|
typeName
|
|
532
|
-
}, infer, readonly)}`;
|
|
544
|
+
}, infer, readonly, style)}`;
|
|
533
545
|
}
|
|
534
546
|
//#endregion
|
|
535
547
|
//#region src/helper/identifiers.ts
|
|
@@ -676,11 +688,19 @@ function makeCyclicContainer(varName, group, schemas, identifiers, adapter) {
|
|
|
676
688
|
$defs
|
|
677
689
|
});
|
|
678
690
|
}
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
691
|
+
/**
|
|
692
|
+
* The import line a schemas file needs. `cyclic` is true when this file (or
|
|
693
|
+
* the bundled document) contains an Arktype `scope` / TypeBox container
|
|
694
|
+
* cycle — split files that are only `scope({...}).export().Member` still
|
|
695
|
+
* have to import `scope`.
|
|
696
|
+
*/
|
|
697
|
+
function schemaImportLine(adapter, options, cyclic = false) {
|
|
698
|
+
const exportTypes = options?.exportTypes === true;
|
|
699
|
+
if (!cyclic && !exportTypes) return adapter.renderImport();
|
|
700
|
+
return adapter.renderImport({
|
|
701
|
+
...cyclic && { cyclic: true },
|
|
702
|
+
...exportTypes && { exportTypes: true }
|
|
703
|
+
});
|
|
684
704
|
}
|
|
685
705
|
function exportedTypeName(ident, varName, adapter, options) {
|
|
686
706
|
if (options?.typeAlias !== "key") return varName;
|
|
@@ -705,6 +725,7 @@ function makeSchemaDeclarations(schemas, adapter, options) {
|
|
|
705
725
|
...adapter.reservedTypeNames ?? []
|
|
706
726
|
]);
|
|
707
727
|
const infer = (varName) => adapter.renderTypeInfer(varName).replace(`export type ${varName}=`, "");
|
|
728
|
+
const style = options?.cyclicTypeStyle === "literal" ? {} : adapter.cyclicTypeStyle ?? {};
|
|
708
729
|
return order.map((name) => {
|
|
709
730
|
const schema = prepared[name] ?? {};
|
|
710
731
|
const ident = identifiers.get(name) ?? toIdentifierPascalCase(name);
|
|
@@ -724,13 +745,15 @@ function makeSchemaDeclarations(schemas, adapter, options) {
|
|
|
724
745
|
const annotationText = helper ? adapter.cyclicAnnotation?.(helper) : void 0;
|
|
725
746
|
const usesHelper = helper !== void 0 && annotationText?.includes(helper) === true;
|
|
726
747
|
if (helper && usesHelper) taken.add(helper);
|
|
727
|
-
const typeDef = helper && usesHelper ? `${makeCyclicType(name, helper, schema, (ref) => infer(`${identifiers.get(ref) ?? toIdentifierPascalCase(ref)}Schema`), options?.readonly === true)}\n\n` : "";
|
|
748
|
+
const typeDef = helper && usesHelper ? `${makeCyclicType(name, helper, schema, (ref) => infer(`${identifiers.get(ref) ?? toIdentifierPascalCase(ref)}Schema`), options?.readonly === true, style)}\n\n` : "";
|
|
728
749
|
const annotation = annotationText === void 0 ? "" : `:${annotationText}`;
|
|
729
750
|
const typeExport = options?.exportTypes === true ? `\n\nexport type ${exportedTypeName(ident, varName, adapter, options)}=${infer(varName)}` : "";
|
|
751
|
+
const containerCycle = group !== void 0 && adapter.wrapLazy === void 0;
|
|
730
752
|
return {
|
|
731
753
|
name,
|
|
732
754
|
varName,
|
|
733
755
|
fileName: fileNameOf(ident),
|
|
756
|
+
importLine: schemaImportLine(adapter, options, containerCycle),
|
|
734
757
|
code: `${typeDef}export const ${varName}${annotation}=${value}${typeExport}`
|
|
735
758
|
};
|
|
736
759
|
});
|
|
@@ -744,7 +767,7 @@ function makeSchemasCode(components, adapter, options) {
|
|
|
744
767
|
if (!schemas) return "";
|
|
745
768
|
const declarations = makeSchemaDeclarations(schemas, adapter, options);
|
|
746
769
|
if (declarations.length === 0) return "";
|
|
747
|
-
return `${schemaImportLine(adapter, options,
|
|
770
|
+
return `${schemaImportLine(adapter, options, declarations.some((d) => d.importLine !== schemaImportLine(adapter, options)))}\n\n${declarations.map((d) => d.code).join(";")}\n`;
|
|
748
771
|
}
|
|
749
772
|
//#endregion
|
|
750
773
|
//#region src/generator/components/security-schemes.ts
|
|
@@ -759,4 +782,4 @@ function makeSecuritySchemesCode(components, readonly) {
|
|
|
759
782
|
}).join(";")}\n`;
|
|
760
783
|
}
|
|
761
784
|
//#endregion
|
|
762
|
-
export { isRecord, makeAdapter, makeCallbacksCode, makeExamplesCode, makeHeadersCode, makeLinksCode, makeParametersCode, makePathItemsCode, makeRequestBodiesCode, makeResponsesCode, makeSafeKey, makeSchemaDeclarations, makeSchemaReplacements, makeSchemasCode, makeSecuritySchemesCode, parseOpenAPI, schemaRefToName, toIdentifierPascalCase, toPascalCase, valueToCode };
|
|
785
|
+
export { isRecord, makeAdapter, makeCallbacksCode, makeExamplesCode, makeHeadersCode, makeLinksCode, makeParametersCode, makePathItemsCode, makeRequestBodiesCode, makeResponsesCode, makeSafeKey, makeSchemaDeclarations, makeSchemaReplacements, makeSchemasCode, makeSecuritySchemesCode, parseOpenAPI, schemaImportLine, schemaRefToName, toIdentifierPascalCase, toPascalCase, valueToCode };
|