-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix/actions-default-values: set default values from zod if FormData k…
…ey is not present
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'astro': patch | ||
--- | ||
|
||
Set action input default values from zod if FormData key is not present |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,6 +91,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')); | ||
|
@@ -135,4 +166,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]) | ||
}); | ||
}); |