@kravc/schema 2.7.6 → 2.8.0-alpha.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.
Files changed (176) hide show
  1. package/README.md +19 -14
  2. package/dist/CredentialFactory.d.ts +345 -0
  3. package/dist/CredentialFactory.d.ts.map +1 -0
  4. package/dist/CredentialFactory.js +381 -0
  5. package/dist/CredentialFactory.js.map +1 -0
  6. package/dist/Schema.d.ts +448 -0
  7. package/dist/Schema.d.ts.map +1 -0
  8. package/dist/Schema.js +506 -0
  9. package/dist/Schema.js.map +1 -0
  10. package/dist/ValidationError.d.ts +70 -0
  11. package/dist/ValidationError.d.ts.map +1 -0
  12. package/dist/ValidationError.js +78 -0
  13. package/dist/ValidationError.js.map +1 -0
  14. package/dist/Validator.d.ts +483 -0
  15. package/dist/Validator.d.ts.map +1 -0
  16. package/dist/Validator.js +570 -0
  17. package/dist/Validator.js.map +1 -0
  18. package/dist/helpers/JsonSchema.d.ts +99 -0
  19. package/dist/helpers/JsonSchema.d.ts.map +1 -0
  20. package/dist/helpers/JsonSchema.js +3 -0
  21. package/dist/helpers/JsonSchema.js.map +1 -0
  22. package/dist/helpers/cleanupAttributes.d.ts +34 -0
  23. package/dist/helpers/cleanupAttributes.d.ts.map +1 -0
  24. package/dist/helpers/cleanupAttributes.js +113 -0
  25. package/dist/helpers/cleanupAttributes.js.map +1 -0
  26. package/dist/helpers/cleanupNulls.d.ts +27 -0
  27. package/dist/helpers/cleanupNulls.d.ts.map +1 -0
  28. package/dist/helpers/cleanupNulls.js +96 -0
  29. package/dist/helpers/cleanupNulls.js.map +1 -0
  30. package/dist/helpers/createSchemasMap.d.ts +67 -0
  31. package/dist/helpers/createSchemasMap.d.ts.map +1 -0
  32. package/dist/helpers/createSchemasMap.js +200 -0
  33. package/dist/helpers/createSchemasMap.js.map +1 -0
  34. package/dist/helpers/getReferenceIds.d.ts +169 -0
  35. package/dist/helpers/getReferenceIds.d.ts.map +1 -0
  36. package/dist/helpers/getReferenceIds.js +241 -0
  37. package/dist/helpers/getReferenceIds.js.map +1 -0
  38. package/dist/helpers/got.d.ts +60 -0
  39. package/dist/helpers/got.d.ts.map +1 -0
  40. package/dist/helpers/got.js +72 -0
  41. package/dist/helpers/got.js.map +1 -0
  42. package/dist/helpers/mapObjectProperties.d.ts +150 -0
  43. package/dist/helpers/mapObjectProperties.d.ts.map +1 -0
  44. package/dist/helpers/mapObjectProperties.js +229 -0
  45. package/dist/helpers/mapObjectProperties.js.map +1 -0
  46. package/dist/helpers/normalizeAttributes.d.ts +213 -0
  47. package/dist/helpers/normalizeAttributes.d.ts.map +1 -0
  48. package/dist/helpers/normalizeAttributes.js +243 -0
  49. package/dist/helpers/normalizeAttributes.js.map +1 -0
  50. package/dist/helpers/normalizeProperties.d.ts +168 -0
  51. package/dist/helpers/normalizeProperties.d.ts.map +1 -0
  52. package/dist/helpers/normalizeProperties.js +223 -0
  53. package/dist/helpers/normalizeProperties.js.map +1 -0
  54. package/dist/helpers/normalizeRequired.d.ts +159 -0
  55. package/dist/helpers/normalizeRequired.d.ts.map +1 -0
  56. package/dist/helpers/normalizeRequired.js +206 -0
  57. package/dist/helpers/normalizeRequired.js.map +1 -0
  58. package/dist/helpers/normalizeType.d.ts +81 -0
  59. package/dist/helpers/normalizeType.d.ts.map +1 -0
  60. package/dist/helpers/normalizeType.js +210 -0
  61. package/dist/helpers/normalizeType.js.map +1 -0
  62. package/dist/helpers/nullifyEmptyValues.d.ts +139 -0
  63. package/dist/helpers/nullifyEmptyValues.d.ts.map +1 -0
  64. package/dist/helpers/nullifyEmptyValues.js +191 -0
  65. package/dist/helpers/nullifyEmptyValues.js.map +1 -0
  66. package/dist/helpers/removeRequiredAndDefault.d.ts +106 -0
  67. package/dist/helpers/removeRequiredAndDefault.d.ts.map +1 -0
  68. package/dist/helpers/removeRequiredAndDefault.js +138 -0
  69. package/dist/helpers/removeRequiredAndDefault.js.map +1 -0
  70. package/dist/helpers/validateId.d.ts +39 -0
  71. package/dist/helpers/validateId.d.ts.map +1 -0
  72. package/dist/helpers/validateId.js +51 -0
  73. package/dist/helpers/validateId.js.map +1 -0
  74. package/dist/index.d.ts +9 -0
  75. package/dist/index.d.ts.map +1 -0
  76. package/dist/index.js +21 -0
  77. package/dist/index.js.map +1 -0
  78. package/dist/ld/documentLoader.d.ts +8 -0
  79. package/dist/ld/documentLoader.d.ts.map +1 -0
  80. package/dist/ld/documentLoader.js +24 -0
  81. package/dist/ld/documentLoader.js.map +1 -0
  82. package/dist/ld/getLinkedDataAttributeType.d.ts +10 -0
  83. package/dist/ld/getLinkedDataAttributeType.d.ts.map +1 -0
  84. package/dist/ld/getLinkedDataAttributeType.js +32 -0
  85. package/dist/ld/getLinkedDataAttributeType.js.map +1 -0
  86. package/dist/ld/getLinkedDataContext.d.ts +19 -0
  87. package/dist/ld/getLinkedDataContext.d.ts.map +1 -0
  88. package/dist/ld/getLinkedDataContext.js +50 -0
  89. package/dist/ld/getLinkedDataContext.js.map +1 -0
  90. package/eslint.config.mjs +32 -52
  91. package/examples/credentials/createAccountCredential.ts +27 -0
  92. package/examples/credentials/createMineSweeperScoreCredential.ts +115 -0
  93. package/examples/index.ts +7 -0
  94. package/examples/schemas/FavoriteItemSchema.ts +27 -0
  95. package/examples/{Preferences.yaml → schemas/Preferences.yaml} +2 -0
  96. package/examples/schemas/PreferencesSchema.ts +29 -0
  97. package/examples/schemas/ProfileSchema.ts +91 -0
  98. package/examples/schemas/Status.yaml +3 -0
  99. package/examples/schemas/StatusSchema.ts +12 -0
  100. package/jest.config.mjs +5 -0
  101. package/package.json +27 -20
  102. package/src/CredentialFactory.ts +392 -0
  103. package/src/Schema.ts +583 -0
  104. package/src/ValidationError.ts +90 -0
  105. package/src/Validator.ts +603 -0
  106. package/src/__tests__/CredentialFactory.test.ts +588 -0
  107. package/src/__tests__/Schema.test.ts +371 -0
  108. package/src/__tests__/ValidationError.test.ts +235 -0
  109. package/src/__tests__/Validator.test.ts +787 -0
  110. package/src/helpers/JsonSchema.ts +119 -0
  111. package/src/helpers/__tests__/cleanupAttributes.test.ts +943 -0
  112. package/src/helpers/__tests__/cleanupNulls.test.ts +772 -0
  113. package/src/helpers/__tests__/createSchemasMap.test.ts +238 -0
  114. package/src/helpers/__tests__/getReferenceIds.test.ts +975 -0
  115. package/src/helpers/__tests__/got.test.ts +193 -0
  116. package/src/helpers/__tests__/mapObjectProperties.test.ts +1126 -0
  117. package/src/helpers/__tests__/normalizeAttributes.test.ts +1435 -0
  118. package/src/helpers/__tests__/normalizeProperties.test.ts +727 -0
  119. package/src/helpers/__tests__/normalizeRequired.test.ts +669 -0
  120. package/src/helpers/__tests__/normalizeType.test.ts +772 -0
  121. package/src/helpers/__tests__/nullifyEmptyValues.test.ts +735 -0
  122. package/src/helpers/__tests__/removeRequiredAndDefault.test.ts +734 -0
  123. package/src/helpers/__tests__/validateId.test.ts +118 -0
  124. package/src/helpers/cleanupAttributes.ts +151 -0
  125. package/src/helpers/cleanupNulls.ts +106 -0
  126. package/src/helpers/createSchemasMap.ts +212 -0
  127. package/src/helpers/getReferenceIds.ts +273 -0
  128. package/src/helpers/got.ts +73 -0
  129. package/src/helpers/mapObjectProperties.ts +272 -0
  130. package/src/helpers/normalizeAttributes.ts +247 -0
  131. package/src/helpers/normalizeProperties.ts +249 -0
  132. package/src/helpers/normalizeRequired.ts +233 -0
  133. package/src/helpers/normalizeType.ts +235 -0
  134. package/src/helpers/nullifyEmptyValues.ts +207 -0
  135. package/src/helpers/removeRequiredAndDefault.ts +151 -0
  136. package/src/helpers/validateId.ts +53 -0
  137. package/src/index.ts +17 -0
  138. package/src/ld/__tests__/documentLoader.test.ts +57 -0
  139. package/src/ld/__tests__/getLinkedDataAttributeType.test.ts +212 -0
  140. package/src/ld/__tests__/getLinkedDataContext.test.ts +378 -0
  141. package/src/ld/documentLoader.ts +28 -0
  142. package/src/ld/getLinkedDataAttributeType.ts +46 -0
  143. package/src/ld/getLinkedDataContext.ts +80 -0
  144. package/tsconfig.json +27 -0
  145. package/types/credentials-context.d.ts +14 -0
  146. package/types/security-context.d.ts +6 -0
  147. package/examples/Status.yaml +0 -3
  148. package/examples/createAccountCredential.js +0 -27
  149. package/examples/createMineSweeperScoreCredential.js +0 -63
  150. package/examples/index.js +0 -9
  151. package/src/CredentialFactory.js +0 -67
  152. package/src/CredentialFactory.spec.js +0 -131
  153. package/src/Schema.js +0 -104
  154. package/src/Schema.spec.js +0 -172
  155. package/src/ValidationError.js +0 -31
  156. package/src/Validator.js +0 -128
  157. package/src/Validator.spec.js +0 -355
  158. package/src/helpers/cleanupAttributes.js +0 -71
  159. package/src/helpers/cleanupNulls.js +0 -42
  160. package/src/helpers/getReferenceIds.js +0 -71
  161. package/src/helpers/mapObject.js +0 -65
  162. package/src/helpers/normalizeAttributes.js +0 -28
  163. package/src/helpers/normalizeProperties.js +0 -61
  164. package/src/helpers/normalizeRequired.js +0 -37
  165. package/src/helpers/normalizeType.js +0 -41
  166. package/src/helpers/nullifyEmptyValues.js +0 -57
  167. package/src/helpers/removeRequiredAndDefault.js +0 -30
  168. package/src/helpers/validateId.js +0 -19
  169. package/src/index.d.ts +0 -25
  170. package/src/index.js +0 -8
  171. package/src/ld/documentLoader.js +0 -25
  172. package/src/ld/documentLoader.spec.js +0 -12
  173. package/src/ld/getLinkedDataContext.js +0 -63
  174. package/src/ld/getLinkedDataType.js +0 -38
  175. /package/examples/{FavoriteItem.yaml → schemas/FavoriteItem.yaml} +0 -0
  176. /package/examples/{Profile.yaml → schemas/Profile.yaml} +0 -0
@@ -0,0 +1,169 @@
1
+ import Schema from '../Schema';
2
+ /**
3
+ * Recursively extracts all referenced schema IDs from a schema structure.
4
+ *
5
+ * **Intent:** Traverse a schema's entire structure (including nested objects and arrays)
6
+ * to collect all schema IDs that are referenced via `$ref` properties. This enables
7
+ * dependency resolution, schema bundling, and validation of schema completeness.
8
+ *
9
+ * **Use Cases:**
10
+ * - **Dependency Resolution:** Identify all schemas that a given schema depends on,
11
+ * ensuring they are loaded before validation
12
+ * - **Schema Bundling:** Collect all related schemas into a single bundle for
13
+ * distribution or storage
14
+ * - **Validation Preparation:** Pre-load all referenced schemas to ensure complete
15
+ * validation context
16
+ * - **Dependency Graph Building:** Understand the relationships and dependencies
17
+ * between schemas in a schema registry
18
+ * - **Schema Analysis:** Analyze schema complexity by identifying all dependencies
19
+ * - **Circular Reference Detection:** (Note: current implementation does not handle
20
+ * circular references and will recurse infinitely)
21
+ *
22
+ * **Behavior:**
23
+ * - Returns an empty array for enum schemas (they don't reference other schemas)
24
+ * - Recursively traverses nested object properties
25
+ * - Handles array items that reference schemas or contain object properties
26
+ * - Follows nested references to collect transitive dependencies
27
+ * - Returns unique schema IDs (deduplicates if same schema is referenced multiple times)
28
+ * - Throws an error if a referenced schema is not found in the schemasMap
29
+ *
30
+ * **Example - Simple Reference:**
31
+ * ```typescript
32
+ * const userSchema = new Schema({
33
+ * profile: { $ref: 'Profile' }
34
+ * }, 'User');
35
+ *
36
+ * const profileSchema = new Schema({
37
+ * name: { type: 'string' }
38
+ * }, 'Profile');
39
+ *
40
+ * const schemasMap = { 'Profile': profileSchema };
41
+ * const referenceIds = getReferenceIds(userSchema, schemasMap);
42
+ * // Returns: ['Profile']
43
+ * ```
44
+ *
45
+ * **Example - Multiple References:**
46
+ * ```typescript
47
+ * const orderSchema = new Schema({
48
+ * customer: { $ref: 'Customer' },
49
+ * product: { $ref: 'Product' },
50
+ * shipping: { $ref: 'Address' }
51
+ * }, 'Order');
52
+ *
53
+ * const schemasMap = {
54
+ * 'Customer': customerSchema,
55
+ * 'Product': productSchema,
56
+ * 'Address': addressSchema
57
+ * };
58
+ * const referenceIds = getReferenceIds(orderSchema, schemasMap);
59
+ * // Returns: ['Customer', 'Product', 'Address']
60
+ * ```
61
+ *
62
+ * **Example - Nested References:**
63
+ * ```typescript
64
+ * const userSchema = new Schema({
65
+ * profile: { $ref: 'Profile' }
66
+ * }, 'User');
67
+ *
68
+ * const profileSchema = new Schema({
69
+ * address: { $ref: 'Address' }
70
+ * }, 'Profile');
71
+ *
72
+ * const addressSchema = new Schema({
73
+ * street: { type: 'string' }
74
+ * }, 'Address');
75
+ *
76
+ * const schemasMap = {
77
+ * 'Profile': profileSchema,
78
+ * 'Address': addressSchema
79
+ * };
80
+ * const referenceIds = getReferenceIds(userSchema, schemasMap);
81
+ * // Returns: ['Profile', 'Address'] (includes transitive dependencies)
82
+ * ```
83
+ *
84
+ * **Example - Array with Reference Items:**
85
+ * ```typescript
86
+ * const orderSchema = new Schema({
87
+ * items: {
88
+ * type: 'array',
89
+ * items: { $ref: 'OrderItem' }
90
+ * }
91
+ * }, 'Order');
92
+ *
93
+ * const schemasMap = { 'OrderItem': orderItemSchema };
94
+ * const referenceIds = getReferenceIds(orderSchema, schemasMap);
95
+ * // Returns: ['OrderItem']
96
+ * ```
97
+ *
98
+ * **Example - Nested Object Properties:**
99
+ * ```typescript
100
+ * const userSchema = new Schema({
101
+ * contact: {
102
+ * type: 'object',
103
+ * properties: {
104
+ * address: { $ref: 'Address' }
105
+ * }
106
+ * }
107
+ * }, 'User');
108
+ *
109
+ * const schemasMap = { 'Address': addressSchema };
110
+ * const referenceIds = getReferenceIds(userSchema, schemasMap);
111
+ * // Returns: ['Address']
112
+ * ```
113
+ *
114
+ * **Example - Complex Mixed Structure:**
115
+ * ```typescript
116
+ * const orderSchema = new Schema({
117
+ * customer: { $ref: 'Customer' },
118
+ * items: {
119
+ * type: 'array',
120
+ * items: {
121
+ * type: 'object',
122
+ * properties: {
123
+ * product: { $ref: 'Product' }
124
+ * }
125
+ * }
126
+ * },
127
+ * shipping: {
128
+ * type: 'object',
129
+ * properties: {
130
+ * address: { $ref: 'Address' }
131
+ * }
132
+ * }
133
+ * }, 'Order');
134
+ *
135
+ * const schemasMap = {
136
+ * 'Customer': customerSchema,
137
+ * 'Product': productSchema,
138
+ * 'Address': addressSchema
139
+ * };
140
+ * const referenceIds = getReferenceIds(orderSchema, schemasMap);
141
+ * // Returns: ['Customer', 'Product', 'Address']
142
+ * ```
143
+ *
144
+ * **Example - Duplicate References:**
145
+ * ```typescript
146
+ * const schema = new Schema({
147
+ * field1: { $ref: 'SharedSchema' },
148
+ * field2: { $ref: 'SharedSchema' }
149
+ * }, 'Test');
150
+ *
151
+ * const schemasMap = { 'SharedSchema': sharedSchema };
152
+ * const referenceIds = getReferenceIds(schema, schemasMap);
153
+ * // Returns: ['SharedSchema'] (deduplicated)
154
+ * ```
155
+ *
156
+ * @param schema - The schema to extract references from
157
+ * @param schemasMap - A map of schema IDs to Schema instances, used to resolve
158
+ * referenced schemas and traverse nested references
159
+ * @returns An array of unique schema IDs that are referenced (directly or indirectly)
160
+ * by the given schema
161
+ * @throws Error if a referenced schema is not found in the schemasMap
162
+ *
163
+ * **Limitations:**
164
+ * - Does not handle circular references (will cause infinite recursion)
165
+ * - Requires all referenced schemas to be present in schemasMap
166
+ */
167
+ declare const getReferenceIds: (schema: Schema, schemasMap: Record<string, Schema>) => string[];
168
+ export default getReferenceIds;
169
+ //# sourceMappingURL=getReferenceIds.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getReferenceIds.d.ts","sourceRoot":"","sources":["../../src/helpers/getReferenceIds.ts"],"names":[],"mappings":"AAEA,OAAO,MAAM,MAAM,WAAW,CAAC;AAU/B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoKG;AACH,QAAA,MAAM,eAAe,GAAI,QAAQ,MAAM,EAAE,YAAY,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KAAG,MAAM,EA6FnF,CAAC;AAEF,eAAe,eAAe,CAAC"}
@@ -0,0 +1,241 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const lodash_1 = require("lodash");
7
+ const Schema_1 = __importDefault(require("../Schema"));
8
+ const got_1 = __importDefault(require("./got"));
9
+ /**
10
+ * Recursively extracts all referenced schema IDs from a schema structure.
11
+ *
12
+ * **Intent:** Traverse a schema's entire structure (including nested objects and arrays)
13
+ * to collect all schema IDs that are referenced via `$ref` properties. This enables
14
+ * dependency resolution, schema bundling, and validation of schema completeness.
15
+ *
16
+ * **Use Cases:**
17
+ * - **Dependency Resolution:** Identify all schemas that a given schema depends on,
18
+ * ensuring they are loaded before validation
19
+ * - **Schema Bundling:** Collect all related schemas into a single bundle for
20
+ * distribution or storage
21
+ * - **Validation Preparation:** Pre-load all referenced schemas to ensure complete
22
+ * validation context
23
+ * - **Dependency Graph Building:** Understand the relationships and dependencies
24
+ * between schemas in a schema registry
25
+ * - **Schema Analysis:** Analyze schema complexity by identifying all dependencies
26
+ * - **Circular Reference Detection:** (Note: current implementation does not handle
27
+ * circular references and will recurse infinitely)
28
+ *
29
+ * **Behavior:**
30
+ * - Returns an empty array for enum schemas (they don't reference other schemas)
31
+ * - Recursively traverses nested object properties
32
+ * - Handles array items that reference schemas or contain object properties
33
+ * - Follows nested references to collect transitive dependencies
34
+ * - Returns unique schema IDs (deduplicates if same schema is referenced multiple times)
35
+ * - Throws an error if a referenced schema is not found in the schemasMap
36
+ *
37
+ * **Example - Simple Reference:**
38
+ * ```typescript
39
+ * const userSchema = new Schema({
40
+ * profile: { $ref: 'Profile' }
41
+ * }, 'User');
42
+ *
43
+ * const profileSchema = new Schema({
44
+ * name: { type: 'string' }
45
+ * }, 'Profile');
46
+ *
47
+ * const schemasMap = { 'Profile': profileSchema };
48
+ * const referenceIds = getReferenceIds(userSchema, schemasMap);
49
+ * // Returns: ['Profile']
50
+ * ```
51
+ *
52
+ * **Example - Multiple References:**
53
+ * ```typescript
54
+ * const orderSchema = new Schema({
55
+ * customer: { $ref: 'Customer' },
56
+ * product: { $ref: 'Product' },
57
+ * shipping: { $ref: 'Address' }
58
+ * }, 'Order');
59
+ *
60
+ * const schemasMap = {
61
+ * 'Customer': customerSchema,
62
+ * 'Product': productSchema,
63
+ * 'Address': addressSchema
64
+ * };
65
+ * const referenceIds = getReferenceIds(orderSchema, schemasMap);
66
+ * // Returns: ['Customer', 'Product', 'Address']
67
+ * ```
68
+ *
69
+ * **Example - Nested References:**
70
+ * ```typescript
71
+ * const userSchema = new Schema({
72
+ * profile: { $ref: 'Profile' }
73
+ * }, 'User');
74
+ *
75
+ * const profileSchema = new Schema({
76
+ * address: { $ref: 'Address' }
77
+ * }, 'Profile');
78
+ *
79
+ * const addressSchema = new Schema({
80
+ * street: { type: 'string' }
81
+ * }, 'Address');
82
+ *
83
+ * const schemasMap = {
84
+ * 'Profile': profileSchema,
85
+ * 'Address': addressSchema
86
+ * };
87
+ * const referenceIds = getReferenceIds(userSchema, schemasMap);
88
+ * // Returns: ['Profile', 'Address'] (includes transitive dependencies)
89
+ * ```
90
+ *
91
+ * **Example - Array with Reference Items:**
92
+ * ```typescript
93
+ * const orderSchema = new Schema({
94
+ * items: {
95
+ * type: 'array',
96
+ * items: { $ref: 'OrderItem' }
97
+ * }
98
+ * }, 'Order');
99
+ *
100
+ * const schemasMap = { 'OrderItem': orderItemSchema };
101
+ * const referenceIds = getReferenceIds(orderSchema, schemasMap);
102
+ * // Returns: ['OrderItem']
103
+ * ```
104
+ *
105
+ * **Example - Nested Object Properties:**
106
+ * ```typescript
107
+ * const userSchema = new Schema({
108
+ * contact: {
109
+ * type: 'object',
110
+ * properties: {
111
+ * address: { $ref: 'Address' }
112
+ * }
113
+ * }
114
+ * }, 'User');
115
+ *
116
+ * const schemasMap = { 'Address': addressSchema };
117
+ * const referenceIds = getReferenceIds(userSchema, schemasMap);
118
+ * // Returns: ['Address']
119
+ * ```
120
+ *
121
+ * **Example - Complex Mixed Structure:**
122
+ * ```typescript
123
+ * const orderSchema = new Schema({
124
+ * customer: { $ref: 'Customer' },
125
+ * items: {
126
+ * type: 'array',
127
+ * items: {
128
+ * type: 'object',
129
+ * properties: {
130
+ * product: { $ref: 'Product' }
131
+ * }
132
+ * }
133
+ * },
134
+ * shipping: {
135
+ * type: 'object',
136
+ * properties: {
137
+ * address: { $ref: 'Address' }
138
+ * }
139
+ * }
140
+ * }, 'Order');
141
+ *
142
+ * const schemasMap = {
143
+ * 'Customer': customerSchema,
144
+ * 'Product': productSchema,
145
+ * 'Address': addressSchema
146
+ * };
147
+ * const referenceIds = getReferenceIds(orderSchema, schemasMap);
148
+ * // Returns: ['Customer', 'Product', 'Address']
149
+ * ```
150
+ *
151
+ * **Example - Duplicate References:**
152
+ * ```typescript
153
+ * const schema = new Schema({
154
+ * field1: { $ref: 'SharedSchema' },
155
+ * field2: { $ref: 'SharedSchema' }
156
+ * }, 'Test');
157
+ *
158
+ * const schemasMap = { 'SharedSchema': sharedSchema };
159
+ * const referenceIds = getReferenceIds(schema, schemasMap);
160
+ * // Returns: ['SharedSchema'] (deduplicated)
161
+ * ```
162
+ *
163
+ * @param schema - The schema to extract references from
164
+ * @param schemasMap - A map of schema IDs to Schema instances, used to resolve
165
+ * referenced schemas and traverse nested references
166
+ * @returns An array of unique schema IDs that are referenced (directly or indirectly)
167
+ * by the given schema
168
+ * @throws Error if a referenced schema is not found in the schemasMap
169
+ *
170
+ * **Limitations:**
171
+ * - Does not handle circular references (will cause infinite recursion)
172
+ * - Requires all referenced schemas to be present in schemasMap
173
+ */
174
+ const getReferenceIds = (schema, schemasMap) => {
175
+ /** Returns schema from the map by ID */
176
+ const getSchema = (id) => (0, got_1.default)(schemasMap, id, 'Schema "$PATH" not found');
177
+ let referenceIds = [];
178
+ const { jsonSchema } = schema;
179
+ const { enum: isEnum } = jsonSchema;
180
+ if (isEnum) {
181
+ return [];
182
+ }
183
+ const objectSchema = jsonSchema;
184
+ for (const propertyName in objectSchema.properties) {
185
+ const property = objectSchema.properties[propertyName];
186
+ const { $ref: refSchemaId } = property;
187
+ const isReference = !(0, lodash_1.isUndefined)(refSchemaId);
188
+ if (isReference) {
189
+ const refJsonSchema = getSchema(refSchemaId);
190
+ const nestedReferenceIds = getReferenceIds(refJsonSchema, schemasMap);
191
+ referenceIds = [
192
+ refSchemaId,
193
+ ...referenceIds,
194
+ ...nestedReferenceIds
195
+ ];
196
+ continue;
197
+ }
198
+ const { type } = property;
199
+ const isObject = type === 'object';
200
+ if (isObject) {
201
+ // istanbul ignore next - unreachable defensive code: properties is always set by normalizeProperties in Schema constructor
202
+ const { properties = {} } = property;
203
+ const nestedSchema = new Schema_1.default(properties, `${objectSchema.id}.${propertyName}.properties`);
204
+ const nestedReferenceIds = getReferenceIds(nestedSchema, schemasMap);
205
+ referenceIds = [
206
+ ...referenceIds,
207
+ ...nestedReferenceIds
208
+ ];
209
+ continue;
210
+ }
211
+ const isArray = type === 'array';
212
+ if (!isArray) {
213
+ continue;
214
+ }
215
+ const { items } = property;
216
+ const itemRefSchemaId = items.$ref;
217
+ if (itemRefSchemaId) {
218
+ const itemJsonSchema = getSchema(itemRefSchemaId);
219
+ const nestedReferenceIds = getReferenceIds(itemJsonSchema, schemasMap);
220
+ referenceIds = [
221
+ itemRefSchemaId,
222
+ ...referenceIds,
223
+ ...nestedReferenceIds
224
+ ];
225
+ continue;
226
+ }
227
+ const itemProperties = items.properties;
228
+ if (itemProperties) {
229
+ const itemSchema = new Schema_1.default(itemProperties, `${objectSchema.id}.${propertyName}.items.properties`);
230
+ const itemReferenceIds = getReferenceIds(itemSchema, schemasMap);
231
+ referenceIds = [
232
+ ...referenceIds,
233
+ ...itemReferenceIds
234
+ ];
235
+ continue;
236
+ }
237
+ }
238
+ return (0, lodash_1.uniq)(referenceIds);
239
+ };
240
+ exports.default = getReferenceIds;
241
+ //# sourceMappingURL=getReferenceIds.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getReferenceIds.js","sourceRoot":"","sources":["../../src/helpers/getReferenceIds.ts"],"names":[],"mapp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BACf,GAAG,gBAAgB;aACpB,CAAC;YAEF,SAAS;QACX,CAAC;IACH,CAAC;IAED,OAAO,IAAA,aAAI,EAAC,YAAY,CAAC,CAAC;AAC5B,CAAC,CAAC;AAEF,kBAAe,eAAe,CAAC"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Safe required property access: returns a value at `path` or throws if it is `undefined`.
3
+ *
4
+ * **Intent:** Provide strict, fail-fast access to nested object properties. Unlike `lodash/get`,
5
+ * which returns `undefined` for missing keys, `got` treats missing data as an error and throws
6
+ * with a clear message including the path. Use it when the property is required and absence
7
+ * indicates a bug or invalid input.
8
+ *
9
+ * **Use cases:**
10
+ * - **Schema / config lookups:** Fetching a schema or config by ID from a map where absence
11
+ * means invalid reference (e.g. `got(schemasMap, schemaId, 'Schema "$PATH" not found')`).
12
+ * - **Validated config access:** Reading required config or options after validation, when
13
+ * you want to avoid `undefined` checks downstream.
14
+ * - **Strict data traversal:** Walking nested structures (APIs, parsed JSON) where missing
15
+ * keys should fail immediately with a descriptive error instead of propagating `undefined`.
16
+ *
17
+ * **Behavior:** Only `undefined` triggers an error. Falsy but defined values (`null`, `0`,
18
+ * `false`, `''`, `[]`, `{}`) are returned as-is. Uses lodash `get` path syntax: dot notation
19
+ * (`a.b.c`), bracket notation (`items[0]`), or mixed (`data.items[0].id`).
20
+ *
21
+ * @param object - Root object to read from.
22
+ * @param path - Lodash-style path (e.g. `'user.profile.name'`, `'items[0].id'`).
23
+ * @param errorTemplate - Error message template; `$PATH` is replaced with `path`. Default:
24
+ * `'Value is undefined for "$PATH"'`.
25
+ * @returns The value at `path`.
26
+ * @throws {Error} When the value at `path` is `undefined`.
27
+ *
28
+ * @example
29
+ * // Simple property
30
+ * got({ name: 'Jane' }, 'name');
31
+ * // => 'Jane'
32
+ *
33
+ * @example
34
+ * // Nested path
35
+ * got({ user: { profile: { role: 'admin' } } }, 'user.profile.role');
36
+ * // => 'admin'
37
+ *
38
+ * @example
39
+ * // Array index
40
+ * got({ items: ['a', 'b'] }, 'items[0]');
41
+ * // => 'a'
42
+ *
43
+ * @example
44
+ * // Falsy but defined values are returned
45
+ * got({ count: 0, enabled: false }, 'count');
46
+ * // => 0
47
+ *
48
+ * @example
49
+ * // Custom error for schema lookups
50
+ * got(schemasMap, schemaId, 'Schema "$PATH" not found');
51
+ * // => schema for schemaId, or throws with that message
52
+ *
53
+ * @example
54
+ * // Missing property throws
55
+ * got({ name: 'Jane' }, 'age');
56
+ * // throws Error('Value is undefined for "age"')
57
+ */
58
+ declare function got<T>(object: Record<string, T>, path: string, errorTemplate?: string): T;
59
+ export default got;
60
+ //# sourceMappingURL=got.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"got.d.ts","sourceRoot":"","sources":["../../src/helpers/got.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AACH,iBAAS,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,aAAa,GAAE,MAA+B,GAAG,CAAC,CAS1G;AAED,eAAe,GAAG,CAAC"}
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const lodash_1 = require("lodash");
4
+ const DEFAULT_ERROR_TEMPLATE = 'Value is undefined for "$PATH"';
5
+ /**
6
+ * Safe required property access: returns a value at `path` or throws if it is `undefined`.
7
+ *
8
+ * **Intent:** Provide strict, fail-fast access to nested object properties. Unlike `lodash/get`,
9
+ * which returns `undefined` for missing keys, `got` treats missing data as an error and throws
10
+ * with a clear message including the path. Use it when the property is required and absence
11
+ * indicates a bug or invalid input.
12
+ *
13
+ * **Use cases:**
14
+ * - **Schema / config lookups:** Fetching a schema or config by ID from a map where absence
15
+ * means invalid reference (e.g. `got(schemasMap, schemaId, 'Schema "$PATH" not found')`).
16
+ * - **Validated config access:** Reading required config or options after validation, when
17
+ * you want to avoid `undefined` checks downstream.
18
+ * - **Strict data traversal:** Walking nested structures (APIs, parsed JSON) where missing
19
+ * keys should fail immediately with a descriptive error instead of propagating `undefined`.
20
+ *
21
+ * **Behavior:** Only `undefined` triggers an error. Falsy but defined values (`null`, `0`,
22
+ * `false`, `''`, `[]`, `{}`) are returned as-is. Uses lodash `get` path syntax: dot notation
23
+ * (`a.b.c`), bracket notation (`items[0]`), or mixed (`data.items[0].id`).
24
+ *
25
+ * @param object - Root object to read from.
26
+ * @param path - Lodash-style path (e.g. `'user.profile.name'`, `'items[0].id'`).
27
+ * @param errorTemplate - Error message template; `$PATH` is replaced with `path`. Default:
28
+ * `'Value is undefined for "$PATH"'`.
29
+ * @returns The value at `path`.
30
+ * @throws {Error} When the value at `path` is `undefined`.
31
+ *
32
+ * @example
33
+ * // Simple property
34
+ * got({ name: 'Jane' }, 'name');
35
+ * // => 'Jane'
36
+ *
37
+ * @example
38
+ * // Nested path
39
+ * got({ user: { profile: { role: 'admin' } } }, 'user.profile.role');
40
+ * // => 'admin'
41
+ *
42
+ * @example
43
+ * // Array index
44
+ * got({ items: ['a', 'b'] }, 'items[0]');
45
+ * // => 'a'
46
+ *
47
+ * @example
48
+ * // Falsy but defined values are returned
49
+ * got({ count: 0, enabled: false }, 'count');
50
+ * // => 0
51
+ *
52
+ * @example
53
+ * // Custom error for schema lookups
54
+ * got(schemasMap, schemaId, 'Schema "$PATH" not found');
55
+ * // => schema for schemaId, or throws with that message
56
+ *
57
+ * @example
58
+ * // Missing property throws
59
+ * got({ name: 'Jane' }, 'age');
60
+ * // throws Error('Value is undefined for "age"')
61
+ */
62
+ function got(object, path, errorTemplate = DEFAULT_ERROR_TEMPLATE) {
63
+ const value = (0, lodash_1.get)(object, path);
64
+ const shouldThrow = (0, lodash_1.isUndefined)(value);
65
+ if (!shouldThrow) {
66
+ return value;
67
+ }
68
+ throw Error(errorTemplate.replace('$PATH', path));
69
+ }
70
+ ;
71
+ exports.default = got;
72
+ //# sourceMappingURL=got.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"got.js","sourceRoot":"","sources":["../../src/helpers/got.ts"],"names":[],"mappings":";;AAAA,mCAA0C;AAE1C,MAAM,sBAAsB,GAAG,gCAAgC,CAAC;AAEhE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwDG;AACH,SAAS,GAAG,CAAI,MAAyB,EAAE,IAAY,EAAE,gBAAwB,sBAAsB;IACrG,MAAM,KAAK,GAAG,IAAA,YAAG,EAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IAChC,MAAM,WAAW,GAAG,IAAA,oBAAW,EAAC,KAAK,CAAC,CAAC;IAEvC,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,KAAK,CAAC,aAAa,CAAC,OAAO,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC;AACpD,CAAC;AAAA,CAAC;AAEF,kBAAe,GAAG,CAAC"}