@amritk/helpers 0.6.2 → 0.7.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.
- package/dist/build-dynamic-ref-map.d.ts +1 -0
- package/dist/build-dynamic-ref-map.d.ts.map +1 -0
- package/dist/build-dynamic-ref-map.js +31 -107
- package/dist/derive-root-type-name.d.ts +1 -0
- package/dist/derive-root-type-name.d.ts.map +1 -0
- package/dist/derive-root-type-name.js +37 -17
- package/dist/extract-dynamic-anchor-defs.d.ts +20 -0
- package/dist/extract-dynamic-anchor-defs.d.ts.map +1 -0
- package/dist/extract-dynamic-anchor-defs.js +30 -0
- package/dist/extract-refs.d.ts +1 -0
- package/dist/extract-refs.d.ts.map +1 -0
- package/dist/extract-refs.js +64 -33
- package/dist/generate-index-barrel.d.ts +29 -0
- package/dist/generate-index-barrel.d.ts.map +1 -0
- package/dist/generate-index-barrel.js +43 -0
- package/dist/generate-type-definition.d.ts +1 -0
- package/dist/generate-type-definition.d.ts.map +1 -0
- package/dist/generate-type-definition.js +449 -567
- package/dist/has-ref.d.ts +1 -0
- package/dist/has-ref.d.ts.map +1 -0
- package/dist/has-ref.js +7 -6
- package/dist/is-object.d.ts +1 -0
- package/dist/is-object.d.ts.map +1 -0
- package/dist/is-object.js +14 -5
- package/dist/mjst-extension.d.ts +1 -0
- package/dist/mjst-extension.d.ts.map +1 -0
- package/dist/mjst-extension.js +50 -122
- package/dist/parse-documentation.d.ts +1 -0
- package/dist/parse-documentation.d.ts.map +1 -0
- package/dist/parse-documentation.js +124 -87
- package/dist/ref-to-filename.d.ts +1 -0
- package/dist/ref-to-filename.d.ts.map +1 -0
- package/dist/ref-to-filename.js +106 -42
- package/dist/ref-to-name.d.ts +1 -0
- package/dist/ref-to-name.d.ts.map +1 -0
- package/dist/ref-to-name.js +39 -53
- package/dist/resolve-dynamic-refs.d.ts +1 -0
- package/dist/resolve-dynamic-refs.d.ts.map +1 -0
- package/dist/resolve-dynamic-refs.js +36 -28
- package/dist/resolve-ref.d.ts +1 -0
- package/dist/resolve-ref.d.ts.map +1 -0
- package/dist/resolve-ref.js +74 -39
- package/dist/safe-accessor.d.ts +1 -0
- package/dist/safe-accessor.d.ts.map +1 -0
- package/dist/safe-accessor.js +42 -19
- package/dist/schema-guards.d.ts +2 -1
- package/dist/schema-guards.d.ts.map +1 -0
- package/dist/schema-guards.js +119 -120
- package/dist/upgrade-draft07-schema.d.ts +1 -0
- package/dist/upgrade-draft07-schema.d.ts.map +1 -0
- package/dist/upgrade-draft07-schema.js +152 -130
- package/dist/validate-array.d.ts +1 -0
- package/dist/validate-array.d.ts.map +1 -0
- package/dist/validate-array.js +12 -14
- package/dist/validate-record.d.ts +1 -0
- package/dist/validate-record.d.ts.map +1 -0
- package/dist/validate-record.js +15 -17
- package/dist/walk-ref-graph.d.ts +54 -0
- package/dist/walk-ref-graph.d.ts.map +1 -0
- package/dist/walk-ref-graph.js +123 -0
- package/package.json +9 -88
- package/src/extract-dynamic-anchor-defs.test.ts +29 -0
- package/src/extract-dynamic-anchor-defs.ts +33 -0
- package/src/generate-index-barrel.test.ts +41 -0
- package/src/generate-index-barrel.ts +62 -0
- package/src/walk-ref-graph.test.ts +118 -0
- package/src/walk-ref-graph.ts +184 -0
package/dist/schema-guards.js
CHANGED
|
@@ -1,121 +1,120 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
|
|
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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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
|
-
|
|
132
|
+
return {
|
|
133
|
+
...rest,
|
|
134
|
+
$defs: hoistedDefs,
|
|
135
|
+
};
|
|
134
136
|
};
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
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
|
};
|
package/dist/validate-array.d.ts
CHANGED
|
@@ -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"}
|
package/dist/validate-array.js
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
};
|
|
@@ -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"}
|