Skip to content

Commit bd80b39

Browse files
committed
feat: use merged request body during validation when mergeMultipartFieldsAndFiles flag is enabled
Closes: #4777
1 parent 06d7591 commit bd80b39

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

modules/http/request_validator.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import type {
1616
} from '@vinejs/vine/types'
1717

1818
import type { HttpContext } from './main.js'
19+
import type { FeatureFlags } from '../app.js'
20+
import type { ExperimentalFlagsList } from '../../types/app.js'
1921
import type { RequestValidationOptions } from '../../types/http.js'
2022

2123
/**
@@ -25,9 +27,11 @@ import type { RequestValidationOptions } from '../../types/http.js'
2527
*/
2628
export class RequestValidator {
2729
#ctx: HttpContext
30+
#experimentalFlags?: FeatureFlags<ExperimentalFlagsList>
2831

29-
constructor(ctx: HttpContext) {
32+
constructor(ctx: HttpContext, experimentalFlags?: FeatureFlags<ExperimentalFlagsList>) {
3033
this.#ctx = ctx
34+
this.#experimentalFlags = experimentalFlags
3135
}
3236

3337
/**
@@ -75,12 +79,18 @@ export class RequestValidator {
7579
validatorOptions.messagesProvider = RequestValidator.messagesProvider(this.#ctx)
7680
}
7781

82+
const requestBody = this.#experimentalFlags?.enabled('mergeMultipartFieldsAndFiles')
83+
? this.#ctx.request.all()
84+
: {
85+
...this.#ctx.request.all(),
86+
...this.#ctx.request.allFiles(),
87+
}
88+
7889
/**
7990
* Data to validate
8091
*/
8192
const data = validatorOptions.data || {
82-
...this.#ctx.request.all(),
83-
...this.#ctx.request.allFiles(),
93+
...requestBody,
8494
params: this.#ctx.request.params(),
8595
headers: this.#ctx.request.headers(),
8696
cookies: this.#ctx.request.cookiesList(),

providers/app_provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export default class AppServiceProvider {
135135
protected registerBodyParserMiddleware() {
136136
this.app.container.bind(BodyParserMiddleware, () => {
137137
const config = this.app.config.get<any>('bodyparser')
138-
return new BodyParserMiddleware(config)
138+
return new BodyParserMiddleware(config, this.app.experimentalFlags)
139139
})
140140
}
141141

providers/vinejs_provider.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ export default class VineJSServiceProvider {
129129
}
130130

131131
boot() {
132+
const experimentalFlags = this.app.experimentalFlags
133+
132134
/**
133135
* The file method is used to validate a field to be a valid
134136
* multipart file.
@@ -142,7 +144,7 @@ export default class VineJSServiceProvider {
142144
* data for the current request using VineJS validators
143145
*/
144146
Request.macro('validateUsing', function (this: Request, ...args) {
145-
return new RequestValidator(this.ctx!).validateUsing(...args)
147+
return new RequestValidator(this.ctx!, experimentalFlags).validateUsing(...args)
146148
})
147149
}
148150
}

0 commit comments

Comments
 (0)