Skip to content

Commit e31d1a8

Browse files
committed
Fixed JSON Schema for simple adapters with empty arrays.
1 parent 41974ec commit e31d1a8

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Fixed diff algorithm problem with tainted objects.
1818
- Prevented crash when custom validity doesn't exist for an element.
1919
- `dateProxy` didn't restore properly with [snapshots](https://superforms.rocks/concepts/snapshots).
20+
- Fixed JSON Schema for simple adapters with empty arrays.
2021

2122
### Changed
2223

src/lib/adapters/simple-schema/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function simpleSchema(value: unknown): JSONSchema {
1717

1818
if (Array.isArray(value)) {
1919
const output: JSONSchema = { type: 'array' };
20-
if (value.length) output.items = simpleSchema(value[0]);
20+
output.items = value.length ? simpleSchema(value[0]) : {};
2121
return output;
2222
} else {
2323
const obj = value as Record<string, unknown>;

src/tests/formData.test.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import * as v from 'valibot';
55
import { zodToJSONSchema } from '$lib/adapters/zod.js';
66
import { SchemaError } from '$lib/index.js';
77
import { valibot } from '$lib/adapters/valibot.js';
8+
import { type } from 'arktype';
9+
import { arktype } from '$lib/adapters/arktype.js';
810

911
enum Foo {
1012
A = 2,
@@ -27,11 +29,14 @@ const schema = z.object({
2729

2830
const bigJsonSchema = zodToJSONSchema(schema);
2931

30-
function dataToFormData(data: Record<string, string | number | string[] | number[]>) {
32+
function dataToFormData(
33+
data: Record<string, string | number | File | string[] | number[] | File[]>
34+
) {
3135
const output = new FormData();
3236
for (const [key, value] of Object.entries(data)) {
33-
if (Array.isArray(value)) value.forEach((v) => output.append(key, String(v)));
34-
else output.set(key, String(value));
37+
if (Array.isArray(value))
38+
value.forEach((v) => output.append(key, v instanceof File ? v : String(v)));
39+
else output.set(key, value instanceof File ? value : String(value));
3540
}
3641
return output;
3742
}
@@ -78,4 +83,22 @@ describe('FormData parsing', () => {
7883

7984
expect(valibot(schema).defaults.urltest).toBe('');
8085
});
86+
87+
it('should handle empty arrays with simple adapters as "any"', () => {
88+
const uploadSchema = type({
89+
files: type.instanceOf(File).array()
90+
});
91+
92+
const defaults = { defaults: { files: [] as File[] } };
93+
const adapter = arktype(uploadSchema, defaults);
94+
95+
const formData = dataToFormData({
96+
files: [new File(['123123'], 'test.png')]
97+
});
98+
const parsed = parseFormData(formData, adapter.jsonSchema, { allowFiles: true });
99+
const file = parsed.data?.files as File[];
100+
101+
expect(file[0].size).toBe(6);
102+
expect(file[0].name).toBe('test.png');
103+
});
81104
});

0 commit comments

Comments
 (0)