@amritk/generate-parsers 0.14.0 → 0.15.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 CHANGED
@@ -9,7 +9,7 @@
9
9
  ![license](https://img.shields.io/badge/license-MIT-22c55e?style=flat-square) 
10
10
  ![JSON Schema](https://img.shields.io/badge/JSON%20Schema-2020--12-f97316?style=flat-square) 
11
11
  ![node](https://img.shields.io/badge/node-%E2%89%A520-339933?style=flat-square&logo=node.js&logoColor=white) 
12
- ![vibe coded](https://img.shields.io/badge/vibe%20coded-84%25-a855f7?style=flat-square)
12
+ ![vibe coded](https://img.shields.io/badge/vibe-coded-a855f7?style=flat-square)
13
13
 
14
14
  </div>
15
15
 
@@ -48,8 +48,11 @@ export type GeneratedFile = {
48
48
  * is used verbatim and is not affected by this suffix.
49
49
  * @param importExt - Extension used on every relative import specifier in the generated
50
50
  * output (cross-file `$ref` imports, the index barrel, and embedded-helper imports).
51
- * `'js'` (default) is the standard TS NodeNext form; `'ts'` emits the literal on-disk
52
- * paths so the generated sources run directly under Node's type stripping.
51
+ * `'js'` (this function's default) is the standard TS NodeNext form; `'ts'` emits the
52
+ * literal on-disk paths so the generated sources run directly under Node's type
53
+ * stripping. Note the `mjst` CLI defaults this to `'ts'` (falling back to `'js'` only
54
+ * under `--build`, where tsc must compile the sources) so generated output runs under
55
+ * Node without a build step; direct callers of `buildSchema` opt in explicitly.
53
56
  * @returns An array of generated TypeScript files
54
57
  *
55
58
  * @example
@@ -84,8 +84,11 @@ const readHelperSource = async (helper, importExt) => {
84
84
  * is used verbatim and is not affected by this suffix.
85
85
  * @param importExt - Extension used on every relative import specifier in the generated
86
86
  * output (cross-file `$ref` imports, the index barrel, and embedded-helper imports).
87
- * `'js'` (default) is the standard TS NodeNext form; `'ts'` emits the literal on-disk
88
- * paths so the generated sources run directly under Node's type stripping.
87
+ * `'js'` (this function's default) is the standard TS NodeNext form; `'ts'` emits the
88
+ * literal on-disk paths so the generated sources run directly under Node's type
89
+ * stripping. Note the `mjst` CLI defaults this to `'ts'` (falling back to `'js'` only
90
+ * under `--build`, where tsc must compile the sources) so generated output runs under
91
+ * Node without a build step; direct callers of `buildSchema` opt in explicitly.
89
92
  * @returns An array of generated TypeScript files
90
93
  *
91
94
  * @example
@@ -137,6 +140,7 @@ export const buildSchema = async (rootSchema, rootTypeName, extensions, typesOnl
137
140
  readonly,
138
141
  typeSuffix,
139
142
  importExt,
143
+ isRoot: node.isRoot,
140
144
  ...(node.ref !== undefined ? { selfRef: node.ref } : {}),
141
145
  ...(logWarnings !== undefined ? { logWarnings } : {}),
142
146
  ...(strict !== undefined ? { strict } : {}),
@@ -71,6 +71,13 @@ type GenerateFileOptions = {
71
71
  * stripping.
72
72
  */
73
73
  readonly importExt?: ImportExtension;
74
+ /**
75
+ * True when this file is the root document rather than a `$ref`-reached
76
+ * definition. Forwarded to the parser generator so the JSON Schema
77
+ * meta-schema special case (type name `Schema`) never applies to a
78
+ * user-named root.
79
+ */
80
+ readonly isRoot?: boolean;
74
81
  };
75
82
  /** Result of generating a single parser file. */
76
83
  export type GeneratedFileContent = {
@@ -62,6 +62,7 @@ export const generateFile = (schema, typeName, options) => {
62
62
  useRefImports: true,
63
63
  typeSuffix,
64
64
  reservedNames,
65
+ ...(options?.isRoot !== undefined ? { isRoot: options.isRoot } : {}),
65
66
  ...(rootSchema !== undefined ? { rootSchema } : {}),
66
67
  ...(options?.logWarnings !== undefined ? { logWarnings: options.logWarnings } : {}),
67
68
  ...(options?.strict !== undefined ? { strict: options.strict } : {}),
@@ -50,6 +50,14 @@ type GenerateParserOptions = {
50
50
  * validate against the wrong schema.
51
51
  */
52
52
  readonly reservedNames?: ReadonlySet<string>;
53
+ /**
54
+ * True when this schema is the root document (not a `$ref`-reached definition).
55
+ * The root type name is user-derived (schema `title`, filename, or `--root-type`),
56
+ * so the JSON Schema meta-schema special case — keyed on the literal name
57
+ * `Schema` — must not apply to it, or a common `schema.json` root would silently
58
+ * generate a validation-free pass-through parser.
59
+ */
60
+ readonly isRoot?: boolean;
53
61
  };
54
62
  /**
55
63
  * Generates a safe parser as an arrow function that never throws.
@@ -1525,8 +1525,12 @@ const selectParserStrategy = (schema, typeName, options) => {
1525
1525
  const stripUnknown = options?.stripUnknown ?? false;
1526
1526
  const suffix = options?.typeSuffix ?? '';
1527
1527
  // Special case for the self-referential JSON Schema meta-schema type (e.g.
1528
- // `Schema` / `SchemaObject`) - it can be any JSON Schema.
1529
- if (typeName === `Schema${suffix}`) {
1528
+ // `Schema` / `SchemaObject`) - it can be any JSON Schema. This is an OpenAPI
1529
+ // heuristic for a `$defs`/`components.schemas` entry named `schema`; it must
1530
+ // never fire for the root document, whose name is user-derived (a `schema.json`
1531
+ // file naturally yields the root type `Schema`) and would otherwise collapse
1532
+ // to a validation-free pass-through parser.
1533
+ if (!options?.isRoot && typeName === `Schema${suffix}`) {
1530
1534
  return generateSchemaObjectParser(typeName);
1531
1535
  }
1532
1536
  const isObjectLikeSchema = isObjectSchema(schema) ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/generate-parsers",
3
- "version": "0.14.0",
3
+ "version": "0.15.0",
4
4
  "description": "Generate TypeScript parsers and type definitions from JSON Schemas.",
5
5
  "module": "./dist/index.js",
6
6
  "type": "module",
@@ -48,7 +48,7 @@
48
48
  },
49
49
  "dependencies": {
50
50
  "json-schema-typed": "^8.0.1",
51
- "@amritk/helpers": "0.11.0",
51
+ "@amritk/helpers": "0.12.0",
52
52
  "@amritk/generate-markdown": "0.4.1"
53
53
  },
54
54
  "devDependencies": {