|
1 |
| -import { getInterpolatedVariables, replaceTemplateFields } from './field.utils' |
| 1 | +import { JSONSchema7 } from 'json-schema' |
| 2 | +import { fixObjectTypes, getInterpolatedVariables, replaceTemplateFields } from './field.utils' |
2 | 3 |
|
3 | 4 | describe('FieldUtils', () => {
|
4 | 5 | describe('getInterpolatedVariables', () => {
|
@@ -125,4 +126,189 @@ describe('FieldUtils', () => {
|
125 | 126 | })
|
126 | 127 | })
|
127 | 128 | })
|
| 129 | + |
| 130 | + describe('fixObjectTypes', () => { |
| 131 | + const schema: JSONSchema7 = { |
| 132 | + type: 'object', |
| 133 | + properties: { |
| 134 | + name: { type: 'string' }, |
| 135 | + age: { type: 'integer' }, |
| 136 | + active: { type: 'boolean' }, |
| 137 | + score: { type: 'number' }, |
| 138 | + hobbies: { |
| 139 | + type: 'array', |
| 140 | + items: { type: 'string' }, |
| 141 | + }, |
| 142 | + address: { |
| 143 | + type: 'object', |
| 144 | + properties: { |
| 145 | + street: { type: 'string' }, |
| 146 | + city: { type: 'string' }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + friends: { |
| 150 | + type: 'array', |
| 151 | + items: { |
| 152 | + type: 'object', |
| 153 | + properties: { |
| 154 | + name: { type: 'string' }, |
| 155 | + age: { type: 'integer' }, |
| 156 | + active: { type: 'boolean' }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + }, |
| 160 | + }, |
| 161 | + } |
| 162 | + |
| 163 | + it('should fix object types according to schema', () => { |
| 164 | + const input = { |
| 165 | + name: 'John Doe', |
| 166 | + age: '30', |
| 167 | + active: 'true', |
| 168 | + score: '100.5', |
| 169 | + hobbies: ['reading', 'sports'], |
| 170 | + address: { |
| 171 | + street: '123 Main St', |
| 172 | + city: 'New York', |
| 173 | + }, |
| 174 | + } |
| 175 | + |
| 176 | + const expectedOutput = { |
| 177 | + name: 'John Doe', |
| 178 | + age: 30, |
| 179 | + active: true, |
| 180 | + score: 100.5, |
| 181 | + hobbies: ['reading', 'sports'], |
| 182 | + address: { |
| 183 | + street: '123 Main St', |
| 184 | + city: 'New York', |
| 185 | + }, |
| 186 | + } |
| 187 | + |
| 188 | + expect(fixObjectTypes(input, schema)).toEqual(expectedOutput) |
| 189 | + }) |
| 190 | + |
| 191 | + it('should not change the value of items not following the schema', () => { |
| 192 | + const input = { |
| 193 | + name: 'John Doe', |
| 194 | + age: '{{template.age}}', |
| 195 | + active: '{{template.active}}', |
| 196 | + score: '{{template.score}}', |
| 197 | + hobbies: '{{template.hobbies}}', |
| 198 | + address: '{{template.address}}', |
| 199 | + } |
| 200 | + |
| 201 | + const expectedOutput = { |
| 202 | + name: 'John Doe', |
| 203 | + age: '{{template.age}}', |
| 204 | + active: '{{template.active}}', |
| 205 | + score: '{{template.score}}', |
| 206 | + hobbies: '{{template.hobbies}}', |
| 207 | + address: '{{template.address}}', |
| 208 | + } |
| 209 | + |
| 210 | + expect(fixObjectTypes(input, schema)).toEqual(expectedOutput) |
| 211 | + }) |
| 212 | + |
| 213 | + it('should not modify values with the correct type', () => { |
| 214 | + const input = { |
| 215 | + name: 'Jane Doe', |
| 216 | + age: 28, |
| 217 | + active: false, |
| 218 | + hobbies: ['painting', 'music'], |
| 219 | + address: { |
| 220 | + street: '456 Elm St', |
| 221 | + city: 'Los Angeles', |
| 222 | + }, |
| 223 | + } |
| 224 | + |
| 225 | + expect(fixObjectTypes(input, schema)).toEqual(input) |
| 226 | + }) |
| 227 | + |
| 228 | + it('should handle nested objects and arrays', () => { |
| 229 | + const input = { |
| 230 | + name: 'John Doe', |
| 231 | + age: '30', |
| 232 | + active: 'true', |
| 233 | + hobbies: ['reading', 'sports'], |
| 234 | + address: { |
| 235 | + street: '123 Main St', |
| 236 | + city: 'New York', |
| 237 | + }, |
| 238 | + friends: [ |
| 239 | + { |
| 240 | + name: 'Alice', |
| 241 | + age: '35', |
| 242 | + active: 'false', |
| 243 | + }, |
| 244 | + { |
| 245 | + name: 'Bob', |
| 246 | + age: '32', |
| 247 | + active: 'true', |
| 248 | + }, |
| 249 | + ], |
| 250 | + } |
| 251 | + |
| 252 | + const expectedOutput = { |
| 253 | + name: 'John Doe', |
| 254 | + age: 30, |
| 255 | + active: true, |
| 256 | + hobbies: ['reading', 'sports'], |
| 257 | + address: { |
| 258 | + street: '123 Main St', |
| 259 | + city: 'New York', |
| 260 | + }, |
| 261 | + friends: [ |
| 262 | + { |
| 263 | + name: 'Alice', |
| 264 | + age: 35, |
| 265 | + active: false, |
| 266 | + }, |
| 267 | + { |
| 268 | + name: 'Bob', |
| 269 | + age: 32, |
| 270 | + active: true, |
| 271 | + }, |
| 272 | + ], |
| 273 | + } |
| 274 | + |
| 275 | + expect(fixObjectTypes(input, schema)).toEqual(expectedOutput) |
| 276 | + }) |
| 277 | + |
| 278 | + it('should handle empty objects and arrays', () => { |
| 279 | + const input = { |
| 280 | + name: 'John Doe', |
| 281 | + age: '30', |
| 282 | + active: 'true', |
| 283 | + hobbies: [], |
| 284 | + address: {}, |
| 285 | + } |
| 286 | + |
| 287 | + const expectedOutput = { |
| 288 | + name: 'John Doe', |
| 289 | + age: 30, |
| 290 | + active: true, |
| 291 | + hobbies: [], |
| 292 | + address: {}, |
| 293 | + } |
| 294 | + |
| 295 | + expect(fixObjectTypes(input, schema)).toEqual(expectedOutput) |
| 296 | + }) |
| 297 | + |
| 298 | + it('should handle missing properties', () => { |
| 299 | + const input = { |
| 300 | + name: 'John Doe', |
| 301 | + age: '30', |
| 302 | + active: 'true', |
| 303 | + } |
| 304 | + |
| 305 | + const expectedOutput = { |
| 306 | + name: 'John Doe', |
| 307 | + age: 30, |
| 308 | + active: true, |
| 309 | + } |
| 310 | + |
| 311 | + expect(fixObjectTypes(input, schema)).toEqual(expectedOutput) |
| 312 | + }) |
| 313 | + }) |
128 | 314 | })
|
0 commit comments