@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,139 @@
1
+ import { type SchemaErrorDetail } from 'z-schema';
2
+ import type { TargetObject } from './JsonSchema';
3
+ /**
4
+ * Converts empty string values to null for specific format validation errors.
5
+ *
6
+ * **Intent:**
7
+ * This function provides a post-validation normalization strategy that attempts to
8
+ * resolve format validation errors by converting empty strings to null. This is
9
+ * particularly useful when dealing with optional fields where an empty string
10
+ * represents "no value provided" rather than an invalid format. By converting
11
+ * empty strings to null, the validation may pass if the schema allows null values
12
+ * for optional fields.
13
+ *
14
+ * **Use Cases:**
15
+ * - **Optional field normalization**: When optional string fields receive empty
16
+ * strings from user input or API responses, converting them to null allows
17
+ * validation to succeed if the schema permits null values for optional fields.
18
+ * This is especially common with form inputs that submit empty strings instead
19
+ * of omitting fields entirely.
20
+ * - **Format error recovery**: After schema validation fails with format errors
21
+ * (pattern mismatch, enum mismatch, invalid format), this function attempts to
22
+ * resolve errors by nullifying empty strings. This enables graceful handling
23
+ * of optional fields that failed format validation due to empty values.
24
+ * - **API integration**: When integrating with external APIs or services that
25
+ * send empty strings for optional fields, this function normalizes the data
26
+ * to use null instead, which is often more semantically correct for optional
27
+ * fields in JSON schemas.
28
+ * - **Data transformation pipeline**: As part of a validation and normalization
29
+ * pipeline, this function can be used to clean and normalize data before
30
+ * further processing or storage, ensuring consistent representation of
31
+ * "missing" values.
32
+ * - **User input handling**: When processing user-submitted forms or data where
33
+ * empty string inputs should be treated as "not provided" rather than invalid,
34
+ * this function converts them to null for proper schema validation.
35
+ *
36
+ * **Behavior:**
37
+ * - Returns a deep clone of the input object (does not mutate the original)
38
+ * - Only processes format-related errors (PATTERN, ENUM_MISMATCH, INVALID_FORMAT)
39
+ * - Skips required attributes (marked with `x-required: true`)
40
+ * - Only converts empty strings (`''`) to null, preserving other values
41
+ * - Supports nested paths and array indices
42
+ * - Returns both the modified object and remaining validation errors that
43
+ * couldn't be resolved
44
+ *
45
+ * **Examples:**
46
+ *
47
+ * ```typescript
48
+ * // Example 1: Basic usage with pattern error
49
+ * const object = { email: '' };
50
+ * const error = {
51
+ * code: 'PATTERN',
52
+ * path: '#/email',
53
+ * // ... other error properties
54
+ * };
55
+ * const [result, remainingErrors] = nullifyEmptyValues(object, [error]);
56
+ * // result: { email: null }
57
+ * // remainingErrors: []
58
+ * ```
59
+ *
60
+ * ```typescript
61
+ * // Example 2: Required fields are not nullified
62
+ * const object = { requiredField: '', optionalField: '' };
63
+ * const requiredError = {
64
+ * code: 'PATTERN',
65
+ * path: '#/requiredField',
66
+ * // schema has x-required: true
67
+ * };
68
+ * const optionalError = {
69
+ * code: 'PATTERN',
70
+ * path: '#/optionalField',
71
+ * // schema has no x-required or x-required: false
72
+ * };
73
+ * const [result, remainingErrors] = nullifyEmptyValues(
74
+ * object,
75
+ * [requiredError, optionalError]
76
+ * );
77
+ * // result: { requiredField: '', optionalField: null }
78
+ * // remainingErrors: [requiredError] // required field error remains
79
+ * ```
80
+ *
81
+ * ```typescript
82
+ * // Example 3: Nested paths and arrays
83
+ * const object = {
84
+ * user: {
85
+ * profile: {
86
+ * bio: '',
87
+ * tags: ['', 'tag1', '']
88
+ * }
89
+ * }
90
+ * };
91
+ * const errors = [
92
+ * { code: 'PATTERN', path: '#/user/profile/bio' },
93
+ * { code: 'INVALID_FORMAT', path: '#/user/profile/tags/0' },
94
+ * { code: 'ENUM_MISMATCH', path: '#/user/profile/tags/2' }
95
+ * ];
96
+ * const [result, remainingErrors] = nullifyEmptyValues(object, errors);
97
+ * // result: {
98
+ * // user: {
99
+ * // profile: {
100
+ * // bio: null,
101
+ * // tags: [null, 'tag1', null]
102
+ * // }
103
+ * // }
104
+ * // }
105
+ * // remainingErrors: []
106
+ * ```
107
+ *
108
+ * ```typescript
109
+ * // Example 4: Non-format errors are not processed
110
+ * const object = { field: '' };
111
+ * const formatError = { code: 'PATTERN', path: '#/field' };
112
+ * const typeError = { code: 'INVALID_TYPE', path: '#/field' };
113
+ * const [result, remainingErrors] = nullifyEmptyValues(
114
+ * object,
115
+ * [formatError, typeError]
116
+ * );
117
+ * // result: { field: null }
118
+ * // remainingErrors: [typeError] // type error remains
119
+ * ```
120
+ *
121
+ * ```typescript
122
+ * // Example 5: Non-empty values are preserved
123
+ * const object = { field: 'invalid-value' };
124
+ * const error = { code: 'PATTERN', path: '#/field' };
125
+ * const [result, remainingErrors] = nullifyEmptyValues(object, [error]);
126
+ * // result: { field: 'invalid-value' } // unchanged
127
+ * // remainingErrors: [error] // error remains
128
+ * ```
129
+ *
130
+ * @param object - The target object to process (will be deep cloned, not mutated)
131
+ * @param validationErrors - Array of schema validation errors from z-schema
132
+ * @returns A tuple containing:
133
+ * - `[0]`: Deep clone of the object with empty strings converted to null where applicable
134
+ * - `[1]`: Array of validation errors that couldn't be resolved (required fields,
135
+ * non-format errors, or errors for non-empty values)
136
+ */
137
+ declare const nullifyEmptyValues: (object: TargetObject, validationErrors: SchemaErrorDetail[]) => [TargetObject, SchemaErrorDetail[]];
138
+ export default nullifyEmptyValues;
139
+ //# sourceMappingURL=nullifyEmptyValues.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nullifyEmptyValues.d.ts","sourceRoot":"","sources":["../../src/helpers/nullifyEmptyValues.ts"],"names":[],"mappings":"AACA,OAAO,EAA4B,KAAK,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAE5E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAiBjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqIG;AACH,QAAA,MAAM,kBAAkB,GACtB,QAAQ,YAAY,EACpB,kBAAkB,iBAAiB,EAAE,KACpC,CAAC,YAAY,EAAE,iBAAiB,EAAE,CA+CpC,CAAC;AAEF,eAAe,kBAAkB,CAAC"}
@@ -0,0 +1,191 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const lodash_1 = require("lodash");
4
+ const z_schema_1 = require("z-schema");
5
+ /**
6
+ * Format error codes that indicate validation failures due to format mismatches.
7
+ * These errors can potentially be resolved by converting empty strings to null.
8
+ */
9
+ const FORMAT_ERROR_CODES = [
10
+ 'PATTERN',
11
+ 'ENUM_MISMATCH',
12
+ 'INVALID_FORMAT'
13
+ ];
14
+ /**
15
+ * Values that are considered "empty" and can be safely converted to null.
16
+ */
17
+ const EMPTY_VALUES = [''];
18
+ /**
19
+ * Converts empty string values to null for specific format validation errors.
20
+ *
21
+ * **Intent:**
22
+ * This function provides a post-validation normalization strategy that attempts to
23
+ * resolve format validation errors by converting empty strings to null. This is
24
+ * particularly useful when dealing with optional fields where an empty string
25
+ * represents "no value provided" rather than an invalid format. By converting
26
+ * empty strings to null, the validation may pass if the schema allows null values
27
+ * for optional fields.
28
+ *
29
+ * **Use Cases:**
30
+ * - **Optional field normalization**: When optional string fields receive empty
31
+ * strings from user input or API responses, converting them to null allows
32
+ * validation to succeed if the schema permits null values for optional fields.
33
+ * This is especially common with form inputs that submit empty strings instead
34
+ * of omitting fields entirely.
35
+ * - **Format error recovery**: After schema validation fails with format errors
36
+ * (pattern mismatch, enum mismatch, invalid format), this function attempts to
37
+ * resolve errors by nullifying empty strings. This enables graceful handling
38
+ * of optional fields that failed format validation due to empty values.
39
+ * - **API integration**: When integrating with external APIs or services that
40
+ * send empty strings for optional fields, this function normalizes the data
41
+ * to use null instead, which is often more semantically correct for optional
42
+ * fields in JSON schemas.
43
+ * - **Data transformation pipeline**: As part of a validation and normalization
44
+ * pipeline, this function can be used to clean and normalize data before
45
+ * further processing or storage, ensuring consistent representation of
46
+ * "missing" values.
47
+ * - **User input handling**: When processing user-submitted forms or data where
48
+ * empty string inputs should be treated as "not provided" rather than invalid,
49
+ * this function converts them to null for proper schema validation.
50
+ *
51
+ * **Behavior:**
52
+ * - Returns a deep clone of the input object (does not mutate the original)
53
+ * - Only processes format-related errors (PATTERN, ENUM_MISMATCH, INVALID_FORMAT)
54
+ * - Skips required attributes (marked with `x-required: true`)
55
+ * - Only converts empty strings (`''`) to null, preserving other values
56
+ * - Supports nested paths and array indices
57
+ * - Returns both the modified object and remaining validation errors that
58
+ * couldn't be resolved
59
+ *
60
+ * **Examples:**
61
+ *
62
+ * ```typescript
63
+ * // Example 1: Basic usage with pattern error
64
+ * const object = { email: '' };
65
+ * const error = {
66
+ * code: 'PATTERN',
67
+ * path: '#/email',
68
+ * // ... other error properties
69
+ * };
70
+ * const [result, remainingErrors] = nullifyEmptyValues(object, [error]);
71
+ * // result: { email: null }
72
+ * // remainingErrors: []
73
+ * ```
74
+ *
75
+ * ```typescript
76
+ * // Example 2: Required fields are not nullified
77
+ * const object = { requiredField: '', optionalField: '' };
78
+ * const requiredError = {
79
+ * code: 'PATTERN',
80
+ * path: '#/requiredField',
81
+ * // schema has x-required: true
82
+ * };
83
+ * const optionalError = {
84
+ * code: 'PATTERN',
85
+ * path: '#/optionalField',
86
+ * // schema has no x-required or x-required: false
87
+ * };
88
+ * const [result, remainingErrors] = nullifyEmptyValues(
89
+ * object,
90
+ * [requiredError, optionalError]
91
+ * );
92
+ * // result: { requiredField: '', optionalField: null }
93
+ * // remainingErrors: [requiredError] // required field error remains
94
+ * ```
95
+ *
96
+ * ```typescript
97
+ * // Example 3: Nested paths and arrays
98
+ * const object = {
99
+ * user: {
100
+ * profile: {
101
+ * bio: '',
102
+ * tags: ['', 'tag1', '']
103
+ * }
104
+ * }
105
+ * };
106
+ * const errors = [
107
+ * { code: 'PATTERN', path: '#/user/profile/bio' },
108
+ * { code: 'INVALID_FORMAT', path: '#/user/profile/tags/0' },
109
+ * { code: 'ENUM_MISMATCH', path: '#/user/profile/tags/2' }
110
+ * ];
111
+ * const [result, remainingErrors] = nullifyEmptyValues(object, errors);
112
+ * // result: {
113
+ * // user: {
114
+ * // profile: {
115
+ * // bio: null,
116
+ * // tags: [null, 'tag1', null]
117
+ * // }
118
+ * // }
119
+ * // }
120
+ * // remainingErrors: []
121
+ * ```
122
+ *
123
+ * ```typescript
124
+ * // Example 4: Non-format errors are not processed
125
+ * const object = { field: '' };
126
+ * const formatError = { code: 'PATTERN', path: '#/field' };
127
+ * const typeError = { code: 'INVALID_TYPE', path: '#/field' };
128
+ * const [result, remainingErrors] = nullifyEmptyValues(
129
+ * object,
130
+ * [formatError, typeError]
131
+ * );
132
+ * // result: { field: null }
133
+ * // remainingErrors: [typeError] // type error remains
134
+ * ```
135
+ *
136
+ * ```typescript
137
+ * // Example 5: Non-empty values are preserved
138
+ * const object = { field: 'invalid-value' };
139
+ * const error = { code: 'PATTERN', path: '#/field' };
140
+ * const [result, remainingErrors] = nullifyEmptyValues(object, [error]);
141
+ * // result: { field: 'invalid-value' } // unchanged
142
+ * // remainingErrors: [error] // error remains
143
+ * ```
144
+ *
145
+ * @param object - The target object to process (will be deep cloned, not mutated)
146
+ * @param validationErrors - Array of schema validation errors from z-schema
147
+ * @returns A tuple containing:
148
+ * - `[0]`: Deep clone of the object with empty strings converted to null where applicable
149
+ * - `[1]`: Array of validation errors that couldn't be resolved (required fields,
150
+ * non-format errors, or errors for non-empty values)
151
+ */
152
+ const nullifyEmptyValues = (object, validationErrors) => {
153
+ // Create a deep clone to avoid mutating the original object
154
+ const result = JSON.parse(JSON.stringify(object));
155
+ const remainingErrors = [];
156
+ for (const error of validationErrors) {
157
+ const { code, path: pathString } = error;
158
+ const schema = (0, lodash_1.get)(error, z_schema_1.schemaSymbol);
159
+ const isAttributeRequired = schema?.['x-required'] === true;
160
+ const isFormatError = FORMAT_ERROR_CODES.includes(code);
161
+ const shouldSkipRequiredAttribute = isAttributeRequired;
162
+ const shouldSkipNonFormatError = !isFormatError;
163
+ // Skip required attributes - they should not be nullified
164
+ if (shouldSkipRequiredAttribute) {
165
+ remainingErrors.push(error);
166
+ continue;
167
+ }
168
+ // Only process format-related errors
169
+ if (shouldSkipNonFormatError) {
170
+ remainingErrors.push(error);
171
+ continue;
172
+ }
173
+ // Parse the JSON path (e.g., '#/user/profile/field' -> ['user', 'profile', 'field'])
174
+ const path = pathString.replace(/^#\//, '').split('/').filter(Boolean);
175
+ // Get the actual value from the error's JSON context
176
+ const json = (0, lodash_1.get)(error, z_schema_1.jsonSymbol);
177
+ const value = (0, lodash_1.get)(json, path);
178
+ const isEmptyValue = EMPTY_VALUES.includes(value);
179
+ const shouldSkipNonEmptyValue = !isEmptyValue;
180
+ // Only nullify if the value is actually empty
181
+ if (shouldSkipNonEmptyValue) {
182
+ remainingErrors.push(error);
183
+ continue;
184
+ }
185
+ // Set the value to null in the result object
186
+ (0, lodash_1.set)(result, path, null);
187
+ }
188
+ return [result, remainingErrors];
189
+ };
190
+ exports.default = nullifyEmptyValues;
191
+ //# sourceMappingURL=nullifyEmptyValues.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nullifyEmptyValues.js","sourceRoot":"","sources":["../../src/helpers/nullifyEmptyValues.ts"],"names":[],"mappings":";;AAAA,mCAAkC;AAClC,uCAA4E;AAI5E;;;GAGG;AACH,MAAM,kBAAkB,GAAG;IACzB,SAAS;IACT,eAAe;IACf,gBAAgB;CACR,CAAC;AAEX;;GAEG;AACH,MAAM,YAAY,GAAG,CAAC,EAAE,CAAU,CAAC;AAEnC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqIG;AACH,MAAM,kBAAkB,GAAG,CACzB,MAAoB,EACpB,gBAAqC,EACA,EAAE;IACvC,4DAA4D;IAC5D,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAiB,CAAC;IAClE,MAAM,eAAe,GAAwB,EAAE,CAAC;IAEhD,KAAK,MAAM,KAAK,IAAI,gBAAgB,EAAE,CAAC;QACrC,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,UAAU,EAAE,GAAG,KAAK,CAAC;QACzC,MAAM,MAAM,GAAG,IAAA,YAAG,EAAC,KAAK,EAAE,uBAAY,CAAwC,CAAC;QAC/E,MAAM,mBAAmB,GAAG,MAAM,EAAE,CAAC,YAAY,CAAC,KAAK,IAAI,CAAC;QAC5D,MAAM,aAAa,GAAG,kBAAkB,CAAC,QAAQ,CAAC,IAAyC,CAAC,CAAC;QAE7F,MAAM,2BAA2B,GAAG,mBAAmB,CAAC;QACxD,MAAM,wBAAwB,GAAG,CAAC,aAAa,CAAC;QAEhD,0DAA0D;QAC1D,IAAI,2BAA2B,EAAE,CAAC;YAChC,eAAe,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAC5B,SAAS;QACX,CAAC;QAED,qCAAqC;QACrC,IAAI,wBAAwB,EAAE,CAAC;YAC7B,eAAe,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAC5B,SAAS;QACX,CAAC;QAED,qFAAqF;QACrF,MAAM,IAAI,GAAG,UAAU,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAEvE,qDAAqD;QACrD,MAAM,IAAI,GAAG,IAAA,YAAG,EAAC,KAAK,EAAE,qBAAU,CAAiB,CAAC;QACpD,MAAM,KAAK,GAAG,IAAA,YAAG,EAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAE9B,MAAM,YAAY,GAAG,YAAY,CAAC,QAAQ,CAAC,KAAoC,CAAC,CAAC;QACjF,MAAM,uBAAuB,GAAG,CAAC,YAAY,CAAC;QAE9C,8CAA8C;QAC9C,IAAI,uBAAuB,EAAE,CAAC;YAC5B,eAAe,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAC5B,SAAS;QACX,CAAC;QAED,6CAA6C;QAC7C,IAAA,YAAG,EAAC,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;IAC1B,CAAC;IAED,OAAO,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;AACnC,CAAC,CAAC;AAEF,kBAAe,kBAAkB,CAAC"}
@@ -0,0 +1,106 @@
1
+ import type { EnumSchema, PropertySchema } from './JsonSchema';
2
+ /**
3
+ * Recursively removes `required` and `default` attributes from a JSON schema.
4
+ *
5
+ * **Intent:**
6
+ * This function is designed to transform JSON schemas by stripping out validation
7
+ * constraints (`required`) and default values (`default`) from all properties,
8
+ * including nested objects and array items. This is useful for creating "pure"
9
+ * or "update-friendly" versions of schemas where all fields become optional and
10
+ * no defaults are applied.
11
+ *
12
+ * **Use Cases:**
13
+ * 1. **Update Schemas**: Create schemas for partial updates where all fields are optional
14
+ * 2. **Schema Transformation**: Prepare schemas for contexts where required/default
15
+ * constraints are not needed (e.g., credential generation, API transformations)
16
+ * 3. **Schema Normalization**: Remove validation metadata before further processing
17
+ *
18
+ * **Behavior:**
19
+ * - Mutates the input schema in-place by deleting `required` and `default` properties
20
+ * - Recursively processes nested object properties
21
+ * - Recursively processes array items (including nested objects within arrays)
22
+ * - Preserves all other schema properties (type, description, pattern, etc.)
23
+ * - Returns an object containing only the `properties` field
24
+ *
25
+ * **Examples:**
26
+ *
27
+ * ```typescript
28
+ * // Example 1: Simple object schema
29
+ * const schema = {
30
+ * type: 'object',
31
+ * properties: {
32
+ * name: { type: 'string', required: true, default: 'John' },
33
+ * age: { type: 'number', required: false, default: 0 }
34
+ * }
35
+ * };
36
+ *
37
+ * const result = removeRequiredAndDefault(schema);
38
+ * // result.properties.name: { type: 'string' } (required and default removed)
39
+ * // result.properties.age: { type: 'number' } (required and default removed)
40
+ * ```
41
+ *
42
+ * ```typescript
43
+ * // Example 2: Nested objects
44
+ * const schema = {
45
+ * type: 'object',
46
+ * properties: {
47
+ * user: {
48
+ * type: 'object',
49
+ * required: true,
50
+ * default: {},
51
+ * properties: {
52
+ * name: { type: 'string', required: true, default: 'John' },
53
+ * address: {
54
+ * type: 'object',
55
+ * properties: {
56
+ * street: { type: 'string', required: true, default: '' }
57
+ * }
58
+ * }
59
+ * }
60
+ * }
61
+ * }
62
+ * };
63
+ *
64
+ * const result = removeRequiredAndDefault(schema);
65
+ * // All required and default attributes are removed at all nesting levels
66
+ * ```
67
+ *
68
+ * ```typescript
69
+ * // Example 3: Arrays with object items
70
+ * const schema = {
71
+ * type: 'object',
72
+ * properties: {
73
+ * users: {
74
+ * type: 'array',
75
+ * required: true,
76
+ * default: [],
77
+ * items: {
78
+ * type: 'object',
79
+ * properties: {
80
+ * name: { type: 'string', required: true, default: 'John' }
81
+ * }
82
+ * }
83
+ * }
84
+ * }
85
+ * };
86
+ *
87
+ * const result = removeRequiredAndDefault(schema);
88
+ * // Removes required/default from array property and nested object properties
89
+ * ```
90
+ *
91
+ * **Limitations:**
92
+ * - Only processes schemas with a `properties` field (ObjectPropertySchema)
93
+ * - EnumSchema is accepted as a parameter but not processed (will return empty properties)
94
+ * - The function mutates the input schema object
95
+ * - Array items that are primitives (string, number, etc.) are processed but
96
+ * their return values are discarded (this is intentional)
97
+ *
98
+ * @param jsonSchema - The JSON schema to process (must have a `properties` field)
99
+ * @returns An object containing only the `properties` field with all `required`
100
+ * and `default` attributes removed recursively
101
+ */
102
+ declare const removeRequiredAndDefault: (jsonSchema: PropertySchema | EnumSchema) => {
103
+ properties: import("./JsonSchema").PropertiesSchema;
104
+ };
105
+ export default removeRequiredAndDefault;
106
+ //# sourceMappingURL=removeRequiredAndDefault.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"removeRequiredAndDefault.d.ts","sourceRoot":"","sources":["../../src/helpers/removeRequiredAndDefault.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,UAAU,EACV,cAAc,EAGf,MAAM,cAAc,CAAC;AAEtB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmGG;AACH,QAAA,MAAM,wBAAwB,GAAI,YAAY,cAAc,GAAG,UAAU;;CAyCxE,CAAC;AAEF,eAAe,wBAAwB,CAAC"}
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Recursively removes `required` and `default` attributes from a JSON schema.
5
+ *
6
+ * **Intent:**
7
+ * This function is designed to transform JSON schemas by stripping out validation
8
+ * constraints (`required`) and default values (`default`) from all properties,
9
+ * including nested objects and array items. This is useful for creating "pure"
10
+ * or "update-friendly" versions of schemas where all fields become optional and
11
+ * no defaults are applied.
12
+ *
13
+ * **Use Cases:**
14
+ * 1. **Update Schemas**: Create schemas for partial updates where all fields are optional
15
+ * 2. **Schema Transformation**: Prepare schemas for contexts where required/default
16
+ * constraints are not needed (e.g., credential generation, API transformations)
17
+ * 3. **Schema Normalization**: Remove validation metadata before further processing
18
+ *
19
+ * **Behavior:**
20
+ * - Mutates the input schema in-place by deleting `required` and `default` properties
21
+ * - Recursively processes nested object properties
22
+ * - Recursively processes array items (including nested objects within arrays)
23
+ * - Preserves all other schema properties (type, description, pattern, etc.)
24
+ * - Returns an object containing only the `properties` field
25
+ *
26
+ * **Examples:**
27
+ *
28
+ * ```typescript
29
+ * // Example 1: Simple object schema
30
+ * const schema = {
31
+ * type: 'object',
32
+ * properties: {
33
+ * name: { type: 'string', required: true, default: 'John' },
34
+ * age: { type: 'number', required: false, default: 0 }
35
+ * }
36
+ * };
37
+ *
38
+ * const result = removeRequiredAndDefault(schema);
39
+ * // result.properties.name: { type: 'string' } (required and default removed)
40
+ * // result.properties.age: { type: 'number' } (required and default removed)
41
+ * ```
42
+ *
43
+ * ```typescript
44
+ * // Example 2: Nested objects
45
+ * const schema = {
46
+ * type: 'object',
47
+ * properties: {
48
+ * user: {
49
+ * type: 'object',
50
+ * required: true,
51
+ * default: {},
52
+ * properties: {
53
+ * name: { type: 'string', required: true, default: 'John' },
54
+ * address: {
55
+ * type: 'object',
56
+ * properties: {
57
+ * street: { type: 'string', required: true, default: '' }
58
+ * }
59
+ * }
60
+ * }
61
+ * }
62
+ * }
63
+ * };
64
+ *
65
+ * const result = removeRequiredAndDefault(schema);
66
+ * // All required and default attributes are removed at all nesting levels
67
+ * ```
68
+ *
69
+ * ```typescript
70
+ * // Example 3: Arrays with object items
71
+ * const schema = {
72
+ * type: 'object',
73
+ * properties: {
74
+ * users: {
75
+ * type: 'array',
76
+ * required: true,
77
+ * default: [],
78
+ * items: {
79
+ * type: 'object',
80
+ * properties: {
81
+ * name: { type: 'string', required: true, default: 'John' }
82
+ * }
83
+ * }
84
+ * }
85
+ * }
86
+ * };
87
+ *
88
+ * const result = removeRequiredAndDefault(schema);
89
+ * // Removes required/default from array property and nested object properties
90
+ * ```
91
+ *
92
+ * **Limitations:**
93
+ * - Only processes schemas with a `properties` field (ObjectPropertySchema)
94
+ * - EnumSchema is accepted as a parameter but not processed (will return empty properties)
95
+ * - The function mutates the input schema object
96
+ * - Array items that are primitives (string, number, etc.) are processed but
97
+ * their return values are discarded (this is intentional)
98
+ *
99
+ * @param jsonSchema - The JSON schema to process (must have a `properties` field)
100
+ * @returns An object containing only the `properties` field with all `required`
101
+ * and `default` attributes removed recursively
102
+ */
103
+ const removeRequiredAndDefault = (jsonSchema) => {
104
+ const { properties } = jsonSchema;
105
+ if (!properties) {
106
+ return { properties: {} };
107
+ }
108
+ for (const fieldName in properties) {
109
+ const property = properties[fieldName];
110
+ // Remove required and default from the property itself
111
+ delete property.required;
112
+ delete property.default;
113
+ // Check if property has a type to determine how to process it
114
+ const { type } = property;
115
+ const isObject = type === 'object';
116
+ const isArray = type === 'array';
117
+ if (isObject) {
118
+ // Recursively process nested object properties
119
+ removeRequiredAndDefault(property);
120
+ continue;
121
+ }
122
+ if (isArray) {
123
+ const { items } = property;
124
+ if (items) {
125
+ // Recursively process array items
126
+ // Note: For EnumSchema/ReferencePropertySchema items without properties,
127
+ // this will return { properties: {} } but the items themselves are not
128
+ // modified (only properties within objects are processed)
129
+ removeRequiredAndDefault(items);
130
+ }
131
+ }
132
+ // For other types (string, number, boolean, etc.) or properties without type,
133
+ // no further processing is needed
134
+ }
135
+ return { properties };
136
+ };
137
+ exports.default = removeRequiredAndDefault;
138
+ //# sourceMappingURL=removeRequiredAndDefault.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"removeRequiredAndDefault.js","sourceRoot":"","sources":["../../src/helpers/removeRequiredAndDefault.ts"],"names":[],"mappings":";;AAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmGG;AACH,MAAM,wBAAwB,GAAG,CAAC,UAAuC,EAAE,EAAE;IAC3E,MAAM,EAAE,UAAU,EAAE,GAAI,UAAmC,CAAC;IAE5D,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,OAAO,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC;IAC5B,CAAC;IAED,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;QACnC,MAAM,QAAQ,GAAG,UAAU,CAAC,SAAS,CAAC,CAAC;QAEvC,uDAAuD;QACvD,OAAO,QAAQ,CAAC,QAAQ,CAAC;QACzB,OAAO,QAAQ,CAAC,OAAO,CAAC;QAExB,8DAA8D;QAC9D,MAAM,EAAE,IAAI,EAAE,GAAI,QAAuD,CAAC;QAE1E,MAAM,QAAQ,GAAG,IAAI,KAAK,QAAQ,CAAC;QACnC,MAAM,OAAO,GAAG,IAAI,KAAK,OAAO,CAAC;QAEjC,IAAI,QAAQ,EAAE,CAAC;YACb,+CAA+C;YAC/C,wBAAwB,CAAC,QAAgC,CAAC,CAAC;YAC3D,SAAS;QACX,CAAC;QAED,IAAI,OAAO,EAAE,CAAC;YACZ,MAAM,EAAE,KAAK,EAAE,GAAI,QAAgC,CAAC;YACpD,IAAI,KAAK,EAAE,CAAC;gBACV,kCAAkC;gBAClC,yEAAyE;gBACzE,uEAAuE;gBACvE,0DAA0D;gBAC1D,wBAAwB,CAAC,KAAK,CAAC,CAAC;YAClC,CAAC;QACH,CAAC;QACD,8EAA8E;QAC9E,kCAAkC;IACpC,CAAC;IAED,OAAO,EAAE,UAAU,EAAE,CAAC;AACxB,CAAC,CAAC;AAEF,kBAAe,wBAAwB,CAAC"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Validates that a value is a URL or a DID (Decentralized Identifier).
3
+ * Throws if the value is missing, null, undefined, or not a valid URL/DID.
4
+ *
5
+ * **Intent**
6
+ * Enforce that identifier-style parameters (schema IDs, credential IDs, subject
7
+ * references, etc.) are either resolvable URLs or W3C DIDs before they are used
8
+ * in Linked Data or verification flows. This prevents malformed or arbitrary
9
+ * strings from propagating into storage, APIs, or cryptographic operations.
10
+ *
11
+ * **Use cases**
12
+ * - Validating `schemaId`, `credentialId`, or similar parameters in credential
13
+ * and schema factories before creating or resolving documents.
14
+ * - Input validation for APIs that accept URI identifiers (e.g. fetch by ID).
15
+ * - Guarding helpers that resolve or dereference IDs (e.g. document loaders,
16
+ * schema registries) from invalid input.
17
+ *
18
+ * **Examples**
19
+ *
20
+ * Valid — URLs:
21
+ * validateId('schemaId', 'https://example.com/schemas/v1');
22
+ * validateId('id', 'http://example.com:8080/path?q=1');
23
+ *
24
+ * Valid — DIDs (case-insensitive `did:` prefix):
25
+ * validateId('subject', 'did:example:123456789');
26
+ * validateId('subject', 'did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK');
27
+ *
28
+ * Throws — missing or invalid:
29
+ * validateId('id', ''); // Error: Parameter "id" is required
30
+ * validateId('id', null); // Error: Parameter "id" is required
31
+ * validateId('id', 'not-a-uri'); // Error: Parameter "id" must be a URL, received: "not-a-uri"
32
+ *
33
+ * @param name - Parameter name used in error messages (e.g. `"schemaId"`, `"credentialId"`).
34
+ * @param value - The value to validate (`string`, `null`, or `undefined`).
35
+ * @throws {Error} When `value` is falsy, or when it is not a URL and does not start with `did:`.
36
+ */
37
+ declare const validateId: (name: string, value: string | null | undefined) => void;
38
+ export default validateId;
39
+ //# sourceMappingURL=validateId.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validateId.d.ts","sourceRoot":"","sources":["../../src/helpers/validateId.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,QAAA,MAAM,UAAU,GAAI,MAAM,MAAM,EAAE,OAAO,MAAM,GAAG,IAAI,GAAG,SAAS,SAYjE,CAAC;AAEF,eAAe,UAAU,CAAC"}