File tree 2 files changed +118
-5
lines changed
2 files changed +118
-5
lines changed Original file line number Diff line number Diff line change @@ -10,11 +10,22 @@ import { capitalize, writeFile } from "./utils";
10
10
const compileOptions : Partial < CompilerOptions > = { bannerComment : "" } ;
11
11
const defaultSchema = { type : "object" , additionalProperties : false } ;
12
12
13
- function addDefaultValueToSchema ( schema : any ) {
14
- return {
15
- ...schema ,
16
- additionalProperties : schema . additionalProperties || false ,
17
- } ;
13
+ export function addDefaultValueToSchema (
14
+ schema : Record < string , any >
15
+ ) : Record < string , any > {
16
+ return Object . entries ( schema ) . reduce ( ( acc , [ key , value ] ) => {
17
+ if ( value !== null && typeof value == "object" ) {
18
+ acc [ key ] = addDefaultValueToSchema ( value ) ;
19
+ } else {
20
+ acc [ key ] = value ;
21
+ }
22
+
23
+ if ( key === "properties" && ! acc . additionalProperties ) {
24
+ acc . additionalProperties = false ;
25
+ }
26
+
27
+ return acc ;
28
+ } , { } as any ) ;
18
29
}
19
30
20
31
export const defaultOptions = {
Original file line number Diff line number Diff line change 3
3
generateReplyInterfaces ,
4
4
generateInterfaces ,
5
5
defaultOptions ,
6
+ addDefaultValueToSchema ,
6
7
} from "../src/schema" ;
7
8
8
9
describe ( "#generateReplyInterfaces" , ( ) => {
@@ -344,3 +345,104 @@ describe("#generateInterfaces", () => {
344
345
) . toMatchSnapshot ( ) ;
345
346
} ) ;
346
347
} ) ;
348
+
349
+ describe ( "#addDefaultValueToSchema" , ( ) => {
350
+ it ( "should add default values to schema" , ( ) => {
351
+ const res = addDefaultValueToSchema ( {
352
+ properties : {
353
+ success : {
354
+ type : "string" ,
355
+ } ,
356
+ message : {
357
+ type : "string" ,
358
+ } ,
359
+ } ,
360
+ } ) ;
361
+
362
+ expect ( res ) . toEqual ( {
363
+ properties : {
364
+ success : {
365
+ type : "string" ,
366
+ } ,
367
+ message : {
368
+ type : "string" ,
369
+ } ,
370
+ } ,
371
+ additionalProperties : false ,
372
+ } ) ;
373
+ } ) ;
374
+
375
+ it ( "should add default values to nested schema" , ( ) => {
376
+ const res = addDefaultValueToSchema ( {
377
+ properties : {
378
+ success : {
379
+ type : "object" ,
380
+ properties : {
381
+ ok : {
382
+ type : "boolean" ,
383
+ } ,
384
+ } ,
385
+ } ,
386
+ message : {
387
+ type : "string" ,
388
+ } ,
389
+ } ,
390
+ } ) ;
391
+
392
+ expect ( res ) . toEqual ( {
393
+ properties : {
394
+ success : {
395
+ type : "object" ,
396
+ additionalProperties : false ,
397
+ properties : {
398
+ ok : {
399
+ type : "boolean" ,
400
+ } ,
401
+ } ,
402
+ } ,
403
+ message : {
404
+ type : "string" ,
405
+ } ,
406
+ } ,
407
+ additionalProperties : false ,
408
+ } ) ;
409
+ } ) ;
410
+
411
+ it ( "should not overide values if already present" , ( ) => {
412
+ const res = addDefaultValueToSchema ( {
413
+ properties : {
414
+ success : {
415
+ type : "object" ,
416
+ additionalProperties : true ,
417
+ properties : {
418
+ ok : {
419
+ type : "boolean" ,
420
+ } ,
421
+ } ,
422
+ } ,
423
+ message : {
424
+ type : "string" ,
425
+ } ,
426
+ } ,
427
+ additionalProperties : true ,
428
+ } ) ;
429
+
430
+ expect ( res ) . toEqual ( {
431
+ properties : {
432
+ success : {
433
+ type : "object" ,
434
+ additionalProperties : true ,
435
+ properties : {
436
+ ok : {
437
+ type : "boolean" ,
438
+ } ,
439
+ } ,
440
+ } ,
441
+ message : {
442
+ type : "string" ,
443
+ } ,
444
+ } ,
445
+ additionalProperties : true ,
446
+ } ) ;
447
+ } ) ;
448
+ } ) ;
You can’t perform that action at this time.
0 commit comments