Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added function type for enum #2852

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/types/swagger-enum.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export type SwaggerEnumType =
| string[]
| number[]
| (string | number)[]
| Record<number, string>;
| Record<number, string>
| (() => (string | number)[] | Record<string, any>);
17 changes: 16 additions & 1 deletion test/services/schema-object-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ describe('SchemaObjectFactory', () => {
Third = 3
}

const getAvailableUserRoles = () => ['admin', 'user'];

class CreatePersonDto {
@ApiProperty()
name: string;
Expand All @@ -57,6 +59,13 @@ describe('SchemaObjectFactory', () => {

@ApiProperty({ enum: Ranking, enumName: 'Ranking', isArray: true })
rankings: Ranking[];

@ApiProperty({
enum: getAvailableUserRoles(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here you need to change this to be

Suggested change
enum: getAvailableUserRoles(),
enum: getAvailableUserRoles,

Otherwise the function will be called on import 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess would be that you will also need to therefore change schema-object-factory.

Let me know if I can help

Copy link
Author

@eyeamkd eyeamkd Feb 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for pointing it out @slukes. I'll let you know if I need any help changing it.

enumName: 'Role',
isArray: true
})
rolesCategory: Role[];
}

it('should explore enum', () => {
Expand Down Expand Up @@ -91,6 +100,12 @@ describe('SchemaObjectFactory', () => {
$ref: '#/components/schemas/Role'
}
},
rolesCategory: {
type: 'array',
items: {
$ref: '#/components/schemas/Role'
}
},
groups: {
type: 'array',
items: {
Expand All @@ -104,7 +119,7 @@ describe('SchemaObjectFactory', () => {
}
}
},
required: ['role', 'roles', 'groups', 'rankings']
required: ['role', 'roles', 'groups', 'rankings', 'rolesCategory']
});
schemaObjectFactory.exploreModelSchema(CreatePersonDto, schemas);

Expand Down