Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set action input default values from zod if FormData key is not present #11655

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nervous-garlics-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes Astro Actions `input` validation when using `default` values with a form input.
16 changes: 13 additions & 3 deletions packages/astro/src/actions/runtime/virtual/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,21 @@ export function formDataToObject<T extends z.AnyZodObject>(
const obj: Record<string, unknown> = {};
for (const [key, baseValidator] of Object.entries(schema.shape)) {
let validator = baseValidator;
while (validator instanceof z.ZodOptional || validator instanceof z.ZodNullable) {

while (validator instanceof z.ZodOptional || validator instanceof z.ZodNullable || validator instanceof z.ZodDefault) {
// use default value when key is undefined
if(validator instanceof z.ZodDefault && !formData.has(key)) {
obj[key] = validator._def.defaultValue();
}
validator = validator._def.innerType;
}
if (validator instanceof z.ZodBoolean) {
obj[key] = formData.has(key);

if (!formData.has(key) && key in obj) {
// continue loop if form input is not found and default value is set
continue;
} else if (validator instanceof z.ZodBoolean) {
const val = formData.get(key);
obj[key] = val === 'true' ? true : val === 'false' ? false : formData.has(key)
} else if (validator instanceof z.ZodArray) {
obj[key] = handleFormDataGetAll(key, formData, validator);
} else {
Expand Down
57 changes: 57 additions & 0 deletions packages/astro/test/units/actions/form-data-to-object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,24 @@ describe('formDataToObject', () => {
it('should handle boolean checks', () => {
const formData = new FormData();
formData.set('isCool', 'yes');
formData.set('isTrue', true)
formData.set("isFalse", false);
formData.set("falseString", 'false')

const input = z.object({
isCool: z.boolean(),
isNotCool: z.boolean(),
isTrue: z.boolean(),
isFalse: z.boolean(),
falseString: z.boolean(),
});

const res = formDataToObject(formData, input);
assert.equal(res.isCool, true);
assert.equal(res.isNotCool, false);
assert.equal(res.isTrue, true);
assert.equal(res.isFalse, false);
assert.equal(res.falseString, false)
});

it('should handle optional values', () => {
Expand Down Expand Up @@ -91,6 +100,37 @@ describe('formDataToObject', () => {
assert.equal(res.age, null);
});

it('should handle zod default values', () => {
const formData = new FormData();

const input = z.object({
name: z.string().default("test"),
email: z.string().default('[email protected]'),
favoriteNumbers: z.array(z.number()).default([1,2])
});

const res = formDataToObject(formData, input);
assert.equal(res.name, 'test');
assert.equal(res.email, '[email protected]');
assert.deepEqual(res.favoriteNumbers, [1, 2]);
})

it('should handle zod chaining of optional, default, and nullish values', () => {
const formData = new FormData();
formData.set('email', '[email protected]')

const input = z.object({
name: z.string().default("test").optional(),
email: z.string().optional().nullish(),
favoriteNumbers: z.array(z.number()).default([1,2]).nullish().optional()
});

const res = formDataToObject(formData, input);
assert.equal(res.name, 'test');
assert.equal(res.email, '[email protected]');
assert.deepEqual(res.favoriteNumbers, [1, 2])
})

it('should handle File objects', () => {
const formData = new FormData();
formData.set('file', new File([''], 'test.txt'));
Expand Down Expand Up @@ -135,4 +175,21 @@ describe('formDataToObject', () => {
assert.ok(Array.isArray(res.age), 'age is not an array');
assert.deepEqual(res.age.sort(), [25, 30, 35]);
});

it('should handle an array of File objects', () => {
const formData = new FormData();
const file1 = new File([''], 'test1.txt');
const file2 = new File([''], 'test2.txt')
formData.append('files', file1);
formData.append('files', file2);

const input = z.object({
files: z.array(z.instanceof(File))
});

const res = formDataToObject(formData, input);

assert.equal(res.files instanceof Array, true);
assert.deepEqual(res.files, [file1, file2])
});
});
Loading