@kravc/schema 2.7.5 → 2.8.0-alpha.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 (170) 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/getReferenceIds.d.ts +169 -0
  31. package/dist/helpers/getReferenceIds.d.ts.map +1 -0
  32. package/dist/helpers/getReferenceIds.js +241 -0
  33. package/dist/helpers/getReferenceIds.js.map +1 -0
  34. package/dist/helpers/got.d.ts +60 -0
  35. package/dist/helpers/got.d.ts.map +1 -0
  36. package/dist/helpers/got.js +72 -0
  37. package/dist/helpers/got.js.map +1 -0
  38. package/dist/helpers/mapObjectProperties.d.ts +150 -0
  39. package/dist/helpers/mapObjectProperties.d.ts.map +1 -0
  40. package/dist/helpers/mapObjectProperties.js +229 -0
  41. package/dist/helpers/mapObjectProperties.js.map +1 -0
  42. package/dist/helpers/normalizeAttributes.d.ts +213 -0
  43. package/dist/helpers/normalizeAttributes.d.ts.map +1 -0
  44. package/dist/helpers/normalizeAttributes.js +243 -0
  45. package/dist/helpers/normalizeAttributes.js.map +1 -0
  46. package/dist/helpers/normalizeProperties.d.ts +168 -0
  47. package/dist/helpers/normalizeProperties.d.ts.map +1 -0
  48. package/dist/helpers/normalizeProperties.js +223 -0
  49. package/dist/helpers/normalizeProperties.js.map +1 -0
  50. package/dist/helpers/normalizeRequired.d.ts +159 -0
  51. package/dist/helpers/normalizeRequired.d.ts.map +1 -0
  52. package/dist/helpers/normalizeRequired.js +206 -0
  53. package/dist/helpers/normalizeRequired.js.map +1 -0
  54. package/dist/helpers/normalizeType.d.ts +81 -0
  55. package/dist/helpers/normalizeType.d.ts.map +1 -0
  56. package/dist/helpers/normalizeType.js +210 -0
  57. package/dist/helpers/normalizeType.js.map +1 -0
  58. package/dist/helpers/nullifyEmptyValues.d.ts +139 -0
  59. package/dist/helpers/nullifyEmptyValues.d.ts.map +1 -0
  60. package/dist/helpers/nullifyEmptyValues.js +191 -0
  61. package/dist/helpers/nullifyEmptyValues.js.map +1 -0
  62. package/dist/helpers/removeRequiredAndDefault.d.ts +106 -0
  63. package/dist/helpers/removeRequiredAndDefault.d.ts.map +1 -0
  64. package/dist/helpers/removeRequiredAndDefault.js +138 -0
  65. package/dist/helpers/removeRequiredAndDefault.js.map +1 -0
  66. package/dist/helpers/validateId.d.ts +39 -0
  67. package/dist/helpers/validateId.d.ts.map +1 -0
  68. package/dist/helpers/validateId.js +51 -0
  69. package/dist/helpers/validateId.js.map +1 -0
  70. package/dist/index.d.ts +7 -0
  71. package/dist/index.d.ts.map +1 -0
  72. package/dist/index.js +17 -0
  73. package/dist/index.js.map +1 -0
  74. package/dist/ld/documentLoader.d.ts +8 -0
  75. package/dist/ld/documentLoader.d.ts.map +1 -0
  76. package/dist/ld/documentLoader.js +24 -0
  77. package/dist/ld/documentLoader.js.map +1 -0
  78. package/dist/ld/getLinkedDataAttributeType.d.ts +10 -0
  79. package/dist/ld/getLinkedDataAttributeType.d.ts.map +1 -0
  80. package/dist/ld/getLinkedDataAttributeType.js +32 -0
  81. package/dist/ld/getLinkedDataAttributeType.js.map +1 -0
  82. package/dist/ld/getLinkedDataContext.d.ts +19 -0
  83. package/dist/ld/getLinkedDataContext.d.ts.map +1 -0
  84. package/dist/ld/getLinkedDataContext.js +50 -0
  85. package/dist/ld/getLinkedDataContext.js.map +1 -0
  86. package/eslint.config.mjs +32 -52
  87. package/examples/credentials/createAccountCredential.ts +27 -0
  88. package/examples/credentials/createMineSweeperScoreCredential.ts +115 -0
  89. package/examples/index.ts +7 -0
  90. package/examples/schemas/FavoriteItemSchema.ts +27 -0
  91. package/examples/{Preferences.yaml → schemas/Preferences.yaml} +2 -0
  92. package/examples/schemas/PreferencesSchema.ts +29 -0
  93. package/examples/schemas/ProfileSchema.ts +91 -0
  94. package/examples/schemas/Status.yaml +3 -0
  95. package/examples/schemas/StatusSchema.ts +12 -0
  96. package/jest.config.mjs +5 -0
  97. package/package.json +28 -21
  98. package/src/CredentialFactory.ts +392 -0
  99. package/src/Schema.ts +583 -0
  100. package/src/ValidationError.ts +90 -0
  101. package/src/Validator.ts +603 -0
  102. package/src/__tests__/CredentialFactory.test.ts +588 -0
  103. package/src/__tests__/Schema.test.ts +371 -0
  104. package/src/__tests__/ValidationError.test.ts +235 -0
  105. package/src/__tests__/Validator.test.ts +787 -0
  106. package/src/helpers/JsonSchema.ts +119 -0
  107. package/src/helpers/__tests__/cleanupAttributes.test.ts +943 -0
  108. package/src/helpers/__tests__/cleanupNulls.test.ts +772 -0
  109. package/src/helpers/__tests__/getReferenceIds.test.ts +975 -0
  110. package/src/helpers/__tests__/got.test.ts +193 -0
  111. package/src/helpers/__tests__/mapObjectProperties.test.ts +1126 -0
  112. package/src/helpers/__tests__/normalizeAttributes.test.ts +1435 -0
  113. package/src/helpers/__tests__/normalizeProperties.test.ts +727 -0
  114. package/src/helpers/__tests__/normalizeRequired.test.ts +669 -0
  115. package/src/helpers/__tests__/normalizeType.test.ts +772 -0
  116. package/src/helpers/__tests__/nullifyEmptyValues.test.ts +735 -0
  117. package/src/helpers/__tests__/removeRequiredAndDefault.test.ts +734 -0
  118. package/src/helpers/__tests__/validateId.test.ts +118 -0
  119. package/src/helpers/cleanupAttributes.ts +151 -0
  120. package/src/helpers/cleanupNulls.ts +106 -0
  121. package/src/helpers/getReferenceIds.ts +273 -0
  122. package/src/helpers/got.ts +73 -0
  123. package/src/helpers/mapObjectProperties.ts +272 -0
  124. package/src/helpers/normalizeAttributes.ts +247 -0
  125. package/src/helpers/normalizeProperties.ts +249 -0
  126. package/src/helpers/normalizeRequired.ts +233 -0
  127. package/src/helpers/normalizeType.ts +235 -0
  128. package/src/helpers/nullifyEmptyValues.ts +207 -0
  129. package/src/helpers/removeRequiredAndDefault.ts +151 -0
  130. package/src/helpers/validateId.ts +53 -0
  131. package/src/index.ts +13 -0
  132. package/src/ld/__tests__/documentLoader.test.ts +57 -0
  133. package/src/ld/__tests__/getLinkedDataAttributeType.test.ts +212 -0
  134. package/src/ld/__tests__/getLinkedDataContext.test.ts +378 -0
  135. package/src/ld/documentLoader.ts +28 -0
  136. package/src/ld/getLinkedDataAttributeType.ts +46 -0
  137. package/src/ld/getLinkedDataContext.ts +80 -0
  138. package/tsconfig.json +27 -0
  139. package/types/credentials-context.d.ts +14 -0
  140. package/types/security-context.d.ts +6 -0
  141. package/examples/Status.yaml +0 -3
  142. package/examples/createAccountCredential.js +0 -27
  143. package/examples/createMineSweeperScoreCredential.js +0 -63
  144. package/examples/index.js +0 -9
  145. package/src/CredentialFactory.js +0 -67
  146. package/src/CredentialFactory.spec.js +0 -131
  147. package/src/Schema.js +0 -104
  148. package/src/Schema.spec.js +0 -172
  149. package/src/ValidationError.js +0 -31
  150. package/src/Validator.js +0 -128
  151. package/src/Validator.spec.js +0 -355
  152. package/src/helpers/cleanupAttributes.js +0 -71
  153. package/src/helpers/cleanupNulls.js +0 -42
  154. package/src/helpers/getReferenceIds.js +0 -71
  155. package/src/helpers/mapObject.js +0 -65
  156. package/src/helpers/normalizeAttributes.js +0 -28
  157. package/src/helpers/normalizeProperties.js +0 -61
  158. package/src/helpers/normalizeRequired.js +0 -37
  159. package/src/helpers/normalizeType.js +0 -41
  160. package/src/helpers/nullifyEmptyValues.js +0 -57
  161. package/src/helpers/removeRequiredAndDefault.js +0 -30
  162. package/src/helpers/validateId.js +0 -19
  163. package/src/index.d.ts +0 -25
  164. package/src/index.js +0 -8
  165. package/src/ld/documentLoader.js +0 -25
  166. package/src/ld/documentLoader.spec.js +0 -12
  167. package/src/ld/getLinkedDataContext.js +0 -63
  168. package/src/ld/getLinkedDataType.js +0 -38
  169. /package/examples/{FavoriteItem.yaml → schemas/FavoriteItem.yaml} +0 -0
  170. /package/examples/{Profile.yaml → schemas/Profile.yaml} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JsonSchema.js","sourceRoot":"","sources":["../../src/helpers/JsonSchema.ts"],"names":[],"mappings":""}
@@ -0,0 +1,34 @@
1
+ import type { JsonSchema, TargetObject, JsonSchemasMap } from './JsonSchema';
2
+ /**
3
+ * Removes properties from an object that are not defined in the JSON schema.
4
+ *
5
+ * **Intent:**
6
+ * This function ensures that objects conform to their schema definition by removing
7
+ * any properties that are not explicitly defined in the schema. It performs a deep
8
+ * cleanup, recursively processing nested objects, arrays, and schema references.
9
+ *
10
+ * **Use Cases:**
11
+ * - **Third-party API integrations**: When integrating with external services (e.g., Telegram)
12
+ * that may send additional fields you don't want to process, this function allows you
13
+ * to define a minimal schema and automatically strip unwanted properties.
14
+ * - **Data sanitization**: Clean up objects received from external sources or user input
15
+ * before validation or processing, ensuring only expected fields are present.
16
+ * - **Schema enforcement**: Enforce strict schema compliance by removing any properties
17
+ * that don't match the defined schema structure.
18
+ * - **Pre-validation cleanup**: Remove extraneous properties before schema validation to
19
+ * prevent validation errors from unexpected fields.
20
+ *
21
+ * **Behavior:**
22
+ * - Mutates the input object in-place (does not return a new object)
23
+ * - Recursively processes nested objects, arrays, and schema references ($ref)
24
+ * - Skips enum schemas (returns early without modification)
25
+ * - Only processes object values (skips null, undefined, and primitive values)
26
+ * - Handles array items by cleaning each object item according to the array's item schema
27
+ *
28
+ * @param object - The target object to clean up (mutated in-place)
29
+ * @param jsonSchema - The JSON schema defining allowed properties
30
+ * @param schemasMap - Optional map of schema IDs to schema definitions for resolving $ref references
31
+ */
32
+ declare const cleanupAttributes: (object: TargetObject, jsonSchema: JsonSchema, schemasMap?: JsonSchemasMap) => void;
33
+ export default cleanupAttributes;
34
+ //# sourceMappingURL=cleanupAttributes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cleanupAttributes.d.ts","sourceRoot":"","sources":["../../src/helpers/cleanupAttributes.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,UAAU,EAEV,YAAY,EAEZ,cAAc,EAIf,MAAM,cAAc,CAAC;AAEtB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,QAAA,MAAM,iBAAiB,GACrB,QAAQ,YAAY,EACpB,YAAY,UAAU,EACtB,aAAY,cAAmB,SAqGhC,CAAC;AAEF,eAAe,iBAAiB,CAAC"}
@@ -0,0 +1,113 @@
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 got_1 = __importDefault(require("./got"));
8
+ /**
9
+ * Removes properties from an object that are not defined in the JSON schema.
10
+ *
11
+ * **Intent:**
12
+ * This function ensures that objects conform to their schema definition by removing
13
+ * any properties that are not explicitly defined in the schema. It performs a deep
14
+ * cleanup, recursively processing nested objects, arrays, and schema references.
15
+ *
16
+ * **Use Cases:**
17
+ * - **Third-party API integrations**: When integrating with external services (e.g., Telegram)
18
+ * that may send additional fields you don't want to process, this function allows you
19
+ * to define a minimal schema and automatically strip unwanted properties.
20
+ * - **Data sanitization**: Clean up objects received from external sources or user input
21
+ * before validation or processing, ensuring only expected fields are present.
22
+ * - **Schema enforcement**: Enforce strict schema compliance by removing any properties
23
+ * that don't match the defined schema structure.
24
+ * - **Pre-validation cleanup**: Remove extraneous properties before schema validation to
25
+ * prevent validation errors from unexpected fields.
26
+ *
27
+ * **Behavior:**
28
+ * - Mutates the input object in-place (does not return a new object)
29
+ * - Recursively processes nested objects, arrays, and schema references ($ref)
30
+ * - Skips enum schemas (returns early without modification)
31
+ * - Only processes object values (skips null, undefined, and primitive values)
32
+ * - Handles array items by cleaning each object item according to the array's item schema
33
+ *
34
+ * @param object - The target object to clean up (mutated in-place)
35
+ * @param jsonSchema - The JSON schema defining allowed properties
36
+ * @param schemasMap - Optional map of schema IDs to schema definitions for resolving $ref references
37
+ */
38
+ const cleanupAttributes = (object, jsonSchema, schemasMap = {}) => {
39
+ const { enum: enumItems } = jsonSchema;
40
+ const isEnum = !!enumItems;
41
+ if (isEnum) {
42
+ return;
43
+ }
44
+ const objectSchema = jsonSchema;
45
+ // Guard against malformed schemas without properties
46
+ if (!objectSchema.properties) {
47
+ return;
48
+ }
49
+ for (const fieldName in object) {
50
+ const property = objectSchema.properties[fieldName];
51
+ const isPropertyUndefined = (0, lodash_1.isUndefined)(property);
52
+ if (isPropertyUndefined) {
53
+ // NOTE: Delete object property if it's not defined in the object schema:
54
+ delete object[fieldName];
55
+ continue;
56
+ }
57
+ const { $ref: refSchemaId } = property;
58
+ const isReference = !(0, lodash_1.isUndefined)(refSchemaId);
59
+ if (isReference) {
60
+ const referenceSchema = (0, got_1.default)(schemasMap, refSchemaId, 'Schema "$PATH" not found');
61
+ const fieldValue = object[fieldName];
62
+ // Only recursively cleanup if the value is an object (not null, undefined, or primitive)
63
+ if (fieldValue && typeof fieldValue === 'object' && !Array.isArray(fieldValue)) {
64
+ cleanupAttributes(fieldValue, referenceSchema, schemasMap);
65
+ }
66
+ continue;
67
+ }
68
+ const { type } = property;
69
+ const isObject = type === 'object';
70
+ if (isObject) {
71
+ const { properties = {} } = property;
72
+ const fieldValue = object[fieldName];
73
+ const isObjectValue = fieldValue &&
74
+ typeof fieldValue === 'object' &&
75
+ !Array.isArray(fieldValue);
76
+ if (isObjectValue) {
77
+ const nestedJsonSchema = {
78
+ id: `${objectSchema.id}.${fieldName}.properties`,
79
+ properties
80
+ };
81
+ cleanupAttributes(fieldValue, nestedJsonSchema, schemasMap);
82
+ }
83
+ continue;
84
+ }
85
+ const isArray = type === 'array';
86
+ if (isArray) {
87
+ const { items } = property;
88
+ const fieldValue = object[fieldName];
89
+ const isArrayValue = Array.isArray(fieldValue);
90
+ if (isArrayValue && items) {
91
+ const { $ref: itemRefSchemaId } = items;
92
+ const { properties: itemObjectProperties = {} } = items;
93
+ const isItemReference = !(0, lodash_1.isUndefined)(itemRefSchemaId);
94
+ const itemSchema = isItemReference
95
+ ? (0, got_1.default)(schemasMap, itemRefSchemaId, 'Schema "$PATH" not found')
96
+ : {
97
+ id: `${objectSchema.id}.${fieldName}.items.properties`,
98
+ properties: itemObjectProperties
99
+ };
100
+ for (const item of fieldValue) {
101
+ const isObjectItem = item &&
102
+ typeof item === 'object' &&
103
+ !Array.isArray(item);
104
+ if (isObjectItem) {
105
+ cleanupAttributes(item, itemSchema, schemasMap);
106
+ }
107
+ }
108
+ }
109
+ }
110
+ }
111
+ };
112
+ exports.default = cleanupAttributes;
113
+ //# sourceMappingURL=cleanupAttributes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cleanupAttributes.js","sourceRoot":"","sources":["../../src/helpers/cleanupAttributes.ts"],"names":[],"mappings":";;;;;AAAA,mCAAqC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0BAA0B,CAAC;oBAC9D,CAAC,CAAC;wBACA,EAAE,EAAE,GAAG,YAAY,CAAC,EAAE,IAAI,SAAS,mBAAmB;wBACtD,UAAU,EAAE,oBAAoB;qBACjC,CAAC;gBAEJ,KAAK,MAAM,IAAI,IAAI,UAAU,EAAE,CAAC;oBAC9B,MAAM,YAAY,GAAG,IAAI;wBACvB,OAAO,IAAI,KAAK,QAAQ;wBACxB,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;oBAEvB,IAAI,YAAY,EAAE,CAAC;wBACjB,iBAAiB,CAAC,IAAoB,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC;oBAClE,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;AACH,CAAC,CAAC;AAEF,kBAAe,iBAAiB,CAAC"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Returns a deep copy of the object with all null properties removed.
3
+ *
4
+ * This is the main exported function that creates a clone of the input object
5
+ * and removes all null properties recursively before returning it.
6
+ *
7
+ * @param object - The object to clean (will be cloned, original is not modified)
8
+ * @returns A new object with all null properties removed recursively
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * const dirty = {
13
+ * name: 'John',
14
+ * age: null,
15
+ * address: {
16
+ * street: 'Main St',
17
+ * zip: null
18
+ * }
19
+ * };
20
+ *
21
+ * const clean = cleanupNulls(dirty);
22
+ * // Result: { name: 'John', address: { street: 'Main St' } }
23
+ * // Original 'dirty' object is unchanged
24
+ * ```
25
+ */
26
+ export default function (object: Record<string, unknown>): Record<string, unknown>;
27
+ //# sourceMappingURL=cleanupNulls.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cleanupNulls.d.ts","sourceRoot":"","sources":["../../src/helpers/cleanupNulls.ts"],"names":[],"mappings":"AA0EA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,CAAC,OAAO,WAAW,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,2BAMvD"}
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = default_1;
4
+ const lodash_1 = require("lodash");
5
+ const { isArray } = Array;
6
+ /**
7
+ * Recursively removes null properties from an object.
8
+ *
9
+ * **Intent:**
10
+ * This function provides a deep cleanup of objects by removing all properties
11
+ * that have `null` values. It's designed to sanitize objects before validation,
12
+ * storage, or transmission by eliminating explicitly null fields.
13
+ *
14
+ * **Use Cases:**
15
+ * - **Pre-validation cleanup**: Remove null values before schema validation to
16
+ * prevent validation errors from optional fields that were explicitly set to null.
17
+ * This is particularly useful when `shouldCleanupNulls` is enabled in the Validator,
18
+ * allowing you to clean objects before `cleanupAttributes` removes undefined properties.
19
+ * - **Data sanitization**: Clean objects received from external sources (APIs, user input,
20
+ * databases) by removing null properties that may have been set during data transformation
21
+ * or migration processes.
22
+ * - **API response normalization**: Prepare objects for API responses by removing null fields,
23
+ * reducing payload size and ensuring consistent data structures across different endpoints.
24
+ * - **Database operations**: Clean objects before database storage or updates, removing
25
+ * null fields that might cause issues with database constraints or indexing.
26
+ * - **JSON serialization optimization**: Reduce JSON payload size by removing null properties
27
+ * before serialization, which is especially beneficial for large objects or high-frequency
28
+ * API calls.
29
+ * - **Optional field handling**: Remove explicitly null optional fields that weren't provided
30
+ * by the user, distinguishing between "field not provided" (undefined) and "field set to null".
31
+ *
32
+ * **Behavior:**
33
+ * - Returns a deep clone of the input object (does not mutate the original)
34
+ * - Recursively processes nested objects and arrays at all depth levels
35
+ * - Only removes properties with `null` values (preserves `undefined`, `0`, `false`, `''`, etc.)
36
+ * - Skips non-object values (returns early for primitives)
37
+ * - Handles arrays by recursively processing each item
38
+ * - Preserves object structure and non-null values exactly as they are
39
+ *
40
+ * @param target - The target object to clean (processed recursively, not mutated)
41
+ */
42
+ const cleanupNulls = (target) => {
43
+ const shouldSkip = !(0, lodash_1.isObject)(target);
44
+ if (shouldSkip) {
45
+ return;
46
+ }
47
+ for (const key in target) {
48
+ const value = target[key];
49
+ if (isArray(value)) {
50
+ for (const item of value) {
51
+ cleanupNulls(item);
52
+ }
53
+ continue;
54
+ }
55
+ if ((0, lodash_1.isObject)(value)) {
56
+ cleanupNulls(value);
57
+ continue;
58
+ }
59
+ const isNull = value === null;
60
+ if (isNull) {
61
+ delete target[key];
62
+ }
63
+ }
64
+ };
65
+ /**
66
+ * Returns a deep copy of the object with all null properties removed.
67
+ *
68
+ * This is the main exported function that creates a clone of the input object
69
+ * and removes all null properties recursively before returning it.
70
+ *
71
+ * @param object - The object to clean (will be cloned, original is not modified)
72
+ * @returns A new object with all null properties removed recursively
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * const dirty = {
77
+ * name: 'John',
78
+ * age: null,
79
+ * address: {
80
+ * street: 'Main St',
81
+ * zip: null
82
+ * }
83
+ * };
84
+ *
85
+ * const clean = cleanupNulls(dirty);
86
+ * // Result: { name: 'John', address: { street: 'Main St' } }
87
+ * // Original 'dirty' object is unchanged
88
+ * ```
89
+ */
90
+ function default_1(object) {
91
+ const clone = (0, lodash_1.cloneDeep)(object);
92
+ cleanupNulls(clone);
93
+ return clone;
94
+ }
95
+ ;
96
+ //# sourceMappingURL=cleanupNulls.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cleanupNulls.js","sourceRoot":"","sources":["../../src/helpers/cleanupNulls.ts"],"names":[],"mappings":";;AAmGA,4BAMC;AAzGD,mCAA6C;AAI7C,MAAM,EAAE,OAAO,EAAE,GAAG,KAAK,CAAC;AAE1B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,MAAM,YAAY,GAAG,CAAC,MAAoB,EAAE,EAAE;IAC5C,MAAM,UAAU,GAAG,CAAC,IAAA,iBAAQ,EAAC,MAAM,CAAC,CAAC;IAErC,IAAI,UAAU,EAAE,CAAC;QACf,OAAO;IACT,CAAC;IAED,KAAK,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;QACzB,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;QAE1B,IAAI,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;YACnB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;gBACzB,YAAY,CAAC,IAAI,CAAC,CAAC;YACrB,CAAC;YAED,SAAS;QACX,CAAC;QAED,IAAI,IAAA,iBAAQ,EAAC,KAAK,CAAC,EAAE,CAAC;YACpB,YAAY,CAAC,KAAgC,CAAC,CAAC;YAE/C,SAAS;QACX,CAAC;QAED,MAAM,MAAM,GAAG,KAAK,KAAK,IAAI,CAAC;QAE9B,IAAI,MAAM,EAAE,CAAC;YACX,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;QACrB,CAAC;IACH,CAAC;AACH,CAAC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,mBAAyB,MAA+B;IACtD,MAAM,KAAK,GAAG,IAAA,kBAAS,EAAC,MAAM,CAAC,CAAC;IAEhC,YAAY,CAAC,KAAK,CAAC,CAAC;IAEpB,OAAO,KAAK,CAAC;AACf,CAAC;AAAA,CAAC"}
@@ -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"}