Skip to content

Commit cb13e27

Browse files
committed
fix: ensure field is defined before validating it as a file
1 parent cfa26fa commit cb13e27

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

tests/bindings/vinejs.spec.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,77 @@ test.group('Bindings | VineJS', (group) => {
178178
assert.equal(avatar.size, 4000000)
179179
assert.lengthOf(avatar.errors, 0)
180180
})
181+
182+
test('pass validation when file is null and marked as nullable', async ({ assert }) => {
183+
const validator = vine.create(
184+
vine.object({
185+
avatar: vine
186+
.file(() => {
187+
return { extnames: ['jpg'] }
188+
})
189+
.nullable(),
190+
})
191+
)
192+
193+
const payload = await validator.validate({
194+
avatar: null,
195+
})
196+
assert.isNull(payload.avatar)
197+
})
198+
199+
test('pass validation when file is null and marked as optional', async ({ assert }) => {
200+
const validator = vine.create(
201+
vine.object({
202+
avatar: vine
203+
.file(() => {
204+
return { extnames: ['jpg'] }
205+
})
206+
.optional(),
207+
})
208+
)
209+
210+
const payload = await validator.validate({
211+
avatar: null,
212+
})
213+
assert.isUndefined(payload.avatar)
214+
})
215+
216+
test('pass validation when file is missing and marked as optional', async ({ assert }) => {
217+
const validator = vine.create(
218+
vine.object({
219+
avatar: vine
220+
.file(() => {
221+
return { extnames: ['jpg'] }
222+
})
223+
.optional(),
224+
})
225+
)
226+
227+
const payload = await validator.validate({})
228+
assert.isUndefined(payload.avatar)
229+
})
230+
231+
test('raise error when field is marked as nullable, but missing', async ({ assert }) => {
232+
const validator = vine.create(
233+
vine.object({
234+
avatar: vine
235+
.file(() => {
236+
return { extnames: ['jpg'] }
237+
})
238+
.nullable(),
239+
})
240+
)
241+
242+
try {
243+
await validator.validate({})
244+
} catch (error) {
245+
assert.deepEqual(error.messages, [
246+
{
247+
field: 'avatar',
248+
message: 'The avatar field must be defined',
249+
rule: 'required',
250+
},
251+
])
252+
}
253+
})
181254
})

0 commit comments

Comments
 (0)