Skip to content

Commit 2cacb8a

Browse files
committed
Added some File tests.
1 parent de831c7 commit 2cacb8a

File tree

2 files changed

+73
-15
lines changed

2 files changed

+73
-15
lines changed

src/lib/formData.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { SuperFormError, SchemaError } from './errors.js';
22
import type { SuperValidateOptions } from './superValidate.js';
33
import { parse } from 'devalue';
44
import type { JSONSchema7Definition } from 'json-schema';
5-
import { schemaInfo, type SchemaInfo } from './jsonSchema/schemaInfo.js';
5+
import { schemaInfo, type SchemaInfo, type SchemaType } from './jsonSchema/schemaInfo.js';
66
import { defaultValues } from './jsonSchema/schemaDefaults.js';
77
import type { JSONSchema } from './index.js';
88
import { setPaths } from './traversal.js';
@@ -166,7 +166,18 @@ function _parseFormData<T extends Record<string, unknown>>(
166166
return allowFiles && (entry.size || entry.name) ? entry : undefined;
167167
}
168168

169-
return parseFormDataEntry(key, entry, info);
169+
if (info.types.length > 1) {
170+
throw new SchemaError(
171+
'FormData parsing failed: ' +
172+
'Multiple types are only supported when the dataType option for superForm is set to "json".' +
173+
'Types found: ' +
174+
info.types,
175+
key
176+
);
177+
}
178+
179+
const [type] = info.types;
180+
return parseFormDataEntry(key, entry, type ?? 'any', info);
170181
}
171182

172183
const defaultPropertyType =
@@ -228,19 +239,12 @@ function _parseFormData<T extends Record<string, unknown>>(
228239
return output;
229240
}
230241

231-
function parseFormDataEntry(key: string, value: string, info: SchemaInfo): unknown {
232-
if (info.types.length != 1) {
233-
throw new SchemaError(
234-
'FormData parsing failed: ' +
235-
'Multiple types are only supported when the dataType option for superForm is set to "json".' +
236-
'Types found: ' +
237-
info.types,
238-
key
239-
);
240-
}
241-
242-
const [type] = info.types;
243-
242+
function parseFormDataEntry(
243+
key: string,
244+
value: string,
245+
type: Exclude<SchemaType, 'null'>,
246+
info: SchemaInfo
247+
): unknown {
244248
if (!value) {
245249
//console.log(`No FormData for "${key}" (${type}).`, info); //debug
246250

src/tests/superValidate.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,60 @@ describe('File handling with the allowFiles option', () => {
615615
});
616616
});
617617

618+
describe('Handling non-existing files', () => {
619+
const schema = z.object({
620+
image: z
621+
.custom<File>((f) => f instanceof File, 'Please upload a file.')
622+
.refine((f) => {
623+
return f instanceof File && f.size <= 1000;
624+
}, 'Max 1Kb upload size.')
625+
});
626+
627+
describe('Non-nullable schema', () => {
628+
it('should not accept a non-existing file', async () => {
629+
const formData = new FormData();
630+
const adapter = zod(schema);
631+
expect(adapter.defaults.image).toBeUndefined();
632+
633+
const form = await superValidate(formData, adapter, { allowFiles: true });
634+
assert(!form.valid);
635+
expect(form.errors.image).toEqual(['Please upload a file.']);
636+
});
637+
638+
it('should not accept an empty posted value', async () => {
639+
const formData = new FormData();
640+
formData.set('image', '');
641+
const form = await superValidate(formData, zod(schema), { allowFiles: true });
642+
643+
assert(!form.valid);
644+
expect(form.errors.image).toEqual(['Please upload a file.']);
645+
});
646+
});
647+
648+
describe('Nullable schema', () => {
649+
const schemaNullable = schema.extend({
650+
image: schema.shape.image.nullable()
651+
});
652+
653+
it('should accept a non-existing file', async () => {
654+
const formData = new FormData();
655+
const form = await superValidate(formData, zod(schemaNullable), { allowFiles: true });
656+
657+
assert(form.valid);
658+
expect(form.data.image).toBeNull();
659+
});
660+
661+
it('should accept an empty posted value', async () => {
662+
const formData = new FormData();
663+
formData.set('image', '');
664+
const form = await superValidate(formData, zod(schemaNullable), { allowFiles: true });
665+
666+
assert(form.valid);
667+
expect(form.data.image).toBeNull();
668+
});
669+
});
670+
});
671+
618672
it('should allow files if specified as an option', async () => {
619673
const formData = new FormData();
620674
formData.set('avatar', new Blob(['A'.repeat(100)]));

0 commit comments

Comments
 (0)