@kravc/schema 2.8.0-alpha.7 → 2.8.0-alpha.8

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 (118) hide show
  1. package/dist/CredentialFactory.d.ts +5 -318
  2. package/dist/CredentialFactory.d.ts.map +1 -1
  3. package/dist/CredentialFactory.js +5 -317
  4. package/dist/CredentialFactory.js.map +1 -1
  5. package/dist/Schema.d.ts +15 -420
  6. package/dist/Schema.d.ts.map +1 -1
  7. package/dist/Schema.js +13 -384
  8. package/dist/Schema.js.map +1 -1
  9. package/dist/ValidationError.d.ts +3 -49
  10. package/dist/ValidationError.d.ts.map +1 -1
  11. package/dist/ValidationError.js +3 -48
  12. package/dist/ValidationError.js.map +1 -1
  13. package/dist/Validator.d.ts +6 -470
  14. package/dist/Validator.d.ts.map +1 -1
  15. package/dist/Validator.js +50 -478
  16. package/dist/Validator.js.map +1 -1
  17. package/dist/helpers/cleanupAttributes.d.ts +3 -32
  18. package/dist/helpers/cleanupAttributes.d.ts.map +1 -1
  19. package/dist/helpers/cleanupAttributes.js +1 -30
  20. package/dist/helpers/cleanupAttributes.js.map +1 -1
  21. package/dist/helpers/cleanupNulls.d.ts +1 -25
  22. package/dist/helpers/cleanupNulls.d.ts.map +1 -1
  23. package/dist/helpers/cleanupNulls.js +2 -61
  24. package/dist/helpers/cleanupNulls.js.map +1 -1
  25. package/dist/helpers/createSchemasMap.d.ts +2 -93
  26. package/dist/helpers/createSchemasMap.d.ts.map +1 -1
  27. package/dist/helpers/createSchemasMap.js +4 -162
  28. package/dist/helpers/createSchemasMap.js.map +1 -1
  29. package/dist/helpers/getReferenceIds.d.ts +1 -165
  30. package/dist/helpers/getReferenceIds.d.ts.map +1 -1
  31. package/dist/helpers/getReferenceIds.js +2 -166
  32. package/dist/helpers/getReferenceIds.js.map +1 -1
  33. package/dist/helpers/got.d.ts +1 -57
  34. package/dist/helpers/got.d.ts.map +1 -1
  35. package/dist/helpers/got.js +1 -57
  36. package/dist/helpers/got.js.map +1 -1
  37. package/dist/helpers/mapObjectProperties.d.ts +2 -144
  38. package/dist/helpers/mapObjectProperties.d.ts.map +1 -1
  39. package/dist/helpers/mapObjectProperties.js +1 -142
  40. package/dist/helpers/mapObjectProperties.js.map +1 -1
  41. package/dist/helpers/normalizeAttributes.d.ts +3 -211
  42. package/dist/helpers/normalizeAttributes.d.ts.map +1 -1
  43. package/dist/helpers/normalizeAttributes.js +1 -209
  44. package/dist/helpers/normalizeAttributes.js.map +1 -1
  45. package/dist/helpers/normalizeProperties.d.ts +1 -165
  46. package/dist/helpers/normalizeProperties.d.ts.map +1 -1
  47. package/dist/helpers/normalizeProperties.js +1 -164
  48. package/dist/helpers/normalizeProperties.js.map +1 -1
  49. package/dist/helpers/normalizeRequired.d.ts +1 -153
  50. package/dist/helpers/normalizeRequired.d.ts.map +1 -1
  51. package/dist/helpers/normalizeRequired.js +0 -151
  52. package/dist/helpers/normalizeRequired.js.map +1 -1
  53. package/dist/helpers/normalizeType.d.ts +1 -77
  54. package/dist/helpers/normalizeType.d.ts.map +1 -1
  55. package/dist/helpers/normalizeType.js +11 -139
  56. package/dist/helpers/normalizeType.js.map +1 -1
  57. package/dist/helpers/nullifyEmptyValues.d.ts +1 -135
  58. package/dist/helpers/nullifyEmptyValues.d.ts.map +1 -1
  59. package/dist/helpers/nullifyEmptyValues.js +13 -143
  60. package/dist/helpers/nullifyEmptyValues.js.map +1 -1
  61. package/dist/helpers/removeRequiredAndDefault.d.ts +2 -102
  62. package/dist/helpers/removeRequiredAndDefault.d.ts.map +1 -1
  63. package/dist/helpers/removeRequiredAndDefault.js +1 -100
  64. package/dist/helpers/removeRequiredAndDefault.js.map +1 -1
  65. package/dist/helpers/validateId.d.ts +1 -36
  66. package/dist/helpers/validateId.d.ts.map +1 -1
  67. package/dist/helpers/validateId.js +1 -36
  68. package/dist/helpers/validateId.js.map +1 -1
  69. package/dist/ld/documentLoader.d.ts +1 -1
  70. package/dist/ld/documentLoader.d.ts.map +1 -1
  71. package/dist/ld/documentLoader.js +1 -1
  72. package/dist/ld/documentLoader.js.map +1 -1
  73. package/dist/ld/getLinkedDataAttributeType.d.ts +1 -1
  74. package/dist/ld/getLinkedDataAttributeType.d.ts.map +1 -1
  75. package/dist/ld/getLinkedDataAttributeType.js +1 -1
  76. package/dist/ld/getLinkedDataAttributeType.js.map +1 -1
  77. package/dist/ld/getLinkedDataContext.d.ts +1 -1
  78. package/dist/ld/getLinkedDataContext.d.ts.map +1 -1
  79. package/dist/ld/getLinkedDataContext.js +1 -1
  80. package/dist/ld/getLinkedDataContext.js.map +1 -1
  81. package/package.json +2 -2
  82. package/src/CredentialFactory.ts +5 -318
  83. package/src/Schema.ts +17 -427
  84. package/src/ValidationError.ts +5 -52
  85. package/src/Validator.ts +19 -483
  86. package/src/__tests__/CredentialFactory.test.ts +1 -1
  87. package/src/__tests__/Schema.test.ts +3 -8
  88. package/src/__tests__/ValidationError.test.ts +4 -2
  89. package/src/__tests__/Validator.test.ts +21 -4
  90. package/src/helpers/__tests__/cleanupAttributes.test.ts +3 -1
  91. package/src/helpers/__tests__/createSchemasMap.test.ts +1 -1
  92. package/src/helpers/__tests__/mapObjectProperties.test.ts +2 -1
  93. package/src/helpers/__tests__/normalizeAttributes.test.ts +4 -2
  94. package/src/helpers/__tests__/normalizeProperties.test.ts +3 -1
  95. package/src/helpers/__tests__/normalizeRequired.test.ts +4 -9
  96. package/src/helpers/__tests__/nullifyEmptyValues.test.ts +43 -12
  97. package/src/helpers/__tests__/removeRequiredAndDefault.test.ts +3 -10
  98. package/src/helpers/cleanupAttributes.ts +6 -44
  99. package/src/helpers/cleanupNulls.ts +2 -63
  100. package/src/helpers/createSchemasMap.ts +4 -163
  101. package/src/helpers/getReferenceIds.ts +2 -173
  102. package/src/helpers/got.ts +1 -57
  103. package/src/helpers/mapObjectProperties.ts +4 -156
  104. package/src/helpers/normalizeAttributes.ts +7 -211
  105. package/src/helpers/normalizeProperties.ts +1 -172
  106. package/src/helpers/normalizeRequired.ts +1 -161
  107. package/src/helpers/normalizeType.ts +11 -139
  108. package/src/helpers/nullifyEmptyValues.ts +10 -145
  109. package/src/helpers/removeRequiredAndDefault.ts +1 -106
  110. package/src/helpers/validateId.ts +1 -36
  111. package/src/ld/documentLoader.ts +1 -1
  112. package/src/ld/getLinkedDataAttributeType.ts +1 -1
  113. package/src/ld/getLinkedDataContext.ts +1 -1
  114. package/src/{helpers/JsonSchema.ts → types.d.ts} +12 -16
  115. package/dist/helpers/JsonSchema.d.ts +0 -99
  116. package/dist/helpers/JsonSchema.d.ts.map +0 -1
  117. package/dist/helpers/JsonSchema.js +0 -3
  118. package/dist/helpers/JsonSchema.js.map +0 -1
@@ -1,213 +1,5 @@
1
- import type { TargetObject, JsonSchema, JsonSchemasMap } from './JsonSchema';
2
- /**
3
- * Normalizes object attribute values based on a JSON Schema definition.
4
- *
5
- * ## Intent
6
- *
7
- * This function ensures that object properties conform to their schema definitions by:
8
- * 1. Setting default values for properties that are undefined (but not null)
9
- * 2. Normalizing existing values to match their schema-defined types (e.g., converting
10
- * string "123" to number 123, or string "true" to boolean true)
11
- *
12
- * The function operates recursively, processing nested objects, arrays, and referenced
13
- * schemas ($ref) to ensure all properties throughout the object tree are normalized
14
- * according to their respective schema definitions.
15
- *
16
- * This is particularly useful in data processing pipelines where data may come from
17
- * external sources (forms, APIs, databases) with inconsistent types, but needs to be
18
- * normalized before validation or further processing.
19
- *
20
- * ## Use Cases
21
- *
22
- * 1. **Form data processing**: HTML forms submit all values as strings. This function
23
- * converts them to their expected types (numbers, booleans) based on schema definitions
24
- * and fills in default values for missing fields.
25
- *
26
- * 2. **API response normalization**: When consuming APIs that return loosely-typed data
27
- * (e.g., numbers as strings, booleans as strings), this function ensures values match
28
- * the expected schema types before validation or business logic processing.
29
- *
30
- * 3. **Configuration object initialization**: Setting default values and normalizing types
31
- * for configuration objects based on their schema definitions, ensuring consistent
32
- * structure and types throughout the application.
33
- *
34
- * 4. **Data migration and transformation**: Normalizing data structures during migration
35
- * or transformation processes where source data may have inconsistent types but target
36
- * schema requires specific types.
37
- *
38
- * 5. **Pre-validation normalization**: Preparing objects for schema validation by ensuring
39
- * types are correct and defaults are applied, reducing validation errors and improving
40
- * data quality.
41
- *
42
- * ## Behavior
43
- *
44
- * - **Default values**: Properties that are `undefined` will be set to their schema-defined
45
- * default value (if one exists). Properties that are `null` are left as `null` and will
46
- * not receive default values. Default values are also normalized according to their type
47
- * (e.g., a default string "123" with type "number" will be converted to the number 123).
48
- *
49
- * - **Type normalization**: Properties with existing values (including default values that
50
- * were just set) are normalized to match their schema type using `normalizeType`. This
51
- * includes converting strings to numbers/booleans where appropriate, while preserving the
52
- * original value if conversion is not possible.
53
- *
54
- * - **Recursive processing**: The function processes nested objects, arrays, and schema
55
- * references ($ref) recursively, ensuring all nested properties are normalized.
56
- *
57
- * - **Non-destructive**: The function mutates the input object in place. If you need to
58
- * preserve the original, create a deep copy before calling this function.
59
- *
60
- * ## Examples
61
- *
62
- * ### Basic Usage: Default Values and Type Normalization
63
- * ```typescript
64
- * import Schema from './Schema';
65
- * import normalizeAttributes from './normalizeAttributes';
66
- *
67
- * const schema = new Schema({
68
- * name: { type: 'string', default: 'Anonymous' },
69
- * age: { type: 'number' },
70
- * isActive: { type: 'boolean', default: false }
71
- * }, 'user-schema');
72
- *
73
- * const user = {
74
- * age: '25' // string that should be a number
75
- * };
76
- *
77
- * normalizeAttributes(user, schema.jsonSchema, {});
78
- *
79
- * // Result:
80
- * // {
81
- * // name: 'Anonymous', // default value applied
82
- * // age: 25, // string converted to number
83
- * // isActive: false // default value applied
84
- * // }
85
- * ```
86
- *
87
- * ### Nested Objects
88
- * ```typescript
89
- * const schema = new Schema({
90
- * address: {
91
- * type: 'object',
92
- * properties: {
93
- * street: { type: 'string', default: 'Unknown' },
94
- * zipCode: { type: 'number' }
95
- * }
96
- * }
97
- * }, 'profile-schema');
98
- *
99
- * const profile = {
100
- * address: {
101
- * zipCode: '12345' // string that should be a number
102
- * }
103
- * };
104
- *
105
- * normalizeAttributes(profile, schema.jsonSchema, {});
106
- *
107
- * // Result:
108
- * // {
109
- * // address: {
110
- * // street: 'Unknown', // default value applied
111
- * // zipCode: 12345 // string converted to number
112
- * // }
113
- * // }
114
- * ```
115
- *
116
- * ### Arrays with Schema References
117
- * ```typescript
118
- * const itemSchema = new Schema({
119
- * id: { type: 'number' },
120
- * name: { type: 'string', default: 'Unnamed' }
121
- * }, 'item-schema');
122
- *
123
- * const schema = new Schema({
124
- * items: {
125
- * type: 'array',
126
- * items: { $ref: 'item-schema' }
127
- * }
128
- * }, 'collection-schema');
129
- *
130
- * const collection = {
131
- * items: [
132
- * { id: '1' }, // id is a string, should be number
133
- * { id: '2', name: 'Item 2' }
134
- * ]
135
- * };
136
- *
137
- * const schemasMap = {
138
- * 'item-schema': itemSchema.jsonSchema
139
- * };
140
- *
141
- * normalizeAttributes(collection, schema.jsonSchema, schemasMap);
142
- *
143
- * // Result:
144
- * // {
145
- * // items: [
146
- * // { id: 1, name: 'Unnamed' }, // id normalized, default name applied
147
- * // { id: 2, name: 'Item 2' } // id normalized, existing name preserved
148
- * // ]
149
- * // }
150
- * ```
151
- *
152
- * ### Boolean Normalization
153
- * ```typescript
154
- * const schema = new Schema({
155
- * enabled: { type: 'boolean', default: false },
156
- * verified: { type: 'boolean' }
157
- * }, 'settings-schema');
158
- *
159
- * const settings = {
160
- * verified: 'yes' // string that should be boolean
161
- * };
162
- *
163
- * normalizeAttributes(settings, schema.jsonSchema, {});
164
- *
165
- * // Result:
166
- * // {
167
- * // enabled: false, // default value applied
168
- * // verified: true // string "yes" converted to boolean true
169
- * // }
170
- * ```
171
- *
172
- * ### Handling Null Values
173
- * ```typescript
174
- * const schema = new Schema({
175
- * optionalField: { type: 'string', default: 'default-value' }
176
- * }, 'test-schema');
177
- *
178
- * const obj1 = {}; // undefined → default applied
179
- * const obj2 = { optionalField: null }; // null → no default applied
180
- *
181
- * normalizeAttributes(obj1, schema.jsonSchema, {});
182
- * normalizeAttributes(obj2, schema.jsonSchema, {});
183
- *
184
- * // obj1: { optionalField: 'default-value' }
185
- * // obj2: { optionalField: null } // null preserved, default not applied
186
- * ```
187
- *
188
- * ### Default Value Normalization
189
- * ```typescript
190
- * const schema = new Schema({
191
- * count: { type: 'number', default: '42' }, // default is string, type is number
192
- * enabled: { type: 'boolean', default: 'true' } // default is string, type is boolean
193
- * }, 'config-schema');
194
- *
195
- * const config = {};
196
- *
197
- * normalizeAttributes(config, schema.jsonSchema, {});
198
- *
199
- * // Result:
200
- * // {
201
- * // count: 42, // default string "42" normalized to number
202
- * // enabled: true // default string "true" normalized to boolean
203
- * // }
204
- * ```
205
- *
206
- * @param object - The target object to normalize (mutated in place)
207
- * @param jsonSchema - The JSON Schema definition describing the object structure
208
- * @param jsonSchemasMap - Map of schema IDs to schema definitions, used for resolving $ref references
209
- * @returns void (mutates the input object)
210
- */
211
- declare const normalizeAttributes: (object: TargetObject, jsonSchema: JsonSchema, jsonSchemasMap: JsonSchemasMap) => void;
1
+ import { type JsonSchema } from 'z-schema';
2
+ /** Normalizes object attribute values based on a JSON Schema definition. */
3
+ declare const normalizeAttributes: (object: TargetObject, jsonSchema: JsonSchema, jsonSchemasMap: Record<string, JsonSchema>) => void;
212
4
  export default normalizeAttributes;
213
5
  //# sourceMappingURL=normalizeAttributes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"normalizeAttributes.d.ts","sourceRoot":"","sources":["../../src/helpers/normalizeAttributes.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,cAAc,EAAkB,MAAM,cAAc,CAAC;AAE7F;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgNG;AACH,QAAA,MAAM,mBAAmB,GAAI,QAAQ,YAAY,EAAE,YAAY,UAAU,EAAE,gBAAgB,cAAc,SA6BxG,CAAC;AAEF,eAAe,mBAAmB,CAAC"}
1
+ {"version":3,"file":"normalizeAttributes.d.ts","sourceRoot":"","sources":["../../src/helpers/normalizeAttributes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,UAAU,CAAC;AAM3C,4EAA4E;AAC5E,QAAA,MAAM,mBAAmB,GACvB,QAAQ,YAAY,EACpB,YAAY,UAAU,EACtB,gBAAgB,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,SA8B3C,CAAC;AAEF,eAAe,mBAAmB,CAAC"}
@@ -6,215 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const lodash_1 = require("lodash");
7
7
  const normalizeType_1 = __importDefault(require("./normalizeType"));
8
8
  const mapObjectProperties_1 = __importDefault(require("./mapObjectProperties"));
9
- /**
10
- * Normalizes object attribute values based on a JSON Schema definition.
11
- *
12
- * ## Intent
13
- *
14
- * This function ensures that object properties conform to their schema definitions by:
15
- * 1. Setting default values for properties that are undefined (but not null)
16
- * 2. Normalizing existing values to match their schema-defined types (e.g., converting
17
- * string "123" to number 123, or string "true" to boolean true)
18
- *
19
- * The function operates recursively, processing nested objects, arrays, and referenced
20
- * schemas ($ref) to ensure all properties throughout the object tree are normalized
21
- * according to their respective schema definitions.
22
- *
23
- * This is particularly useful in data processing pipelines where data may come from
24
- * external sources (forms, APIs, databases) with inconsistent types, but needs to be
25
- * normalized before validation or further processing.
26
- *
27
- * ## Use Cases
28
- *
29
- * 1. **Form data processing**: HTML forms submit all values as strings. This function
30
- * converts them to their expected types (numbers, booleans) based on schema definitions
31
- * and fills in default values for missing fields.
32
- *
33
- * 2. **API response normalization**: When consuming APIs that return loosely-typed data
34
- * (e.g., numbers as strings, booleans as strings), this function ensures values match
35
- * the expected schema types before validation or business logic processing.
36
- *
37
- * 3. **Configuration object initialization**: Setting default values and normalizing types
38
- * for configuration objects based on their schema definitions, ensuring consistent
39
- * structure and types throughout the application.
40
- *
41
- * 4. **Data migration and transformation**: Normalizing data structures during migration
42
- * or transformation processes where source data may have inconsistent types but target
43
- * schema requires specific types.
44
- *
45
- * 5. **Pre-validation normalization**: Preparing objects for schema validation by ensuring
46
- * types are correct and defaults are applied, reducing validation errors and improving
47
- * data quality.
48
- *
49
- * ## Behavior
50
- *
51
- * - **Default values**: Properties that are `undefined` will be set to their schema-defined
52
- * default value (if one exists). Properties that are `null` are left as `null` and will
53
- * not receive default values. Default values are also normalized according to their type
54
- * (e.g., a default string "123" with type "number" will be converted to the number 123).
55
- *
56
- * - **Type normalization**: Properties with existing values (including default values that
57
- * were just set) are normalized to match their schema type using `normalizeType`. This
58
- * includes converting strings to numbers/booleans where appropriate, while preserving the
59
- * original value if conversion is not possible.
60
- *
61
- * - **Recursive processing**: The function processes nested objects, arrays, and schema
62
- * references ($ref) recursively, ensuring all nested properties are normalized.
63
- *
64
- * - **Non-destructive**: The function mutates the input object in place. If you need to
65
- * preserve the original, create a deep copy before calling this function.
66
- *
67
- * ## Examples
68
- *
69
- * ### Basic Usage: Default Values and Type Normalization
70
- * ```typescript
71
- * import Schema from './Schema';
72
- * import normalizeAttributes from './normalizeAttributes';
73
- *
74
- * const schema = new Schema({
75
- * name: { type: 'string', default: 'Anonymous' },
76
- * age: { type: 'number' },
77
- * isActive: { type: 'boolean', default: false }
78
- * }, 'user-schema');
79
- *
80
- * const user = {
81
- * age: '25' // string that should be a number
82
- * };
83
- *
84
- * normalizeAttributes(user, schema.jsonSchema, {});
85
- *
86
- * // Result:
87
- * // {
88
- * // name: 'Anonymous', // default value applied
89
- * // age: 25, // string converted to number
90
- * // isActive: false // default value applied
91
- * // }
92
- * ```
93
- *
94
- * ### Nested Objects
95
- * ```typescript
96
- * const schema = new Schema({
97
- * address: {
98
- * type: 'object',
99
- * properties: {
100
- * street: { type: 'string', default: 'Unknown' },
101
- * zipCode: { type: 'number' }
102
- * }
103
- * }
104
- * }, 'profile-schema');
105
- *
106
- * const profile = {
107
- * address: {
108
- * zipCode: '12345' // string that should be a number
109
- * }
110
- * };
111
- *
112
- * normalizeAttributes(profile, schema.jsonSchema, {});
113
- *
114
- * // Result:
115
- * // {
116
- * // address: {
117
- * // street: 'Unknown', // default value applied
118
- * // zipCode: 12345 // string converted to number
119
- * // }
120
- * // }
121
- * ```
122
- *
123
- * ### Arrays with Schema References
124
- * ```typescript
125
- * const itemSchema = new Schema({
126
- * id: { type: 'number' },
127
- * name: { type: 'string', default: 'Unnamed' }
128
- * }, 'item-schema');
129
- *
130
- * const schema = new Schema({
131
- * items: {
132
- * type: 'array',
133
- * items: { $ref: 'item-schema' }
134
- * }
135
- * }, 'collection-schema');
136
- *
137
- * const collection = {
138
- * items: [
139
- * { id: '1' }, // id is a string, should be number
140
- * { id: '2', name: 'Item 2' }
141
- * ]
142
- * };
143
- *
144
- * const schemasMap = {
145
- * 'item-schema': itemSchema.jsonSchema
146
- * };
147
- *
148
- * normalizeAttributes(collection, schema.jsonSchema, schemasMap);
149
- *
150
- * // Result:
151
- * // {
152
- * // items: [
153
- * // { id: 1, name: 'Unnamed' }, // id normalized, default name applied
154
- * // { id: 2, name: 'Item 2' } // id normalized, existing name preserved
155
- * // ]
156
- * // }
157
- * ```
158
- *
159
- * ### Boolean Normalization
160
- * ```typescript
161
- * const schema = new Schema({
162
- * enabled: { type: 'boolean', default: false },
163
- * verified: { type: 'boolean' }
164
- * }, 'settings-schema');
165
- *
166
- * const settings = {
167
- * verified: 'yes' // string that should be boolean
168
- * };
169
- *
170
- * normalizeAttributes(settings, schema.jsonSchema, {});
171
- *
172
- * // Result:
173
- * // {
174
- * // enabled: false, // default value applied
175
- * // verified: true // string "yes" converted to boolean true
176
- * // }
177
- * ```
178
- *
179
- * ### Handling Null Values
180
- * ```typescript
181
- * const schema = new Schema({
182
- * optionalField: { type: 'string', default: 'default-value' }
183
- * }, 'test-schema');
184
- *
185
- * const obj1 = {}; // undefined → default applied
186
- * const obj2 = { optionalField: null }; // null → no default applied
187
- *
188
- * normalizeAttributes(obj1, schema.jsonSchema, {});
189
- * normalizeAttributes(obj2, schema.jsonSchema, {});
190
- *
191
- * // obj1: { optionalField: 'default-value' }
192
- * // obj2: { optionalField: null } // null preserved, default not applied
193
- * ```
194
- *
195
- * ### Default Value Normalization
196
- * ```typescript
197
- * const schema = new Schema({
198
- * count: { type: 'number', default: '42' }, // default is string, type is number
199
- * enabled: { type: 'boolean', default: 'true' } // default is string, type is boolean
200
- * }, 'config-schema');
201
- *
202
- * const config = {};
203
- *
204
- * normalizeAttributes(config, schema.jsonSchema, {});
205
- *
206
- * // Result:
207
- * // {
208
- * // count: 42, // default string "42" normalized to number
209
- * // enabled: true // default string "true" normalized to boolean
210
- * // }
211
- * ```
212
- *
213
- * @param object - The target object to normalize (mutated in place)
214
- * @param jsonSchema - The JSON Schema definition describing the object structure
215
- * @param jsonSchemasMap - Map of schema IDs to schema definitions, used for resolving $ref references
216
- * @returns void (mutates the input object)
217
- */
9
+ /** Normalizes object attribute values based on a JSON Schema definition. */
218
10
  const normalizeAttributes = (object, jsonSchema, jsonSchemasMap) => {
219
11
  /** Callback to normalize value based on property type defined in schema */
220
12
  const callback = (propertyName, propertySchema, object) => {
@@ -1 +1 @@
1
- {"version":3,"file":"normalizeAttributes.js","sourceRoot":"","sources":["../../src/helpers/normalizeAttributes.ts"],"names":[],"mappings":";;;;;AAAA,mCAA0C;AAE1C,oEAA4C;AAC5C,gFAAwD;AAGxD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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,EAAE,cAAc,EAAE,QAAQ,CAAC,CAAC;AACpE,CAAC,CAAC;AAEF,kBAAe,mBAAmB,CAAC"}
@@ -1,168 +1,4 @@
1
- import type { EnumSchema, PropertiesSchema } from './JsonSchema';
2
- /**
3
- * Normalizes JSON schema properties by ensuring all properties have an explicit `type` attribute.
4
- *
5
- * **Intent:**
6
- * This function transforms schemas that may have implicit or missing type information into
7
- * fully normalized schemas with explicit types. It recursively processes nested structures
8
- * (objects and arrays) to ensure type consistency throughout the schema hierarchy.
9
- *
10
- * **Use Cases:**
11
- * - **Schema Validation Preparation**: Ensures schemas are ready for validation where type
12
- * information is required by validators or processing tools
13
- * - **External Schema Normalization**: Normalizes schemas imported from external sources
14
- * (e.g., OpenAPI specs, YAML schemas) that may omit type information
15
- * - **Type Inference**: Automatically infers types from structural hints (e.g., presence of
16
- * `properties` implies `object`, presence of `items` implies `array`)
17
- * - **Schema Consistency**: Guarantees consistent schema structure before further processing
18
- * or transformation
19
- * - **Default Type Assignment**: Provides sensible defaults (e.g., `string` for primitives)
20
- * when type cannot be inferred
21
- *
22
- * **Behavior:**
23
- * - **Enum Schemas**: Sets `type` to `'string'` if missing (preserves existing type if present)
24
- * - **Reference Properties**: Skips `$ref` properties (they don't need type normalization)
25
- * - **Type Inference Priority** (when type is missing):
26
- * 1. If property has `properties` → sets `type: 'object'`
27
- * 2. Else if property has `items` → sets `type: 'array'`
28
- * 3. Else → sets `type: 'string'` (default)
29
- * - **Object Properties**:
30
- * - Infers `type: 'object'` from presence of `properties` (if type not already set)
31
- * - Creates empty `properties: {}` if `type: 'object'` but no properties exist
32
- * - Recursively normalizes nested object properties
33
- * - Note: If both `properties` and `items` exist, `properties` takes precedence
34
- * - **Array Properties**:
35
- * - Infers `type: 'array'` from presence of `items` (if type not already set)
36
- * - Sets default `items: { type: 'string' }` if array has no items
37
- * - Normalizes item schemas: sets `type: 'object'` if items have `properties` (not undefined)
38
- * - `properties: null` → treated as having properties (sets type to 'object')
39
- * - `properties: undefined` → treated as not having properties (no type set)
40
- * - Empty object `{}` → treated as not having properties (no type set)
41
- * - Recursively normalizes nested properties within array items
42
- * - **Primitive Properties**: Sets `type: 'string'` as default when no type, items, or properties exist
43
- * - **Existing Types**: Never overrides existing type values (even if structure suggests different type)
44
- * - **Edge Cases**:
45
- * - Empty schemas (`{}`) are handled gracefully
46
- * - Properties with conflicting structure (e.g., `type: 'object'` with `items`) are normalized
47
- * according to the explicit type, ignoring conflicting structural hints
48
- *
49
- * **Examples:**
50
- *
51
- * @example Enum schema normalization
52
- * ```typescript
53
- * const schema: EnumSchema = { enum: ['red', 'green', 'blue'] };
54
- * normalizeProperties(schema);
55
- * // Result: { enum: ['red', 'green', 'blue'], type: 'string' }
56
- * ```
57
- *
58
- * @example Object type inference
59
- * ```typescript
60
- * const schema: PropertiesSchema = {
61
- * user: {
62
- * properties: {
63
- * name: {}
64
- * }
65
- * }
66
- * };
67
- * normalizeProperties(schema);
68
- * // Result:
69
- * // {
70
- * // user: {
71
- * // type: 'object',
72
- * // properties: {
73
- * // name: { type: 'string' }
74
- * // }
75
- * // }
76
- * // }
77
- * ```
78
- *
79
- * @example Array type inference
80
- * ```typescript
81
- * const schema: PropertiesSchema = {
82
- * tags: {
83
- * items: { type: 'string' }
84
- * }
85
- * };
86
- * normalizeProperties(schema);
87
- * // Result:
88
- * // {
89
- * // tags: {
90
- * // type: 'array',
91
- * // items: { type: 'string' }
92
- * // }
93
- * // }
94
- * ```
95
- *
96
- * @example Complex nested structure
97
- * ```typescript
98
- * const schema: PropertiesSchema = {
99
- * profile: {
100
- * properties: {
101
- * name: {},
102
- * addresses: {
103
- * items: {
104
- * properties: {
105
- * street: {},
106
- * city: {}
107
- * }
108
- * }
109
- * }
110
- * }
111
- * }
112
- * };
113
- * normalizeProperties(schema);
114
- * // Result:
115
- * // {
116
- * // profile: {
117
- * // type: 'object',
118
- * // properties: {
119
- * // name: { type: 'string' },
120
- * // addresses: {
121
- * // type: 'array',
122
- * // items: {
123
- * // type: 'object',
124
- * // properties: {
125
- * // street: { type: 'string' },
126
- * // city: { type: 'string' }
127
- * // }
128
- * // }
129
- * // }
130
- * // }
131
- * // }
132
- * // }
133
- * ```
134
- *
135
- * @example Reference properties are skipped
136
- * ```typescript
137
- * const schema: PropertiesSchema = {
138
- * refField: { $ref: '#/definitions/User' },
139
- * normalField: {}
140
- * };
141
- * normalizeProperties(schema);
142
- * // Result:
143
- * // {
144
- * // refField: { $ref: '#/definitions/User' }, // Unchanged
145
- * // normalField: { type: 'string' }
146
- * // }
147
- * ```
148
- *
149
- * @example Default type assignment
150
- * ```typescript
151
- * const schema: PropertiesSchema = {
152
- * title: {},
153
- * count: { type: 'number' }
154
- * };
155
- * normalizeProperties(schema);
156
- * // Result:
157
- * // {
158
- * // title: { type: 'string' }, // Default assigned
159
- * // count: { type: 'number' } // Existing preserved
160
- * // }
161
- * ```
162
- *
163
- * @param schema - The schema to normalize (either an EnumSchema or PropertiesSchema)
164
- * @modifies The schema object is mutated in place with normalized types
165
- */
1
+ /** Normalizes JSON schema properties by ensuring all properties have an explicit `type` attribute. */
166
2
  declare const normalizeProperties: (schema: EnumSchema | PropertiesSchema) => void;
167
3
  export default normalizeProperties;
168
4
  //# sourceMappingURL=normalizeProperties.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"normalizeProperties.d.ts","sourceRoot":"","sources":["../../src/helpers/normalizeProperties.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,UAAU,EACV,gBAAgB,EAIjB,MAAM,cAAc,CAAC;AAEtB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmKG;AACH,QAAA,MAAM,mBAAmB,GAAI,QAAQ,UAAU,GAAG,gBAAgB,SAwEjE,CAAC;AAEF,eAAe,mBAAmB,CAAC"}
1
+ {"version":3,"file":"normalizeProperties.d.ts","sourceRoot":"","sources":["../../src/helpers/normalizeProperties.ts"],"names":[],"mappings":"AAEA,sGAAsG;AACtG,QAAA,MAAM,mBAAmB,GAAI,QAAQ,UAAU,GAAG,gBAAgB,SAwEjE,CAAC;AAEF,eAAe,mBAAmB,CAAC"}