@amritk/helpers 0.8.0 → 0.10.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.
Files changed (86) hide show
  1. package/dist/build-dynamic-ref-map.d.ts +0 -1
  2. package/dist/derive-root-type-name.d.ts +0 -1
  3. package/dist/escape-regex-pattern.d.ts +0 -1
  4. package/dist/extract-dynamic-anchor-defs.d.ts +0 -1
  5. package/dist/extract-refs.d.ts +0 -1
  6. package/dist/generate-index-barrel.d.ts +0 -1
  7. package/dist/generate-type-definition.d.ts +0 -1
  8. package/dist/has-ref.d.ts +0 -1
  9. package/dist/is-object.d.ts +0 -1
  10. package/dist/mjst-extension.d.ts +0 -1
  11. package/dist/parse-documentation.d.ts +0 -1
  12. package/dist/ref-to-filename.d.ts +0 -1
  13. package/dist/ref-to-name.d.ts +0 -1
  14. package/dist/resolve-dynamic-refs.d.ts +0 -1
  15. package/dist/resolve-ref.d.ts +0 -1
  16. package/dist/safe-accessor.d.ts +0 -1
  17. package/dist/schema-guards.d.ts +8 -1
  18. package/dist/schema-guards.js +18 -0
  19. package/dist/unknown-key-check.d.ts +46 -0
  20. package/dist/unknown-key-check.js +40 -0
  21. package/dist/upgrade-draft07-schema.d.ts +0 -1
  22. package/dist/validate-array.d.ts +0 -1
  23. package/dist/validate-record.d.ts +0 -1
  24. package/dist/walk-ref-graph.d.ts +0 -1
  25. package/package.json +2 -4
  26. package/dist/build-dynamic-ref-map.d.ts.map +0 -1
  27. package/dist/derive-root-type-name.d.ts.map +0 -1
  28. package/dist/escape-regex-pattern.d.ts.map +0 -1
  29. package/dist/extract-dynamic-anchor-defs.d.ts.map +0 -1
  30. package/dist/extract-refs.d.ts.map +0 -1
  31. package/dist/generate-index-barrel.d.ts.map +0 -1
  32. package/dist/generate-type-definition.d.ts.map +0 -1
  33. package/dist/has-ref.d.ts.map +0 -1
  34. package/dist/is-object.d.ts.map +0 -1
  35. package/dist/mjst-extension.d.ts.map +0 -1
  36. package/dist/parse-documentation.d.ts.map +0 -1
  37. package/dist/ref-to-filename.d.ts.map +0 -1
  38. package/dist/ref-to-name.d.ts.map +0 -1
  39. package/dist/resolve-dynamic-refs.d.ts.map +0 -1
  40. package/dist/resolve-ref.d.ts.map +0 -1
  41. package/dist/safe-accessor.d.ts.map +0 -1
  42. package/dist/schema-guards.d.ts.map +0 -1
  43. package/dist/upgrade-draft07-schema.d.ts.map +0 -1
  44. package/dist/validate-array.d.ts.map +0 -1
  45. package/dist/validate-record.d.ts.map +0 -1
  46. package/dist/walk-ref-graph.d.ts.map +0 -1
  47. package/src/build-dynamic-ref-map.test.ts +0 -65
  48. package/src/build-dynamic-ref-map.ts +0 -42
  49. package/src/derive-root-type-name.test.ts +0 -42
  50. package/src/derive-root-type-name.ts +0 -42
  51. package/src/escape-regex-pattern.test.ts +0 -35
  52. package/src/escape-regex-pattern.ts +0 -20
  53. package/src/extract-dynamic-anchor-defs.test.ts +0 -29
  54. package/src/extract-dynamic-anchor-defs.ts +0 -33
  55. package/src/extract-refs.test.ts +0 -183
  56. package/src/extract-refs.ts +0 -73
  57. package/src/generate-index-barrel.test.ts +0 -41
  58. package/src/generate-index-barrel.ts +0 -62
  59. package/src/generate-type-definition.test.ts +0 -1386
  60. package/src/generate-type-definition.ts +0 -580
  61. package/src/has-ref.ts +0 -10
  62. package/src/is-object.test.ts +0 -65
  63. package/src/is-object.ts +0 -15
  64. package/src/mjst-extension.test.ts +0 -59
  65. package/src/mjst-extension.ts +0 -81
  66. package/src/parse-documentation.test.ts +0 -313
  67. package/src/parse-documentation.ts +0 -173
  68. package/src/ref-to-filename.test.ts +0 -64
  69. package/src/ref-to-filename.ts +0 -116
  70. package/src/ref-to-name.test.ts +0 -57
  71. package/src/ref-to-name.ts +0 -42
  72. package/src/resolve-dynamic-refs.test.ts +0 -106
  73. package/src/resolve-dynamic-refs.ts +0 -54
  74. package/src/resolve-ref.test.ts +0 -186
  75. package/src/resolve-ref.ts +0 -82
  76. package/src/safe-accessor.test.ts +0 -41
  77. package/src/safe-accessor.ts +0 -48
  78. package/src/schema-guards.test.ts +0 -427
  79. package/src/schema-guards.ts +0 -177
  80. package/src/upgrade-draft07-schema.ts +0 -179
  81. package/src/validate-array.test.ts +0 -52
  82. package/src/validate-array.ts +0 -15
  83. package/src/validate-record.test.ts +0 -56
  84. package/src/validate-record.ts +0 -19
  85. package/src/walk-ref-graph.test.ts +0 -118
  86. package/src/walk-ref-graph.ts +0 -184
@@ -15,4 +15,3 @@ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
15
15
  * // Returns: { "#meta": "#/$defs/schema" }
16
16
  */
17
17
  export declare const buildDynamicRefMap: (rootSchema: JSONSchema) => Record<string, string>;
18
- //# sourceMappingURL=build-dynamic-ref-map.d.ts.map
@@ -18,4 +18,3 @@
18
18
  * ```
19
19
  */
20
20
  export declare const deriveRootTypeName: (schema: unknown) => string;
21
- //# sourceMappingURL=derive-root-type-name.d.ts.map
@@ -14,4 +14,3 @@
14
14
  * escapeRegexPattern('\\d{4}/\\d{2}') // → '\\d{4}\\/\\d{2}' (i.e. \d{4}\/\d{2})
15
15
  */
16
16
  export declare const escapeRegexPattern: (pattern: string) => string;
17
- //# sourceMappingURL=escape-regex-pattern.d.ts.map
@@ -17,4 +17,3 @@ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
17
17
  * ```
18
18
  */
19
19
  export declare const extractDynamicAnchorDefs: (schema: JSONSchema) => string[];
20
- //# sourceMappingURL=extract-dynamic-anchor-defs.d.ts.map
@@ -21,4 +21,3 @@ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
21
21
  * ```
22
22
  */
23
23
  export declare const extractRefs: (schema: JSONSchema) => Set<string>;
24
- //# sourceMappingURL=extract-refs.d.ts.map
@@ -26,4 +26,3 @@ export type GenerateIndexBarrelOptions = {
26
26
  * @returns The `index.ts` file content.
27
27
  */
28
28
  export declare const generateIndexBarrel: (files: IndexBarrelFile[], options?: GenerateIndexBarrelOptions) => string;
29
- //# sourceMappingURL=generate-index-barrel.d.ts.map
@@ -15,4 +15,3 @@ export type TypeOptions = {
15
15
  * Uses $comment as inline JSDoc description when present.
16
16
  */
17
17
  export declare const generateTypeDefinition: (schema: JSONSchema, typeName: string, options?: TypeOptions) => string;
18
- //# sourceMappingURL=generate-type-definition.d.ts.map
package/dist/has-ref.d.ts CHANGED
@@ -2,4 +2,3 @@
2
2
  export declare const hasRef: (value: unknown) => value is {
3
3
  $ref: string;
4
4
  } & Record<string, unknown>;
5
- //# sourceMappingURL=has-ref.d.ts.map
@@ -12,4 +12,3 @@
12
12
  * isObject('string') // false
13
13
  */
14
14
  export declare const isObject: (value: unknown) => value is Record<string, unknown>;
15
- //# sourceMappingURL=is-object.d.ts.map
@@ -41,4 +41,3 @@ export declare const getMjstPrimitive: (schema: JSONSchema) => string | undefine
41
41
  * present and safe to embed in generated output. Returns undefined otherwise.
42
42
  */
43
43
  export declare const getMjstBrand: (schema: JSONSchema) => string | undefined;
44
- //# sourceMappingURL=mjst-extension.d.ts.map
@@ -15,4 +15,3 @@ export type ObjectDocumentation = {
15
15
  */
16
16
  export declare const parseDocumentation: (markdownDocumentation: string, commentUrl: string, fallbackCommentUrl?: string) => ObjectDocumentation | null;
17
17
  export {};
18
- //# sourceMappingURL=parse-documentation.d.ts.map
@@ -34,4 +34,3 @@ export declare const toKebabCase: (value: string) => string;
34
34
  * ```
35
35
  */
36
36
  export declare const refToFilename: (ref: string) => string;
37
- //# sourceMappingURL=ref-to-filename.d.ts.map
@@ -19,4 +19,3 @@
19
19
  * ```
20
20
  */
21
21
  export declare const refToName: (ref: string, suffix?: string) => string;
22
- //# sourceMappingURL=ref-to-name.d.ts.map
@@ -12,4 +12,3 @@ import type { JSONSchema } from 'json-schema-typed/draft-2020-12';
12
12
  * separate file per `$def`, so each schema walked here is relatively shallow.
13
13
  */
14
14
  export declare const resolveDynamicRefs: (schema: JSONSchema, dynamicRefMap: Record<string, string>) => JSONSchema;
15
- //# sourceMappingURL=resolve-dynamic-refs.d.ts.map
@@ -24,4 +24,3 @@
24
24
  * ```
25
25
  */
26
26
  export declare const resolveRef: (ref: string, rootSchema: Record<string, unknown>) => Record<string, unknown> | undefined;
27
- //# sourceMappingURL=resolve-ref.d.ts.map
@@ -22,4 +22,3 @@ export declare const safeAccessor: (variable: string, key: string) => string;
22
22
  * safeKey("x-linkedin") // "'x-linkedin'"
23
23
  */
24
24
  export declare const safeKey: (key: string) => string;
25
- //# sourceMappingURL=safe-accessor.d.ts.map
@@ -84,6 +84,14 @@ export declare const hasExclusiveMinimum: (schema: JSONSchema) => schema is Sche
84
84
  export declare const hasExclusiveMaximum: (schema: JSONSchema) => schema is SchemaObject & {
85
85
  exclusiveMaximum: number;
86
86
  };
87
+ /**
88
+ * Draft-04 expressed a strict lower bound as a boolean `exclusiveMinimum: true`
89
+ * paired with `minimum`, where draft-06+ uses a standalone numeric keyword. True
90
+ * only for that legacy boolean form, so callers can tighten the `minimum` compare.
91
+ */
92
+ export declare const hasStrictExclusiveMinimum: (schema: JSONSchema) => boolean;
93
+ /** Draft-04 boolean `exclusiveMaximum: true` paired with `maximum`. See {@link hasStrictExclusiveMinimum}. */
94
+ export declare const hasStrictExclusiveMaximum: (schema: JSONSchema) => boolean;
87
95
  /** Type guard to check if schema has multipleOf */
88
96
  export declare const hasMultipleOf: (schema: JSONSchema) => schema is SchemaObject & {
89
97
  multipleOf: number;
@@ -117,4 +125,3 @@ export declare const hasPropertyNames: (schema: JSONSchema) => schema is SchemaO
117
125
  propertyNames: JSONSchema;
118
126
  };
119
127
  export { hasRef } from './has-ref.js';
120
- //# sourceMappingURL=schema-guards.d.ts.map
@@ -93,6 +93,24 @@ export const hasExclusiveMinimum = (schema) => {
93
93
  export const hasExclusiveMaximum = (schema) => {
94
94
  return isSchemaObject(schema) && 'exclusiveMaximum' in schema && typeof schema.exclusiveMaximum === 'number';
95
95
  };
96
+ /**
97
+ * Draft-04 expressed a strict lower bound as a boolean `exclusiveMinimum: true`
98
+ * paired with `minimum`, where draft-06+ uses a standalone numeric keyword. True
99
+ * only for that legacy boolean form, so callers can tighten the `minimum` compare.
100
+ */
101
+ export const hasStrictExclusiveMinimum = (schema) => {
102
+ if (!isSchemaObject(schema))
103
+ return false;
104
+ const flag = schema.exclusiveMinimum;
105
+ return flag === true;
106
+ };
107
+ /** Draft-04 boolean `exclusiveMaximum: true` paired with `maximum`. See {@link hasStrictExclusiveMinimum}. */
108
+ export const hasStrictExclusiveMaximum = (schema) => {
109
+ if (!isSchemaObject(schema))
110
+ return false;
111
+ const flag = schema.exclusiveMaximum;
112
+ return flag === true;
113
+ };
96
114
  /** Type guard to check if schema has multipleOf */
97
115
  export const hasMultipleOf = (schema) => {
98
116
  return isSchemaObject(schema) && 'multipleOf' in schema && typeof schema.multipleOf === 'number';
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Default key-count cutoff below which an unknown-key sweep inlines `!==`
3
+ * comparisons instead of a hoisted `Set`. Chosen so typical schema objects stay
4
+ * on the faster inline path while pathologically wide objects fall back to the
5
+ * `Set`'s O(1) lookup.
6
+ */
7
+ export declare const INLINE_KEY_LIMIT = 16;
8
+ /** The pieces a caller needs to emit an `additionalProperties: false` sweep. */
9
+ export type UnknownKeyCheck = {
10
+ /**
11
+ * Declarations to emit once before the sweep — a single hoisted `Set` when the
12
+ * key count exceeds the inline limit, otherwise empty (the inline form is
13
+ * stateless). Each entry is a full statement with no trailing `;` or newline,
14
+ * so callers can punctuate to match their surrounding style.
15
+ */
16
+ readonly declarations: readonly string[];
17
+ /**
18
+ * Builds the boolean expression that is true when `keyVar` is NOT one of the
19
+ * known keys — an inline chain of `!==` comparisons, a `Set.has` miss, or the
20
+ * constant `true` when there are no known keys (every key is undeclared).
21
+ */
22
+ readonly isUnknown: (keyVar: string) => string;
23
+ /**
24
+ * The complement of {@link isUnknown}: true when `keyVar` IS a known key — an
25
+ * inline chain of `===` comparisons, a `Set.has` hit, or the constant `false`
26
+ * when there are no known keys.
27
+ */
28
+ readonly isKnown: (keyVar: string) => string;
29
+ };
30
+ /**
31
+ * Builds the "is this an undeclared key" test used by `additionalProperties:
32
+ * false` sweeps in the generated parsers and validators.
33
+ *
34
+ * For a small number of known keys V8 evaluates a chain of `!==` string
35
+ * comparisons faster than `Set.has` (which has to hash the string), and the
36
+ * inline form skips the per-module `Set` allocation — the same shape Ajv and
37
+ * TypeBox compile to. Above `inlineLimit` the chain grows long enough that the
38
+ * `Set`'s O(1) lookup wins, so a `Set` named `setName` is hoisted instead.
39
+ *
40
+ * @example
41
+ * const check = unknownKeyCheck(['id', 'name'], '_knownKeys0')
42
+ * check.declarations // []
43
+ * check.isUnknown('_k') // '_k !== "id" && _k !== "name"'
44
+ * check.isKnown('_k') // '_k === "id" || _k === "name"'
45
+ */
46
+ export declare const unknownKeyCheck: (knownKeys: readonly string[], setName: string, inlineLimit?: number) => UnknownKeyCheck;
@@ -0,0 +1,40 @@
1
+ /**
2
+ * Default key-count cutoff below which an unknown-key sweep inlines `!==`
3
+ * comparisons instead of a hoisted `Set`. Chosen so typical schema objects stay
4
+ * on the faster inline path while pathologically wide objects fall back to the
5
+ * `Set`'s O(1) lookup.
6
+ */
7
+ export const INLINE_KEY_LIMIT = 16;
8
+ /**
9
+ * Builds the "is this an undeclared key" test used by `additionalProperties:
10
+ * false` sweeps in the generated parsers and validators.
11
+ *
12
+ * For a small number of known keys V8 evaluates a chain of `!==` string
13
+ * comparisons faster than `Set.has` (which has to hash the string), and the
14
+ * inline form skips the per-module `Set` allocation — the same shape Ajv and
15
+ * TypeBox compile to. Above `inlineLimit` the chain grows long enough that the
16
+ * `Set`'s O(1) lookup wins, so a `Set` named `setName` is hoisted instead.
17
+ *
18
+ * @example
19
+ * const check = unknownKeyCheck(['id', 'name'], '_knownKeys0')
20
+ * check.declarations // []
21
+ * check.isUnknown('_k') // '_k !== "id" && _k !== "name"'
22
+ * check.isKnown('_k') // '_k === "id" || _k === "name"'
23
+ */
24
+ export const unknownKeyCheck = (knownKeys, setName, inlineLimit = INLINE_KEY_LIMIT) => {
25
+ if (knownKeys.length === 0) {
26
+ return { declarations: [], isUnknown: () => 'true', isKnown: () => 'false' };
27
+ }
28
+ if (knownKeys.length <= inlineLimit) {
29
+ return {
30
+ declarations: [],
31
+ isUnknown: (keyVar) => knownKeys.map((key) => `${keyVar} !== ${JSON.stringify(key)}`).join(' && '),
32
+ isKnown: (keyVar) => knownKeys.map((key) => `${keyVar} === ${JSON.stringify(key)}`).join(' || '),
33
+ };
34
+ }
35
+ return {
36
+ declarations: [`const ${setName} = new Set(${JSON.stringify(knownKeys)})`],
37
+ isUnknown: (keyVar) => `!${setName}.has(${keyVar})`,
38
+ isKnown: (keyVar) => `${setName}.has(${keyVar})`,
39
+ };
40
+ };
@@ -31,4 +31,3 @@ export declare const isDraft07Schema: (schema: Record<string, unknown>) => boole
31
31
  * nested defs hoisted to the root, and internal refs rewritten
32
32
  */
33
33
  export declare const upgradeDraft07Schema: (schema: Record<string, unknown>) => Record<string, unknown>;
34
- //# sourceMappingURL=upgrade-draft07-schema.d.ts.map
@@ -1,3 +1,2 @@
1
1
  /** Parses the items of an array with a parser function */
2
2
  export declare const validateArray: (input: unknown, parser: (input: unknown) => unknown) => any[];
3
- //# sourceMappingURL=validate-array.d.ts.map
@@ -3,4 +3,3 @@
3
3
  * Uses for...in instead of Object.entries() to avoid allocating an intermediate array.
4
4
  */
5
5
  export declare const validateRecord: (input: unknown, parser: (input: unknown) => unknown) => Record<string, unknown>;
6
- //# sourceMappingURL=validate-record.d.ts.map
@@ -51,4 +51,3 @@ export type WalkRefGraphOptions = {
51
51
  * @param visit - Called once per output file with a fully prepared {@link RefNode}.
52
52
  */
53
53
  export declare const walkRefGraph: (rootSchema: JSONSchema, rootTypeName: string, options: WalkRefGraphOptions, visit: (node: RefNode) => void) => void;
54
- //# sourceMappingURL=walk-ref-graph.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/helpers",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "description": "Shared utilities for the mjst code generation ecosystem.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -20,8 +20,7 @@
20
20
  "url": "https://github.com/amritk/mjst/issues"
21
21
  },
22
22
  "files": [
23
- "dist",
24
- "src"
23
+ "dist"
25
24
  ],
26
25
  "publishConfig": {
27
26
  "access": "public"
@@ -37,7 +36,6 @@
37
36
  "exports": {
38
37
  "./package.json": "./package.json",
39
38
  "./*": {
40
- "development": "./src/*.ts",
41
39
  "import": "./dist/*.js",
42
40
  "types": "./dist/*.d.ts",
43
41
  "default": "./dist/*.js"
@@ -1 +0,0 @@
1
- {"version":3,"file":"build-dynamic-ref-map.d.ts","sourceRoot":"","sources":["../src/build-dynamic-ref-map.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAIjE;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,kBAAkB,eAAgB,UAAU,KAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAsBhF,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"derive-root-type-name.d.ts","sourceRoot":"","sources":["../src/derive-root-type-name.ts"],"names":[],"mappings":"AAeA;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,kBAAkB,WAAY,OAAO,KAAG,MAOpD,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"escape-regex-pattern.d.ts","sourceRoot":"","sources":["../src/escape-regex-pattern.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,kBAAkB,YAAa,MAAM,KAAG,MAIwB,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"extract-dynamic-anchor-defs.d.ts","sourceRoot":"","sources":["../src/extract-dynamic-anchor-defs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAEjE;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,wBAAwB,WAAY,UAAU,KAAG,MAAM,EAanE,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"extract-refs.d.ts","sourceRoot":"","sources":["../src/extract-refs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAsBjE;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,WAAW,WAAY,UAAU,KAAG,GAAG,CAAC,MAAM,CA6B1D,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"generate-index-barrel.d.ts","sourceRoot":"","sources":["../src/generate-index-barrel.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,MAAM,MAAM,eAAe,GAAG;IAC5B,QAAQ,EAAE,MAAM,CAAA;IAChB,OAAO,EAAE,MAAM,CAAA;CAChB,CAAA;AAED,iEAAiE;AACjE,MAAM,MAAM,0BAA0B,GAAG;IACvC;;;;OAIG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,CAAA;CAC7B,CAAA;AAOD;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,mBAAmB,UAAW,eAAe,EAAE,YAAW,0BAA0B,KAAQ,MA2BxG,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"generate-type-definition.d.ts","sourceRoot":"","sources":["../src/generate-type-definition.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAYjE,iDAAiD;AACjD,MAAM,MAAM,WAAW,GAAG;IACxB,kGAAkG;IAClG,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;IAC3B;;;OAGG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAA;CAC7B,CAAA;AAmZD;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,WAAY,UAAU,YAAY,MAAM,YAAW,WAAW,KAAQ,MAsJxG,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"has-ref.d.ts","sourceRoot":"","sources":["../src/has-ref.ts"],"names":[],"mappings":"AAAA,iGAAiG;AACjG,eAAO,MAAM,MAAM,UAAW,OAAO,KAAG,KAAK,IAAI;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAQzF,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"is-object.d.ts","sourceRoot":"","sources":["../src/is-object.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,QAAQ,UAAW,OAAO,KAAG,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CACV,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"mjst-extension.d.ts","sourceRoot":"","sources":["../src/mjst-extension.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAIjE;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,WAAW,CAAA;AAE1C;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAA;IAC5B,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAA;IAC3B,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CACxB,CAAA;AAwBD;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,WAAY,UAAU,KAAG,MAAM,GAAG,SAG/D,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,gBAAgB,WAAY,UAAU,KAAG,MAAM,GAAG,SAG9D,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,YAAY,WAAY,UAAU,KAAG,MAAM,GAAG,SAG1D,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"parse-documentation.d.ts","sourceRoot":"","sources":["../src/parse-documentation.ts"],"names":[],"mappings":"AAAA,KAAK,qBAAqB,GAAG;IAC3B,WAAW,EAAE,MAAM,CAAA;IACnB,UAAU,EAAE,OAAO,CAAA;CACpB,CAAA;AAED,MAAM,MAAM,mBAAmB,GAAG;IAChC,KAAK,EAAE,MAAM,CAAA;IACb,WAAW,EAAE,MAAM,CAAA;IACnB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,qBAAqB,CAAC,CAAA;CAClD,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,0BACN,MAAM,cACjB,MAAM,uBACG,MAAM,KAC1B,mBAAmB,GAAG,IAuJxB,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"ref-to-filename.d.ts","sourceRoot":"","sources":["../src/ref-to-filename.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,WAAW,UAAW,MAAM,KAAG,MAM1B,CAAA;AA0DlB;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,aAAa,QAAS,MAAM,KAAG,MAiB3C,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"ref-to-name.d.ts","sourceRoot":"","sources":["../src/ref-to-name.ts"],"names":[],"mappings":"AAqBA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,SAAS,QAAS,MAAM,sBAAgB,MAAmD,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"resolve-dynamic-refs.d.ts","sourceRoot":"","sources":["../src/resolve-dynamic-refs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAEjE;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,kBAAkB,WAAY,UAAU,iBAAiB,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KAAG,UAuC9F,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"resolve-ref.d.ts","sourceRoot":"","sources":["../src/resolve-ref.ts"],"names":[],"mappings":"AA0BA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,eAAO,MAAM,UAAU,QAAS,MAAM,cAAc,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SA8BvG,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"safe-accessor.d.ts","sourceRoot":"","sources":["../src/safe-accessor.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,YAAY,aAAc,MAAM,OAAO,MAAM,KAAG,MAW5D,CAAA;AAED;;;;;;;GAOG;AACH,eAAO,MAAM,OAAO,QAAS,MAAM,KAAG,MAKrC,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"schema-guards.d.ts","sourceRoot":"","sources":["../src/schema-guards.ts"],"names":[],"mappings":"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"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"upgrade-draft07-schema.d.ts","sourceRoot":"","sources":["../src/upgrade-draft07-schema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAIH;;GAEG;AACH,eAAO,MAAM,eAAe,WAAY,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,OACe,CAAA;AAqFjF;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB,WAAY,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAiC5F,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"validate-array.d.ts","sourceRoot":"","sources":["../src/validate-array.ts"],"names":[],"mappings":"AAAA,0DAA0D;AAC1D,eAAO,MAAM,aAAa,UAAW,OAAO,UAAU,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,UAahF,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"validate-record.d.ts","sourceRoot":"","sources":["../src/validate-record.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,eAAO,MAAM,cAAc,UAAW,OAAO,UAAU,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,4BAYjF,CAAA"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"walk-ref-graph.d.ts","sourceRoot":"","sources":["../src/walk-ref-graph.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAWjE;;;;;GAKG;AACH,MAAM,MAAM,OAAO,GAAG;IACpB,2FAA2F;IAC3F,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;IACvB,2FAA2F;IAC3F,QAAQ,EAAE,MAAM,CAAA;IAChB,sGAAsG;IACtG,QAAQ,EAAE,MAAM,CAAA;IAChB,qFAAqF;IACrF,MAAM,EAAE,UAAU,CAAA;IAClB,+EAA+E;IAC/E,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IACnC,oEAAoE;IACpE,MAAM,EAAE,OAAO,CAAA;CAChB,CAAA;AAED,iEAAiE;AACjE,MAAM,MAAM,mBAAmB,GAAG;IAChC;;;;OAIG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAA;CAC7B,CAAA;AA2DD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,eAAO,MAAM,YAAY,eACX,UAAU,gBACR,MAAM,WACX,mBAAmB,SACrB,CAAC,IAAI,EAAE,OAAO,KAAK,IAAI,KAC7B,IAyDF,CAAA"}
@@ -1,65 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
-
3
- import { buildDynamicRefMap } from './build-dynamic-ref-map'
4
-
5
- describe('build-dynamic-ref-map', () => {
6
- it('maps $dynamicAnchor values to $ref paths', () => {
7
- const schema = {
8
- type: 'object' as const,
9
- $defs: {
10
- schema: {
11
- $dynamicAnchor: 'meta',
12
- type: 'object',
13
- },
14
- },
15
- }
16
-
17
- const result = buildDynamicRefMap(schema)
18
- expect(result).toEqual({ '#meta': '#/$defs/schema' })
19
- })
20
-
21
- it('returns empty map when no $dynamicAnchor exists', () => {
22
- const schema = {
23
- type: 'object' as const,
24
- $defs: {
25
- info: { type: 'object' },
26
- },
27
- }
28
-
29
- const result = buildDynamicRefMap(schema)
30
- expect(result).toEqual({})
31
- })
32
-
33
- it('returns empty map for non-object schema', () => {
34
- const result = buildDynamicRefMap(true)
35
- expect(result).toEqual({})
36
- })
37
-
38
- it('returns empty map when no $defs exist', () => {
39
- const schema = { type: 'object' as const }
40
- const result = buildDynamicRefMap(schema)
41
- expect(result).toEqual({})
42
- })
43
-
44
- it('handles multiple $dynamicAnchor definitions', () => {
45
- const schema = {
46
- type: 'object' as const,
47
- $defs: {
48
- schema: {
49
- $dynamicAnchor: 'meta',
50
- type: 'object',
51
- },
52
- other: {
53
- $dynamicAnchor: 'other-anchor',
54
- type: 'string',
55
- },
56
- },
57
- }
58
-
59
- const result = buildDynamicRefMap(schema)
60
- expect(result).toEqual({
61
- '#meta': '#/$defs/schema',
62
- '#other-anchor': '#/$defs/other',
63
- })
64
- })
65
- })
@@ -1,42 +0,0 @@
1
- import type { JSONSchema } from 'json-schema-typed/draft-2020-12'
2
-
3
- import { isSchemaObject } from './schema-guards'
4
-
5
- /**
6
- * Builds a map of $dynamicRef anchor values to their corresponding $ref paths.
7
- *
8
- * JSON Schema 2020-12 uses $dynamicAnchor and $dynamicRef for late-binding references.
9
- * In the OpenAPI spec, $dynamicAnchor: "meta" on the schema definition allows properties
10
- * like media-type.schema to reference it via $dynamicRef: "#meta".
11
- *
12
- * This function scans all $defs for entries with $dynamicAnchor and builds a lookup
13
- * so we can convert $dynamicRef values to concrete $ref paths.
14
- *
15
- * @example
16
- * // Given a schema with $defs.schema having $dynamicAnchor: "meta"
17
- * buildDynamicRefMap(rootSchema)
18
- * // Returns: { "#meta": "#/$defs/schema" }
19
- */
20
- export const buildDynamicRefMap = (rootSchema: JSONSchema): Record<string, string> => {
21
- const map: Record<string, string> = {}
22
-
23
- if (!isSchemaObject(rootSchema) || !('$defs' in rootSchema)) {
24
- return map
25
- }
26
-
27
- const defs = rootSchema.$defs as Record<string, unknown>
28
-
29
- for (const [key, value] of Object.entries(defs)) {
30
- if (
31
- typeof value === 'object' &&
32
- value !== null &&
33
- '$dynamicAnchor' in value &&
34
- typeof (value as Record<string, unknown>)['$dynamicAnchor'] === 'string'
35
- ) {
36
- const anchor = (value as Record<string, unknown>)['$dynamicAnchor'] as string
37
- map[`#${anchor}`] = `#/$defs/${key}`
38
- }
39
- }
40
-
41
- return map
42
- }
@@ -1,42 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
-
3
- import { deriveRootTypeName } from './derive-root-type-name'
4
-
5
- describe('derive-root-type-name', () => {
6
- it('derives a PascalCase name from a single-word title', () => {
7
- expect(deriveRootTypeName({ title: 'Document' })).toBe('Document')
8
- })
9
-
10
- it('joins multi-word titles into PascalCase', () => {
11
- expect(deriveRootTypeName({ title: 'OpenAPI Document' })).toBe('OpenAPIDocument')
12
- })
13
-
14
- it('preserves acronyms intact', () => {
15
- expect(deriveRootTypeName({ title: 'JSON Schema' })).toBe('JSONSchema')
16
- })
17
-
18
- it('splits on non-alphanumeric separators', () => {
19
- expect(deriveRootTypeName({ title: 'my-config_file.spec' })).toBe('MyConfigFileSpec')
20
- })
21
-
22
- it('drops leading digits so the name is a valid identifier', () => {
23
- expect(deriveRootTypeName({ title: '3 amigos' })).toBe('Amigos')
24
- })
25
-
26
- it('falls back to Document when the title is missing', () => {
27
- expect(deriveRootTypeName({ type: 'object' })).toBe('Document')
28
- })
29
-
30
- it('falls back to Document when the title is not a string', () => {
31
- expect(deriveRootTypeName({ title: 42 })).toBe('Document')
32
- })
33
-
34
- it('falls back to Document for a title with no usable characters', () => {
35
- expect(deriveRootTypeName({ title: ' --- ' })).toBe('Document')
36
- })
37
-
38
- it('falls back to Document for boolean schemas', () => {
39
- expect(deriveRootTypeName(true)).toBe('Document')
40
- expect(deriveRootTypeName(false)).toBe('Document')
41
- })
42
- })
@@ -1,42 +0,0 @@
1
- import { isObject } from './is-object'
2
-
3
- /**
4
- * Converts an arbitrary title string into a PascalCase TypeScript identifier.
5
- * Splits on any run of non-alphanumeric characters, capitalizes the first
6
- * letter of each word while preserving the rest (so acronyms like "API" or
7
- * "JSON" survive intact), and drops leading digits since an identifier may not
8
- * start with a number.
9
- */
10
- const titleToPascalCase = (title: string): string => {
11
- const words = title.split(/[^a-zA-Z0-9]+/).filter(Boolean)
12
- const pascal = words.map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join('')
13
- return pascal.replace(/^\d+/, '')
14
- }
15
-
16
- /**
17
- * Derives the root type name for a generated schema from its `title` keyword.
18
- *
19
- * We name the root after the schema itself instead of a generic "Document" so
20
- * the generated types and parsers read naturally (e.g. an OpenAPI schema yields
21
- * `OpenApi` / `parseOpenApi`). When the schema has no usable `title`, we fall
22
- * back to "Document" to keep output deterministic.
23
- *
24
- * @param schema - The root JSON Schema. A boolean schema or one without a
25
- * string `title` falls back to the default.
26
- * @returns A PascalCase type name derived from the title, or "Document".
27
- *
28
- * @example
29
- * ```ts
30
- * deriveRootTypeName({ title: 'OpenAPI Document' }) // 'OpenAPIDocument'
31
- * deriveRootTypeName({ title: 'my-config' }) // 'MyConfig'
32
- * deriveRootTypeName({ type: 'object' }) // 'Document'
33
- * ```
34
- */
35
- export const deriveRootTypeName = (schema: unknown): string => {
36
- if (!isObject(schema) || typeof schema['title'] !== 'string') {
37
- return 'Document'
38
- }
39
-
40
- const name = titleToPascalCase(schema['title'])
41
- return name || 'Document'
42
- }
@@ -1,35 +0,0 @@
1
- import { describe, expect, it } from 'vitest'
2
-
3
- import { escapeRegexPattern } from './escape-regex-pattern'
4
-
5
- describe('escape-regex-pattern', () => {
6
- it('escapes bare forward slashes so the regex literal does not close early', () => {
7
- expect(escapeRegexPattern('a/b')).toBe('a\\/b')
8
- // Input \d{4}/\d{2}/\d{2} → \d{4}\/\d{2}\/\d{2}
9
- expect(escapeRegexPattern('\\d{4}/\\d{2}/\\d{2}')).toBe('\\d{4}\\/\\d{2}\\/\\d{2}')
10
- })
11
-
12
- it('leaves backslash escape sequences exactly as-is (does not double them)', () => {
13
- // \d must stay \d — doubling it would match a literal backslash, not a digit.
14
- expect(escapeRegexPattern('\\d+')).toBe('\\d+')
15
- expect(escapeRegexPattern('\\w\\s')).toBe('\\w\\s')
16
- })
17
-
18
- it('does not double-escape an already-escaped slash', () => {
19
- // \/ (escaped slash) stays \/, not \\\/.
20
- expect(escapeRegexPattern('\\/')).toBe('\\/')
21
- })
22
-
23
- it('leaves regex metacharacters that do not affect the literal untouched', () => {
24
- expect(escapeRegexPattern('^[a-z]+$')).toBe('^[a-z]+$')
25
- })
26
-
27
- it('round-trips: the escaped body parses to a regex equivalent to the source pattern', () => {
28
- for (const pattern of ['\\d{4}/\\d{2}', 'a/b/c', '^https?:\\/\\/', '\\w+@\\w+']) {
29
- // Build the literal the generator emits and read its source back out.
30
- const re = new RegExp(escapeRegexPattern(pattern))
31
- // The RegExp's own source, with \/ normalized back to /, equals the input.
32
- expect(re.source.replace(/\\\//g, '/')).toBe(pattern.replace(/\\\//g, '/'))
33
- }
34
- })
35
- })
@@ -1,20 +0,0 @@
1
- /**
2
- * Escapes a JSON Schema `pattern` so it can be embedded between the slashes of
3
- * a generated regex literal (`/…/`).
4
- *
5
- * A `pattern` is an ECMA-262 regex *body*, and the generated text goes into a
6
- * regex literal — not a string literal — so backslashes are regex syntax and
7
- * must be left exactly as-is (doubling `\d` to `\\d` would change it from "a
8
- * digit" to "a literal backslash followed by d"). The only character that would
9
- * corrupt the surrounding literal is an *unescaped* `/`, which would close it
10
- * early. So we escape bare slashes to `\/` while leaving every existing escape
11
- * sequence (including an already-escaped `\/`) untouched.
12
- *
13
- * @example
14
- * escapeRegexPattern('\\d{4}/\\d{2}') // → '\\d{4}\\/\\d{2}' (i.e. \d{4}\/\d{2})
15
- */
16
- export const escapeRegexPattern = (pattern: string): string =>
17
- // Match either an escape sequence (`\` + any char, kept verbatim) or a bare
18
- // `/` (escaped). Consuming escape pairs first means the slash in `\/` is never
19
- // seen as bare, so it is not double-escaped.
20
- pattern.replace(/\\[\s\S]|\//g, (match) => (match === '/' ? '\\/' : match))