@amritk/generate-examples 0.0.0 → 0.1.1

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.
@@ -0,0 +1,32 @@
1
+ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
2
+ /**
3
+ * Represents a generated TypeScript file with its filename and content.
4
+ */
5
+ export type GeneratedFile = {
6
+ filename: string;
7
+ content: string;
8
+ };
9
+ /**
10
+ * Builds all TypeScript example files from a JSON Schema by traversing all
11
+ * $ref references recursively, mirroring the generate-parsers pipeline.
12
+ *
13
+ * Each generated file exports:
14
+ * - A TypeScript type definition
15
+ * - A `fast-check` arbitrary (`FooArbitrary`) that produces schema-valid values
16
+ * - A concrete example value (`fooExample`)
17
+ *
18
+ * An `index.ts` re-exports everything. The generated output imports `fast-check`,
19
+ * which consumers must install as a (dev) dependency.
20
+ *
21
+ * @param rootSchema - The root JSON Schema to build from
22
+ * @param rootTypeName - The name for the root type (e.g. "Document")
23
+ * @param typeSuffix - Suffix appended to every `$ref`-derived name (default `''`)
24
+ * @returns An array of generated TypeScript files
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * const files = await buildExampleSchema(schema, 'Document')
29
+ * // files → [{ filename: 'document.ts', content: '...' }, { filename: 'index.ts', ... }]
30
+ * ```
31
+ */
32
+ export declare const buildExampleSchema: (rootSchema: JSONSchema, rootTypeName: string, typeSuffix?: string) => Promise<GeneratedFile[]>;
@@ -0,0 +1,34 @@
1
+ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
2
+ /**
3
+ * Options for controlling how example imports are collected.
4
+ */
5
+ type CollectExampleImportsOptions = {
6
+ /**
7
+ * The $ref path of the schema being generated (e.g. `#/$defs/address`).
8
+ * Prevents a file from importing itself.
9
+ */
10
+ readonly selfRef?: string | undefined;
11
+ /**
12
+ * The root schema document. URI refs that cannot be resolved within it
13
+ * are excluded from the import list (they were never generated as files).
14
+ */
15
+ readonly rootSchema?: Record<string, unknown> | undefined;
16
+ /**
17
+ * Suffix appended to every type/arbitrary name derived from a `$ref`. Must
18
+ * match the suffix used when generating the referenced files. Defaults to `''`.
19
+ */
20
+ readonly typeSuffix?: string;
21
+ };
22
+ /**
23
+ * Collects import statements for all $ref dependencies of a schema. Each import
24
+ * brings in both the generated TypeScript type and the arbitrary for that ref.
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * const schema = { properties: { address: { $ref: '#/$defs/address' } } }
29
+ * collectExampleImports(schema)
30
+ * // ["import { type Address, AddressArbitrary } from './address'"]
31
+ * ```
32
+ */
33
+ export declare const collectExampleImports: (schema: JSONSchema, options?: CollectExampleImportsOptions) => string[];
34
+ export {};
@@ -0,0 +1,29 @@
1
+ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
2
+ /**
3
+ * Derives a single concrete, schema-valid value from a JSON Schema.
4
+ *
5
+ * Prefers explicit hints in this order: `const`, `examples[0]`, `default`,
6
+ * `enum[0]`; otherwise produces a canonical value for the declared type.
7
+ * `$ref`s are resolved and inlined by value; recursive refs short-circuit to
8
+ * `null` (tracked via `seen`).
9
+ *
10
+ * Note: values constrained only by `pattern` are not guaranteed to match the
11
+ * pattern — use the generated arbitrary when pattern fidelity matters.
12
+ */
13
+ export declare const deriveExample: (schema: JSONSchema, rootSchema?: Record<string, unknown>, seen?: ReadonlySet<string>) => unknown;
14
+ /**
15
+ * Serializes a derived value into a TypeScript source expression. Handles the
16
+ * non-JSON values `deriveExample` can produce (`Date`, `bigint`) in addition to
17
+ * plain JSON.
18
+ */
19
+ export declare const serializeValue: (value: unknown) => string;
20
+ /**
21
+ * Generates an exported const holding a concrete, schema-valid example value.
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * generateExampleConst({ type: 'object', properties: { name: { type: 'string' } } }, 'Info')
26
+ * // export const infoExample: Info = { "name": "string" }
27
+ * ```
28
+ */
29
+ export declare const generateExampleConst: (schema: JSONSchema, typeName: string, rootSchema?: Record<string, unknown>) => string;
@@ -0,0 +1,11 @@
1
+ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
2
+ /**
3
+ * Generates a `fast-check` arbitrary that produces schema-valid values.
4
+ *
5
+ * @example
6
+ * ```typescript
7
+ * generateArbitrary({ type: 'object', properties: { name: { type: 'string' } }, required: ['name'] }, 'Info')
8
+ * // export const InfoArbitrary: fc.Arbitrary<Info> = fc.record({ "name": fc.string() })
9
+ * ```
10
+ */
11
+ export declare const generateArbitrary: (schema: JSONSchema, typeName: string, suffix?: string) => string;
@@ -0,0 +1,42 @@
1
+ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
2
+ /**
3
+ * Options for controlling what gets generated in an example file.
4
+ */
5
+ type GenerateExampleFileOptions = {
6
+ /**
7
+ * The $ref path of the schema being generated (e.g. `#/$defs/address`).
8
+ * Prevents the file from importing itself.
9
+ */
10
+ readonly selfRef?: string;
11
+ /**
12
+ * The root schema document. Used to resolve `$ref`s when deriving a concrete
13
+ * example value, and to filter out unresolvable refs from the import list.
14
+ */
15
+ readonly rootSchema?: Record<string, unknown>;
16
+ /**
17
+ * Suffix appended to every type/arbitrary name derived from a `$ref`.
18
+ * Defaults to `''` (no suffix).
19
+ */
20
+ readonly typeSuffix?: string;
21
+ };
22
+ /**
23
+ * Generates a complete TypeScript example file from a JSON Schema.
24
+ *
25
+ * The file contains:
26
+ * - An import of `fast-check` and imports for any `$ref` types and arbitraries
27
+ * - The exported TypeScript type definition
28
+ * - An exported `fast-check` arbitrary (`FooArbitrary`)
29
+ * - An exported concrete example value (`fooExample`)
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * const schema = { type: 'object', properties: { title: { type: 'string' } }, required: ['title'] }
34
+ * generateExampleFile(schema, 'Info')
35
+ * // import * as fc from 'fast-check'
36
+ * // export type Info = { title: string }
37
+ * // export const InfoArbitrary: fc.Arbitrary<Info> = fc.record({ "title": fc.string() })
38
+ * // export const infoExample: Info = { "title": "string" }
39
+ * ```
40
+ */
41
+ export declare const generateExampleFile: (schema: JSONSchema, typeName: string, options?: GenerateExampleFileOptions) => string;
42
+ export {};
@@ -0,0 +1,4 @@
1
+ export type { GeneratedFile } from './generators/build-schema';
2
+ export { buildExampleSchema } from './generators/build-schema';
3
+ export { deriveExample, generateExampleConst, serializeValue } from './generators/derive-example';
4
+ export { generateArbitrary } from './generators/generate-arbitrary';