@amritk/helpers 0.6.2 → 0.7.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 (67) hide show
  1. package/dist/build-dynamic-ref-map.d.ts +1 -0
  2. package/dist/build-dynamic-ref-map.d.ts.map +1 -0
  3. package/dist/build-dynamic-ref-map.js +31 -107
  4. package/dist/derive-root-type-name.d.ts +1 -0
  5. package/dist/derive-root-type-name.d.ts.map +1 -0
  6. package/dist/derive-root-type-name.js +37 -17
  7. package/dist/extract-dynamic-anchor-defs.d.ts +20 -0
  8. package/dist/extract-dynamic-anchor-defs.d.ts.map +1 -0
  9. package/dist/extract-dynamic-anchor-defs.js +30 -0
  10. package/dist/extract-refs.d.ts +1 -0
  11. package/dist/extract-refs.d.ts.map +1 -0
  12. package/dist/extract-refs.js +64 -33
  13. package/dist/generate-index-barrel.d.ts +29 -0
  14. package/dist/generate-index-barrel.d.ts.map +1 -0
  15. package/dist/generate-index-barrel.js +43 -0
  16. package/dist/generate-type-definition.d.ts +1 -0
  17. package/dist/generate-type-definition.d.ts.map +1 -0
  18. package/dist/generate-type-definition.js +449 -567
  19. package/dist/has-ref.d.ts +1 -0
  20. package/dist/has-ref.d.ts.map +1 -0
  21. package/dist/has-ref.js +7 -6
  22. package/dist/is-object.d.ts +1 -0
  23. package/dist/is-object.d.ts.map +1 -0
  24. package/dist/is-object.js +14 -5
  25. package/dist/mjst-extension.d.ts +1 -0
  26. package/dist/mjst-extension.d.ts.map +1 -0
  27. package/dist/mjst-extension.js +50 -122
  28. package/dist/parse-documentation.d.ts +1 -0
  29. package/dist/parse-documentation.d.ts.map +1 -0
  30. package/dist/parse-documentation.js +124 -87
  31. package/dist/ref-to-filename.d.ts +1 -0
  32. package/dist/ref-to-filename.d.ts.map +1 -0
  33. package/dist/ref-to-filename.js +106 -42
  34. package/dist/ref-to-name.d.ts +1 -0
  35. package/dist/ref-to-name.d.ts.map +1 -0
  36. package/dist/ref-to-name.js +39 -53
  37. package/dist/resolve-dynamic-refs.d.ts +1 -0
  38. package/dist/resolve-dynamic-refs.d.ts.map +1 -0
  39. package/dist/resolve-dynamic-refs.js +36 -28
  40. package/dist/resolve-ref.d.ts +1 -0
  41. package/dist/resolve-ref.d.ts.map +1 -0
  42. package/dist/resolve-ref.js +74 -39
  43. package/dist/safe-accessor.d.ts +1 -0
  44. package/dist/safe-accessor.d.ts.map +1 -0
  45. package/dist/safe-accessor.js +42 -19
  46. package/dist/schema-guards.d.ts +2 -1
  47. package/dist/schema-guards.d.ts.map +1 -0
  48. package/dist/schema-guards.js +119 -120
  49. package/dist/upgrade-draft07-schema.d.ts +1 -0
  50. package/dist/upgrade-draft07-schema.d.ts.map +1 -0
  51. package/dist/upgrade-draft07-schema.js +152 -130
  52. package/dist/validate-array.d.ts +1 -0
  53. package/dist/validate-array.d.ts.map +1 -0
  54. package/dist/validate-array.js +12 -14
  55. package/dist/validate-record.d.ts +1 -0
  56. package/dist/validate-record.d.ts.map +1 -0
  57. package/dist/validate-record.js +15 -17
  58. package/dist/walk-ref-graph.d.ts +54 -0
  59. package/dist/walk-ref-graph.d.ts.map +1 -0
  60. package/dist/walk-ref-graph.js +123 -0
  61. package/package.json +9 -88
  62. package/src/extract-dynamic-anchor-defs.test.ts +29 -0
  63. package/src/extract-dynamic-anchor-defs.ts +33 -0
  64. package/src/generate-index-barrel.test.ts +41 -0
  65. package/src/generate-index-barrel.ts +62 -0
  66. package/src/walk-ref-graph.test.ts +103 -0
  67. package/src/walk-ref-graph.ts +184 -0
@@ -1,121 +1,120 @@
1
- // src/has-ref.ts
2
- var hasRef = (value) => {
3
- return typeof value === "object" && value !== null && !Array.isArray(value) && "$ref" in value && typeof value.$ref === "string";
4
- };
5
-
6
- // src/schema-guards.ts
7
- var isSchemaObject = (schema) => {
8
- return typeof schema === "object" && schema !== null && typeof schema !== "boolean";
9
- };
10
- var hasType = (schema) => {
11
- return isSchemaObject(schema) && "type" in schema && typeof schema.type === "string";
12
- };
13
- var isObjectSchema = (schema) => {
14
- return isSchemaObject(schema) && (("type" in schema) && schema.type === "object" || ("properties" in schema));
15
- };
16
- var hasProperties = (schema) => {
17
- return isSchemaObject(schema) && "properties" in schema && typeof schema.properties === "object" && schema.properties !== null;
18
- };
19
- var hasEnum = (schema) => {
20
- return isSchemaObject(schema) && "enum" in schema && Array.isArray(schema.enum);
21
- };
22
- var hasConst = (schema) => {
23
- return isSchemaObject(schema) && "const" in schema;
24
- };
25
- var hasPattern = (schema) => {
26
- return isSchemaObject(schema) && "pattern" in schema && typeof schema.pattern === "string";
27
- };
28
- var hasFormat = (schema) => {
29
- return isSchemaObject(schema) && "format" in schema && typeof schema.format === "string";
30
- };
31
- var hasDefault = (schema) => {
32
- return isSchemaObject(schema) && "default" in schema;
33
- };
34
- var hasExamples = (schema) => {
35
- return isSchemaObject(schema) && "examples" in schema && Array.isArray(schema.examples);
36
- };
37
- var hasOneOf = (schema) => {
38
- return isSchemaObject(schema) && "oneOf" in schema && Array.isArray(schema.oneOf);
39
- };
40
- var hasAnyOf = (schema) => {
41
- return isSchemaObject(schema) && "anyOf" in schema && Array.isArray(schema.anyOf);
42
- };
43
- var hasAllOf = (schema) => {
44
- return isSchemaObject(schema) && "allOf" in schema && Array.isArray(schema.allOf);
45
- };
46
- var hasRequired = (schema) => {
47
- return isSchemaObject(schema) && "required" in schema && Array.isArray(schema.required);
48
- };
49
- var hasItems = (schema) => {
50
- return isSchemaObject(schema) && "items" in schema && typeof schema.items === "object" && schema.items !== null && typeof schema.items !== "boolean";
51
- };
52
- var hasAdditionalProperties = (schema) => {
53
- return isSchemaObject(schema) && "additionalProperties" in schema;
54
- };
55
- var hasMinLength = (schema) => {
56
- return isSchemaObject(schema) && "minLength" in schema && typeof schema.minLength === "number";
57
- };
58
- var hasMaxLength = (schema) => {
59
- return isSchemaObject(schema) && "maxLength" in schema && typeof schema.maxLength === "number";
60
- };
61
- var hasMinimum = (schema) => {
62
- return isSchemaObject(schema) && "minimum" in schema && typeof schema.minimum === "number";
63
- };
64
- var hasMaximum = (schema) => {
65
- return isSchemaObject(schema) && "maximum" in schema && typeof schema.maximum === "number";
66
- };
67
- var hasExclusiveMinimum = (schema) => {
68
- return isSchemaObject(schema) && "exclusiveMinimum" in schema && typeof schema.exclusiveMinimum === "number";
69
- };
70
- var hasExclusiveMaximum = (schema) => {
71
- return isSchemaObject(schema) && "exclusiveMaximum" in schema && typeof schema.exclusiveMaximum === "number";
72
- };
73
- var hasMultipleOf = (schema) => {
74
- return isSchemaObject(schema) && "multipleOf" in schema && typeof schema.multipleOf === "number";
75
- };
76
- var hasMinItems = (schema) => {
77
- return isSchemaObject(schema) && "minItems" in schema && typeof schema.minItems === "number";
78
- };
79
- var hasMaxItems = (schema) => {
80
- return isSchemaObject(schema) && "maxItems" in schema && typeof schema.maxItems === "number";
81
- };
82
- var hasUniqueItems = (schema) => {
83
- return isSchemaObject(schema) && "uniqueItems" in schema && typeof schema.uniqueItems === "boolean";
84
- };
85
- var hasMinProperties = (schema) => {
86
- return isSchemaObject(schema) && "minProperties" in schema && typeof schema.minProperties === "number";
87
- };
88
- var hasMaxProperties = (schema) => {
89
- return isSchemaObject(schema) && "maxProperties" in schema && typeof schema.maxProperties === "number";
90
- };
91
- export {
92
- isSchemaObject,
93
- isObjectSchema,
94
- hasUniqueItems,
95
- hasType,
96
- hasRequired,
97
- hasRef,
98
- hasProperties,
99
- hasPattern,
100
- hasOneOf,
101
- hasMultipleOf,
102
- hasMinimum,
103
- hasMinProperties,
104
- hasMinLength,
105
- hasMinItems,
106
- hasMaximum,
107
- hasMaxProperties,
108
- hasMaxLength,
109
- hasMaxItems,
110
- hasItems,
111
- hasFormat,
112
- hasExclusiveMinimum,
113
- hasExclusiveMaximum,
114
- hasExamples,
115
- hasEnum,
116
- hasDefault,
117
- hasConst,
118
- hasAnyOf,
119
- hasAllOf,
120
- hasAdditionalProperties
1
+ /** Type guard to check if schema is not false */
2
+ export const isSchemaObject = (schema) => {
3
+ return typeof schema === 'object' && schema !== null && typeof schema !== 'boolean';
4
+ };
5
+ /** Type guard to check if schema has a type property */
6
+ export const hasType = (schema) => {
7
+ return isSchemaObject(schema) && 'type' in schema && typeof schema.type === 'string';
8
+ };
9
+ /** Type guard to check if schema is an object schema */
10
+ export const isObjectSchema = (schema) => {
11
+ return isSchemaObject(schema) && (('type' in schema && schema.type === 'object') || 'properties' in schema);
12
+ };
13
+ /** Type guard to check if schema has properties */
14
+ export const hasProperties = (schema) => {
15
+ return (isSchemaObject(schema) &&
16
+ 'properties' in schema &&
17
+ typeof schema.properties === 'object' &&
18
+ schema.properties !== null);
19
+ };
20
+ /** Type guard to check if schema has enum */
21
+ export const hasEnum = (schema) => {
22
+ return isSchemaObject(schema) && 'enum' in schema && Array.isArray(schema.enum);
23
+ };
24
+ /** Type guard to check if schema has const */
25
+ export const hasConst = (schema) => {
26
+ return isSchemaObject(schema) && 'const' in schema;
27
+ };
28
+ /** Type guard to check if schema has pattern */
29
+ export const hasPattern = (schema) => {
30
+ return isSchemaObject(schema) && 'pattern' in schema && typeof schema.pattern === 'string';
31
+ };
32
+ /** Type guard to check if schema has format */
33
+ export const hasFormat = (schema) => {
34
+ return isSchemaObject(schema) && 'format' in schema && typeof schema.format === 'string';
35
+ };
36
+ /** Type guard to check if schema has default */
37
+ export const hasDefault = (schema) => {
38
+ return isSchemaObject(schema) && 'default' in schema;
39
+ };
40
+ /** Type guard to check if schema has examples */
41
+ export const hasExamples = (schema) => {
42
+ return isSchemaObject(schema) && 'examples' in schema && Array.isArray(schema.examples);
43
+ };
44
+ /** Type guard to check if schema has oneOf */
45
+ export const hasOneOf = (schema) => {
46
+ return isSchemaObject(schema) && 'oneOf' in schema && Array.isArray(schema.oneOf);
47
+ };
48
+ /** Type guard to check if schema has anyOf */
49
+ export const hasAnyOf = (schema) => {
50
+ return isSchemaObject(schema) && 'anyOf' in schema && Array.isArray(schema.anyOf);
51
+ };
52
+ /** Type guard to check if schema has allOf */
53
+ export const hasAllOf = (schema) => {
54
+ return isSchemaObject(schema) && 'allOf' in schema && Array.isArray(schema.allOf);
55
+ };
56
+ /** Type guard to check if schema has required */
57
+ export const hasRequired = (schema) => {
58
+ return isSchemaObject(schema) && 'required' in schema && Array.isArray(schema.required);
59
+ };
60
+ /** Type guard to check if schema has items (and it's not just boolean) */
61
+ export const hasItems = (schema) => {
62
+ return (isSchemaObject(schema) &&
63
+ 'items' in schema &&
64
+ typeof schema.items === 'object' &&
65
+ schema.items !== null &&
66
+ typeof schema.items !== 'boolean');
67
+ };
68
+ /** Type guard to check if schema has additionalProperties */
69
+ export const hasAdditionalProperties = (schema) => {
70
+ return isSchemaObject(schema) && 'additionalProperties' in schema;
71
+ };
72
+ /** Type guard to check if schema has minLength */
73
+ export const hasMinLength = (schema) => {
74
+ return isSchemaObject(schema) && 'minLength' in schema && typeof schema.minLength === 'number';
75
+ };
76
+ /** Type guard to check if schema has maxLength */
77
+ export const hasMaxLength = (schema) => {
78
+ return isSchemaObject(schema) && 'maxLength' in schema && typeof schema.maxLength === 'number';
79
+ };
80
+ /** Type guard to check if schema has minimum */
81
+ export const hasMinimum = (schema) => {
82
+ return isSchemaObject(schema) && 'minimum' in schema && typeof schema.minimum === 'number';
83
+ };
84
+ /** Type guard to check if schema has maximum */
85
+ export const hasMaximum = (schema) => {
86
+ return isSchemaObject(schema) && 'maximum' in schema && typeof schema.maximum === 'number';
87
+ };
88
+ /** Type guard to check if schema has exclusiveMinimum */
89
+ export const hasExclusiveMinimum = (schema) => {
90
+ return isSchemaObject(schema) && 'exclusiveMinimum' in schema && typeof schema.exclusiveMinimum === 'number';
91
+ };
92
+ /** Type guard to check if schema has exclusiveMaximum */
93
+ export const hasExclusiveMaximum = (schema) => {
94
+ return isSchemaObject(schema) && 'exclusiveMaximum' in schema && typeof schema.exclusiveMaximum === 'number';
95
+ };
96
+ /** Type guard to check if schema has multipleOf */
97
+ export const hasMultipleOf = (schema) => {
98
+ return isSchemaObject(schema) && 'multipleOf' in schema && typeof schema.multipleOf === 'number';
99
+ };
100
+ /** Type guard to check if schema has minItems */
101
+ export const hasMinItems = (schema) => {
102
+ return isSchemaObject(schema) && 'minItems' in schema && typeof schema.minItems === 'number';
103
+ };
104
+ /** Type guard to check if schema has maxItems */
105
+ export const hasMaxItems = (schema) => {
106
+ return isSchemaObject(schema) && 'maxItems' in schema && typeof schema.maxItems === 'number';
107
+ };
108
+ /** Type guard to check if schema has uniqueItems */
109
+ export const hasUniqueItems = (schema) => {
110
+ return isSchemaObject(schema) && 'uniqueItems' in schema && typeof schema.uniqueItems === 'boolean';
111
+ };
112
+ /** Type guard to check if schema has minProperties */
113
+ export const hasMinProperties = (schema) => {
114
+ return isSchemaObject(schema) && 'minProperties' in schema && typeof schema.minProperties === 'number';
115
+ };
116
+ /** Type guard to check if schema has maxProperties */
117
+ export const hasMaxProperties = (schema) => {
118
+ return isSchemaObject(schema) && 'maxProperties' in schema && typeof schema.maxProperties === 'number';
121
119
  };
120
+ export { hasRef } from './has-ref.js';
@@ -31,3 +31,4 @@ 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
@@ -0,0 +1 @@
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,138 +1,160 @@
1
- // src/ref-to-filename.ts
2
- var toKebabCase = (value) => value.replace(/OAuth/g, "Oauth").replace(/([A-Z]+)([A-Z][a-z])/g, "$1-$2").replace(/([a-z\d])([A-Z])/g, "$1-$2").toLowerCase();
3
- var uriRefToFilename = (uri) => {
4
- const hashIndex = uri.indexOf("#");
5
- const baseUri = hashIndex === -1 ? uri : uri.slice(0, hashIndex);
6
- const fragment = hashIndex === -1 ? "" : uri.slice(hashIndex + 1);
7
- const withoutProtocol = baseUri.replace(/^https?:\/\/[^/]+\//, "");
8
- const withoutExt = withoutProtocol.replace(/\.json$/, "");
9
- const rawSegments = withoutExt.split("/");
10
- const SKIP_KEYS = new Set(["definitions", "$defs"]);
11
- const segments = [];
12
- for (let i = 0;i < rawSegments.length; i++) {
13
- const s = rawSegments[i];
14
- if (SKIP_KEYS.has(s))
15
- continue;
16
- const prevRaw = rawSegments[i - 1];
17
- if (/^\d+\.\d+/.test(s) && prevRaw !== undefined && SKIP_KEYS.has(prevRaw))
18
- continue;
19
- segments.push(s);
20
- }
21
- const baseName = segments.map((s) => toKebabCase(s).replace(/\./g, "-")).join("-");
22
- if (!fragment)
23
- return baseName;
24
- const fragSegments = fragment.split("/").filter((s) => s && !SKIP_KEYS.has(s) && s !== "properties");
25
- const fragLast = fragSegments[fragSegments.length - 1];
26
- if (!fragLast)
27
- return baseName;
28
- return `${baseName}-${toKebabCase(fragLast)}`;
29
- };
30
- var refToFilename = (ref) => {
31
- if (ref.startsWith("http://") || ref.startsWith("https://")) {
32
- return uriRefToFilename(ref);
33
- }
34
- const segments = ref.split("/");
35
- let filename = segments[segments.length - 1];
36
- if (/[A-Z]/.test(filename)) {
37
- filename = toKebabCase(filename);
38
- }
39
- return filename;
40
- };
41
-
42
- // src/upgrade-draft07-schema.ts
43
- var isDraft07Schema = (schema) => typeof schema["$schema"] === "string" && schema["$schema"].includes("draft-07");
44
- var rewriteRefs = (obj, refMap, selfRef) => {
45
- if (typeof obj !== "object" || obj === null)
46
- return obj;
47
- if (Array.isArray(obj))
48
- return obj.map((item) => rewriteRefs(item, refMap, selfRef));
49
- const record = obj;
50
- const result = {};
51
- for (const [key, value] of Object.entries(record)) {
52
- if (key === "$ref" && typeof value === "string") {
53
- if (refMap.has(value)) {
54
- result[key] = refMap.get(value);
55
- } else if (value === "#" && selfRef) {
56
- result[key] = selfRef;
57
- } else {
58
- result[key] = value;
59
- }
60
- } else {
61
- result[key] = rewriteRefs(value, refMap, selfRef);
1
+ /**
2
+ * Upgrades a JSON Schema draft-07 document to be compatible with the
3
+ * draft 2020-12 conventions used by the build pipeline.
4
+ *
5
+ * Draft-07 schemas differ from 2020-12 in two ways that affect our pipeline:
6
+ * - They use `definitions` instead of `$defs`
7
+ * - Their `definitions` keys (and `$ref` values) may be full URIs
8
+ * (e.g. `http://asyncapi.com/definitions/3.1.0/channel.json`) rather than
9
+ * short names (e.g. `channel`)
10
+ *
11
+ * This function:
12
+ * 1. Renames `definitions` → `$defs` at the root level only
13
+ * 2. Hoists any nested `$defs` (originally `definitions` inside sub-schemas)
14
+ * up to the root `$defs` with a prefixed name, rewriting internal refs
15
+ * so they resolve correctly from the root
16
+ * 3. Rewrites bare `$ref: "#"` self-references within each definition to
17
+ * point back to that definition's root-level `$defs` entry
18
+ *
19
+ * Only applied when the schema declares `$schema: http://json-schema.org/draft-07/schema`.
20
+ */
21
+ import { refToFilename, toKebabCase } from './ref-to-filename.js';
22
+ /**
23
+ * Returns true if the schema is a draft-07 document that needs upgrading.
24
+ */
25
+ export const isDraft07Schema = (schema) => typeof schema['$schema'] === 'string' && schema['$schema'].includes('draft-07');
26
+ /**
27
+ * Rewrites `$ref` values in a schema tree using an explicit string→string map.
28
+ * Also rewrites bare `$ref: "#"` to the given `selfRef` path when provided.
29
+ */
30
+ const rewriteRefs = (obj, refMap, selfRef) => {
31
+ if (typeof obj !== 'object' || obj === null)
32
+ return obj;
33
+ if (Array.isArray(obj))
34
+ return obj.map((item) => rewriteRefs(item, refMap, selfRef));
35
+ const record = obj;
36
+ const result = {};
37
+ for (const [key, value] of Object.entries(record)) {
38
+ if (key === '$ref' && typeof value === 'string') {
39
+ if (refMap.has(value)) {
40
+ result[key] = refMap.get(value);
41
+ }
42
+ else if (value === '#' && selfRef) {
43
+ result[key] = selfRef;
44
+ }
45
+ else {
46
+ result[key] = value;
47
+ }
48
+ }
49
+ else {
50
+ result[key] = rewriteRefs(value, refMap, selfRef);
51
+ }
62
52
  }
63
- }
64
- return result;
53
+ return result;
65
54
  };
66
- var hoistNestedDefs = (defs) => {
67
- const hoisted = {};
68
- for (const [parentName, parentSchema] of Object.entries(defs)) {
69
- if (typeof parentSchema !== "object" || parentSchema === null) {
70
- hoisted[parentName] = parentSchema;
71
- continue;
72
- }
73
- const parentObj = parentSchema;
74
- const nestedDefs = parentObj["$defs"];
75
- if (!nestedDefs || typeof nestedDefs !== "object") {
76
- hoisted[parentName] = parentSchema;
77
- continue;
78
- }
79
- const parentPrefix = parentName.startsWith("http://") || parentName.startsWith("https://") ? refToFilename(parentName) : parentName;
80
- const localToHoisted = new Map;
81
- for (const localName of Object.keys(nestedDefs)) {
82
- const hoistedName = `${parentPrefix}-${toKebabCase(localName)}`;
83
- localToHoisted.set(`#/$defs/${localName}`, `#/$defs/${hoistedName}`);
55
+ /**
56
+ * Hoists nested `$defs` from each root-level definition up to the root `$defs`.
57
+ *
58
+ * When a definition contains its own `$defs` (originally `definitions` in draft-07,
59
+ * e.g. the json-schema meta-schema or avro schema), those nested defs are moved to
60
+ * the root with a `parentName-childName` prefix. All internal `#/$defs/X` refs
61
+ * within the parent and its nested defs are rewritten to `#/$defs/parentName-X`.
62
+ * Bare `$ref: "#"` within nested defs is rewritten to `#/$defs/parentName`.
63
+ */
64
+ const hoistNestedDefs = (defs) => {
65
+ const hoisted = {};
66
+ for (const [parentName, parentSchema] of Object.entries(defs)) {
67
+ if (typeof parentSchema !== 'object' || parentSchema === null) {
68
+ hoisted[parentName] = parentSchema;
69
+ continue;
70
+ }
71
+ const parentObj = parentSchema;
72
+ const nestedDefs = parentObj['$defs'];
73
+ if (!nestedDefs || typeof nestedDefs !== 'object') {
74
+ hoisted[parentName] = parentSchema;
75
+ continue;
76
+ }
77
+ // Derive a short kebab-case prefix from the parent name (which may be a URI)
78
+ const parentPrefix = parentName.startsWith('http://') || parentName.startsWith('https://') ? refToFilename(parentName) : parentName;
79
+ // Build a map from local ref → hoisted ref for every nested def
80
+ const localToHoisted = new Map();
81
+ for (const localName of Object.keys(nestedDefs)) {
82
+ const hoistedName = `${parentPrefix}-${toKebabCase(localName)}`;
83
+ localToHoisted.set(`#/$defs/${localName}`, `#/$defs/${hoistedName}`);
84
+ }
85
+ const selfRef = `#/$defs/${parentPrefix}`;
86
+ // Rewrite refs in the parent. Keep the nested $defs in place so that
87
+ // URI-with-fragment refs (e.g. "http://foo.json#/$defs/queue") can still
88
+ // navigate into the parent's nested defs after resolution.
89
+ const rewrittenParent = rewriteRefs(parentObj, localToHoisted, selfRef);
90
+ hoisted[parentName] = rewrittenParent;
91
+ // Hoist each nested def, rewriting its internal refs too
92
+ for (const [localName, localSchema] of Object.entries(nestedDefs)) {
93
+ const hoistedName = `${parentPrefix}-${toKebabCase(localName)}`;
94
+ hoisted[hoistedName] = rewriteRefs(localSchema, localToHoisted, selfRef);
95
+ }
84
96
  }
85
- const selfRef = `#/$defs/${parentPrefix}`;
86
- const rewrittenParent = rewriteRefs(parentObj, localToHoisted, selfRef);
87
- hoisted[parentName] = rewrittenParent;
88
- for (const [localName, localSchema] of Object.entries(nestedDefs)) {
89
- const hoistedName = `${parentPrefix}-${toKebabCase(localName)}`;
90
- hoisted[hoistedName] = rewriteRefs(localSchema, localToHoisted, selfRef);
91
- }
92
- }
93
- return hoisted;
97
+ return hoisted;
94
98
  };
95
- var upgradeDraft07Schema = (schema) => {
96
- if (!isDraft07Schema(schema))
97
- return schema;
98
- const { definitions, $schema: _, ...rest } = schema;
99
- const rawDefs = definitions ?? {};
100
- const renamedDefs = {};
101
- for (const [key, value] of Object.entries(rawDefs)) {
102
- renamedDefs[key] = renameNestedDefs(value);
103
- }
104
- const hoistedDefs = hoistNestedDefs(renamedDefs);
105
- for (const key of Object.keys(hoistedDefs)) {
106
- if (key.startsWith("http://") || key.startsWith("https://")) {
107
- const shortName = refToFilename(key);
108
- if (shortName && !(shortName in hoistedDefs)) {
109
- hoistedDefs[shortName] = hoistedDefs[key];
110
- }
99
+ /**
100
+ * Upgrades a draft-07 schema so it is compatible with the build pipeline.
101
+ * If the schema is not draft-07, it is returned unchanged.
102
+ *
103
+ * @param schema - The raw JSON Schema (any draft)
104
+ * @returns The schema with `definitions` renamed to `$defs` at the root,
105
+ * nested defs hoisted to the root, and internal refs rewritten
106
+ */
107
+ export const upgradeDraft07Schema = (schema) => {
108
+ if (!isDraft07Schema(schema))
109
+ return schema;
110
+ // Rename root-level `definitions` to `$defs` (keep all other keys as-is)
111
+ const { definitions, $schema: _, ...rest } = schema;
112
+ const rawDefs = (definitions ?? {});
113
+ // Recursively rename `definitions` → `$defs` inside each definition's body
114
+ // so nested defs are accessible before hoisting
115
+ const renamedDefs = {};
116
+ for (const [key, value] of Object.entries(rawDefs)) {
117
+ renamedDefs[key] = renameNestedDefs(value);
111
118
  }
112
- }
113
- return {
114
- ...rest,
115
- $defs: hoistedDefs
116
- };
117
- };
118
- var renameNestedDefs = (obj) => {
119
- if (typeof obj !== "object" || obj === null)
120
- return obj;
121
- if (Array.isArray(obj))
122
- return obj.map(renameNestedDefs);
123
- const record = obj;
124
- const result = {};
125
- for (const [key, value] of Object.entries(record)) {
126
- if (key === "$ref" && typeof value === "string" && value.startsWith("#/definitions/")) {
127
- result[key] = value.replace("#/definitions/", "#/$defs/");
128
- } else {
129
- const outKey = key === "definitions" ? "$defs" : key;
130
- result[outKey] = renameNestedDefs(value);
119
+ // Hoist nested $defs up to root so the pipeline can resolve all refs flatly
120
+ const hoistedDefs = hoistNestedDefs(renamedDefs);
121
+ // Add short-name aliases for URI-keyed definitions so that internal refs
122
+ // like `#/$defs/draft-07-schema` (produced by self-ref rewriting in hoistNestedDefs)
123
+ // resolve correctly alongside the original URI key lookups.
124
+ for (const key of Object.keys(hoistedDefs)) {
125
+ if (key.startsWith('http://') || key.startsWith('https://')) {
126
+ const shortName = refToFilename(key);
127
+ if (shortName && !(shortName in hoistedDefs)) {
128
+ hoistedDefs[shortName] = hoistedDefs[key];
129
+ }
130
+ }
131
131
  }
132
- }
133
- return result;
132
+ return {
133
+ ...rest,
134
+ $defs: hoistedDefs,
135
+ };
134
136
  };
135
- export {
136
- upgradeDraft07Schema,
137
- isDraft07Schema
137
+ /**
138
+ * Recursively renames `definitions` → `$defs` within a schema value and
139
+ * rewrites `$ref: "#/definitions/X"` to `$ref: "#/$defs/X"` so that
140
+ * `hoistNestedDefs` can map them to their hoisted root-level equivalents.
141
+ * Does NOT hoist — hoisting is done separately at the root level.
142
+ */
143
+ const renameNestedDefs = (obj) => {
144
+ if (typeof obj !== 'object' || obj === null)
145
+ return obj;
146
+ if (Array.isArray(obj))
147
+ return obj.map(renameNestedDefs);
148
+ const record = obj;
149
+ const result = {};
150
+ for (const [key, value] of Object.entries(record)) {
151
+ if (key === '$ref' && typeof value === 'string' && value.startsWith('#/definitions/')) {
152
+ result[key] = value.replace('#/definitions/', '#/$defs/');
153
+ }
154
+ else {
155
+ const outKey = key === 'definitions' ? '$defs' : key;
156
+ result[outKey] = renameNestedDefs(value);
157
+ }
158
+ }
159
+ return result;
138
160
  };
@@ -1,2 +1,3 @@
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
@@ -0,0 +1 @@
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,15 +1,13 @@
1
- // src/validate-array.ts
2
- var validateArray = (input, parser) => {
3
- if (!Array.isArray(input)) {
4
- return [];
5
- }
6
- const len = input.length;
7
- const result = new Array(len);
8
- for (let i = 0;i < len; i++) {
9
- result[i] = parser(input[i]);
10
- }
11
- return result;
12
- };
13
- export {
14
- validateArray
1
+ /** Parses the items of an array with a parser function */
2
+ export const validateArray = (input, parser) => {
3
+ if (!Array.isArray(input)) {
4
+ return [];
5
+ }
6
+ // Pre-allocate the result array for better performance than push()
7
+ const len = input.length;
8
+ const result = new Array(len);
9
+ for (let i = 0; i < len; i++) {
10
+ result[i] = parser(input[i]);
11
+ }
12
+ return result;
15
13
  };
@@ -3,3 +3,4 @@
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
@@ -0,0 +1 @@
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"}