Skip to content

Commit dd07eb2

Browse files
committed
fix: support ctx.request.validateUsing with ValidatorExtractor
1 parent 60a9ed1 commit dd07eb2

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/code_scanners/routes_scanner/validator_extractor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,11 @@ export async function extractValidators(
7979
* Inspect validators via "request.validateUsing" and "vine.validate"
8080
* method calls.
8181
*/
82-
const validationCalls = inspectMethodArguments(method, ['request.validateUsing', 'vine.validate'])
82+
const validationCalls = inspectMethodArguments(method, [
83+
'request.validateUsing',
84+
'$CTX.request.validateUsing',
85+
'vine.validate',
86+
])
8387
.map((node) => {
8488
const firstArg = node.find({
8589
rule: { any: [{ kind: 'identifier' }, { kind: 'member_expression' }] },

tests/code_scanners/validator_extractor.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,44 @@ test.group('Validator extractor', () => {
408408
)
409409
})
410410

411+
test('extract validator when using ctx.request.validateUsing', async ({ assert, fs }) => {
412+
await fs.create(
413+
'app/controllers/users_controller.ts',
414+
`
415+
import { createUserValidator } from '#validators/user'
416+
417+
export default class UsersController {
418+
async store(ctx: HttpContext) {
419+
await ctx.request.validateUsing(createUserValidator)
420+
}
421+
}
422+
`
423+
)
424+
425+
assert.deepEqual(
426+
await extractValidators(fs.basePath, new VirtualFileSystem(fs.basePath), {
427+
path: join(fs.basePath, 'app/controllers/users_controller.ts'),
428+
method: 'store',
429+
name: 'UsersController',
430+
import: {
431+
type: 'default',
432+
value: 'UsersController',
433+
specifier: '#controllers/users_controller',
434+
},
435+
}),
436+
[
437+
{
438+
import: {
439+
specifier: '#validators/user',
440+
type: 'named',
441+
value: 'createUserValidator',
442+
},
443+
name: 'createUserValidator',
444+
},
445+
]
446+
)
447+
})
448+
411449
test('do not match method name when it appear in body of another method', async ({
412450
assert,
413451
fs,

0 commit comments

Comments
 (0)