File tree 3 files changed +62
-0
lines changed
repository-json-schema/src
3 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -143,6 +143,10 @@ export function jsonToSchemaObject(
143
143
if ( matched ) {
144
144
result [ 'x-typescript-type' ] = matched [ 1 ] ;
145
145
}
146
+ const indexInfoMatched = result . description ?. match ( / \{ " i n d e x I n f o " .* $ / s) ;
147
+ if ( indexInfoMatched ) {
148
+ result [ 'x-index-info' ] = indexInfoMatched [ 1 ] ;
149
+ }
146
150
return result ;
147
151
}
148
152
Original file line number Diff line number Diff line change @@ -485,6 +485,26 @@ describe('build-schema', () => {
485
485
} ,
486
486
} ) ;
487
487
} ) ;
488
+ it ( 'adds index info in description' , ( ) => {
489
+ @model ( )
490
+ class TestModel {
491
+ @property ( {
492
+ type : 'string' ,
493
+ required : true ,
494
+ index : { unique : true } ,
495
+ jsonSchema : {
496
+ format : 'email' ,
497
+ maxLength : 50 ,
498
+ minLength : 5 ,
499
+ } ,
500
+ } )
501
+ email : string ;
502
+ }
503
+ const jsonSchema = modelToJsonSchema ( TestModel ) ;
504
+ expect ( jsonSchema . description ) . to . eql (
505
+ '{"indexInfo":{"email":{"unique":true}}}' ,
506
+ ) ;
507
+ } ) ;
488
508
489
509
context ( 'with custom type properties' , ( ) => {
490
510
it ( 'properly converts undecorated custom type properties' , ( ) => {
@@ -728,6 +748,7 @@ describe('build-schema', () => {
728
748
@property ( {
729
749
type : 'string' ,
730
750
required : true ,
751
+ index : { unique : true } ,
731
752
jsonSchema : {
732
753
format : 'email' ,
733
754
maxLength : 50 ,
Original file line number Diff line number Diff line change @@ -486,6 +486,43 @@ export function modelToJsonSchema<T extends object>(
486
486
continue ;
487
487
}
488
488
489
+ const index = meta . properties [ p ] . index ;
490
+ let indexInfo : { } = { } ;
491
+ if ( index && Object . keys ( index ) . length ) {
492
+ indexInfo = { [ p ] : index } ;
493
+ }
494
+ if ( indexInfo && Object . keys ( indexInfo ) . length ) {
495
+ if ( result . description === undefined ) result . description = '' ;
496
+ if ( result . description . includes ( 'indexInfo' ) ) {
497
+ const indexInfoMatched = result . description . match ( / \{ " i n d e x I n f o " .* $ / s) ;
498
+ if ( indexInfoMatched ) {
499
+ const { indexInfo : existingIndexInfo } = JSON . parse (
500
+ indexInfoMatched [ 0 ] ,
501
+ ) ;
502
+ existingIndexInfo [ Object . keys ( indexInfo ) [ 0 ] ] = {
503
+ ...indexInfo ,
504
+ } ;
505
+ result . description = result . description . replace (
506
+ / \{ " i n d e x I n f o " .* $ / s,
507
+ '' ,
508
+ ) ;
509
+ if ( result . description ) {
510
+ result . description =
511
+ result . description +
512
+ `, ${ JSON . stringify ( { indexInfo : existingIndexInfo } ) } ` ;
513
+ } else {
514
+ result . description = `${ JSON . stringify ( { indexInfo : existingIndexInfo } ) } ` ;
515
+ }
516
+ }
517
+ } else {
518
+ if ( result . description ) {
519
+ result . description =
520
+ result . description + `, ${ JSON . stringify ( { indexInfo} ) } ` ;
521
+ } else {
522
+ result . description = `${ JSON . stringify ( { indexInfo} ) } ` ;
523
+ }
524
+ }
525
+ }
489
526
if ( meta . properties [ p ] . type == null ) {
490
527
// Circular import of model classes can lead to this situation
491
528
throw new Error (
You can’t perform that action at this time.
0 commit comments