|
| 1 | +import { Controller, Get, Query } from '@nestjs/common'; |
1 | 2 | import { Result } from 'true-myth'; |
2 | 3 |
|
3 | | -import { generateSchemas, input, make } from '../../tests/helpers'; |
| 4 | +import { generateSchemas, generateSpec, input, make } from '../../tests/helpers'; |
4 | 5 | import { IsEnum } from '../nestjs-swagger-dto'; |
5 | 6 |
|
6 | 7 | describe('IsEnum', () => { |
@@ -153,6 +154,48 @@ describe('IsEnum', () => { |
153 | 154 | }); |
154 | 155 | }); |
155 | 156 |
|
| 157 | + it('generates correct schema including minLength and maxLength when used in @Query', async () => { |
| 158 | + enum Flag { |
| 159 | + On = 'On', |
| 160 | + Off = 'Off', |
| 161 | + } |
| 162 | + |
| 163 | + class TestQuery { |
| 164 | + @IsEnum({ |
| 165 | + enum: { Flag }, |
| 166 | + isArray: { minLength: 1, maxLength: 2, force: true }, |
| 167 | + optional: true, |
| 168 | + }) |
| 169 | + enumField?: Flag; |
| 170 | + } |
| 171 | + |
| 172 | + @Controller('x') |
| 173 | + class AppController { |
| 174 | + @Get() |
| 175 | + someEndpoint(@Query() {}: TestQuery) {} |
| 176 | + } |
| 177 | + |
| 178 | + const spec = await generateSpec([AppController]); |
| 179 | + |
| 180 | + expect(spec.paths['/x'].get).toStrictEqual({ |
| 181 | + operationId: 'AppController_someEndpoint', |
| 182 | + parameters: [ |
| 183 | + { |
| 184 | + in: 'query', |
| 185 | + name: 'enumField', |
| 186 | + required: false, |
| 187 | + schema: { |
| 188 | + items: { $ref: '#/components/schemas/Flag' }, |
| 189 | + maxItems: 2, |
| 190 | + minItems: 1, |
| 191 | + type: 'array', |
| 192 | + }, |
| 193 | + }, |
| 194 | + ], |
| 195 | + responses: { '200': { description: '' } }, |
| 196 | + }); |
| 197 | + }); |
| 198 | + |
156 | 199 | it('accepts enum arrays', async () => { |
157 | 200 | expect(await input(Test, { enumField: ['a', 'b'] })).toStrictEqual( |
158 | 201 | Result.ok(make(Test, { enumField: ['a', 'b'] })), |
|
0 commit comments