File tree Expand file tree Collapse file tree 3 files changed +63
-0
lines changed
repository-json-schema/src Expand file tree Collapse file tree 3 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -143,6 +143,11 @@ export function jsonToSchemaObject(
143143 if ( matched ) {
144144 result [ 'x-typescript-type' ] = matched [ 1 ] ;
145145 }
146+
147+ const indexInfoMatched = result . description ?. match ( / \{ " i n d e x I n f o " .* $ / s) ;
148+ if ( indexInfoMatched ) {
149+ result [ 'x-index-info' ] = indexInfoMatched [ 1 ] ;
150+ }
146151 return result ;
147152}
148153
Original file line number Diff line number Diff line change @@ -485,6 +485,26 @@ describe('build-schema', () => {
485485 } ,
486486 } ) ;
487487 } ) ;
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+ } ) ;
488508
489509 context ( 'with custom type properties' , ( ) => {
490510 it ( 'properly converts undecorated custom type properties' , ( ) => {
@@ -728,6 +748,7 @@ describe('build-schema', () => {
728748 @property ( {
729749 type : 'string' ,
730750 required : true ,
751+ index : { unique : true } ,
731752 jsonSchema : {
732753 format : 'email' ,
733754 maxLength : 50 ,
Original file line number Diff line number Diff line change @@ -486,6 +486,43 @@ export function modelToJsonSchema<T extends object>(
486486 continue ;
487487 }
488488
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+ }
489526 if ( meta . properties [ p ] . type == null ) {
490527 // Circular import of model classes can lead to this situation
491528 throw new Error (
You can’t perform that action at this time.
0 commit comments