-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #142 from Pigrabbit/feature/fix-read-many-swagger-…
…query Feature/fix read many swagger query
- Loading branch information
Showing
4 changed files
with
62 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,40 @@ | ||
/* eslint-disable max-classes-per-file */ | ||
import { mixin } from '@nestjs/common'; | ||
import { PickType } from '@nestjs/swagger'; | ||
import { MetadataStorage, getMetadataStorage } from 'class-validator'; | ||
import { getMetadataStorage, MetadataStorage } from 'class-validator'; | ||
import { ValidationMetadata } from 'class-validator/types/metadata/ValidationMetadata'; | ||
import { BaseEntity } from 'typeorm'; | ||
|
||
import { Method } from '../interface'; | ||
import { capitalizeFirstLetter } from '../util'; | ||
|
||
export function CreateRequestDto(parentClass: typeof BaseEntity, group: Method) { | ||
const propertyNamesAppliedValidation = getPropertyNamesFromMetadata(parentClass, group); | ||
|
||
class PickClass extends PickType(parentClass, propertyNamesAppliedValidation as Array<keyof BaseEntity>) {} | ||
const requestDto = mixin(PickClass); | ||
Object.defineProperty(requestDto, 'name', { | ||
value: `${capitalizeFirstLetter(group)}${parentClass.name}Dto`, | ||
}); | ||
|
||
return requestDto; | ||
} | ||
|
||
export function getPropertyNamesFromMetadata(parentClass: typeof BaseEntity, group: Method): string[] { | ||
const metadataStorage: MetadataStorage = getMetadataStorage(); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const getTargetValidationMetadatasArgs = [parentClass, null!, false, false]; | ||
const targetMetadata: ReturnType<typeof metadataStorage.getTargetValidationMetadatas> = ( | ||
metadataStorage.getTargetValidationMetadatas as (...args: unknown[]) => ValidationMetadata[] | ||
)(...getTargetValidationMetadatasArgs); | ||
|
||
const propertyNamesAppliedValidation = [ | ||
...new Set( | ||
targetMetadata | ||
.filter(({ groups, always }) => always === true || (groups ?? []).includes(group)) | ||
.map(({ propertyName }) => propertyName), | ||
), | ||
]; | ||
|
||
class PickClass extends PickType(parentClass, propertyNamesAppliedValidation as Array<keyof BaseEntity>) {} | ||
const requestDto = mixin(PickClass); | ||
Object.defineProperty(requestDto, 'name', { | ||
value: `${capitalizeFirstLetter(group)}${parentClass.name}Dto`, | ||
}); | ||
|
||
return requestDto; | ||
return propertyNamesAppliedValidation; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters