0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-20 22:12:38 -05:00

[ci] format

This commit is contained in:
Billy Le 2024-08-13 10:22:03 +00:00 committed by astrobot-houston
parent dc0a297e2a
commit 5c9183a8ee
2 changed files with 29 additions and 25 deletions

View file

@ -135,20 +135,24 @@ export function formDataToObject<T extends z.AnyZodObject>(
for (const [key, baseValidator] of Object.entries(schema.shape)) { for (const [key, baseValidator] of Object.entries(schema.shape)) {
let validator = baseValidator; let validator = baseValidator;
while (validator instanceof z.ZodOptional || validator instanceof z.ZodNullable || validator instanceof z.ZodDefault) { while (
validator instanceof z.ZodOptional ||
validator instanceof z.ZodNullable ||
validator instanceof z.ZodDefault
) {
// use default value when key is undefined // use default value when key is undefined
if(validator instanceof z.ZodDefault && !formData.has(key)) { if (validator instanceof z.ZodDefault && !formData.has(key)) {
obj[key] = validator._def.defaultValue(); obj[key] = validator._def.defaultValue();
} }
validator = validator._def.innerType; validator = validator._def.innerType;
} }
if (!formData.has(key) && key in obj) { if (!formData.has(key) && key in obj) {
// continue loop if form input is not found and default value is set // continue loop if form input is not found and default value is set
continue; continue;
} else if (validator instanceof z.ZodBoolean) { } else if (validator instanceof z.ZodBoolean) {
const val = formData.get(key); const val = formData.get(key);
obj[key] = val === 'true' ? true : val === 'false' ? false : formData.has(key) obj[key] = val === 'true' ? true : val === 'false' ? false : formData.has(key);
} else if (validator instanceof z.ZodArray) { } else if (validator instanceof z.ZodArray) {
obj[key] = handleFormDataGetAll(key, formData, validator); obj[key] = handleFormDataGetAll(key, formData, validator);
} else { } else {

View file

@ -46,9 +46,9 @@ describe('formDataToObject', () => {
it('should handle boolean checks', () => { it('should handle boolean checks', () => {
const formData = new FormData(); const formData = new FormData();
formData.set('isCool', 'yes'); formData.set('isCool', 'yes');
formData.set('isTrue', true) formData.set('isTrue', true);
formData.set("isFalse", false); formData.set('isFalse', false);
formData.set("falseString", 'false') formData.set('falseString', 'false');
const input = z.object({ const input = z.object({
isCool: z.boolean(), isCool: z.boolean(),
@ -63,7 +63,7 @@ describe('formDataToObject', () => {
assert.equal(res.isNotCool, false); assert.equal(res.isNotCool, false);
assert.equal(res.isTrue, true); assert.equal(res.isTrue, true);
assert.equal(res.isFalse, false); assert.equal(res.isFalse, false);
assert.equal(res.falseString, false) assert.equal(res.falseString, false);
}); });
it('should handle optional values', () => { it('should handle optional values', () => {
@ -102,34 +102,34 @@ describe('formDataToObject', () => {
it('should handle zod default values', () => { it('should handle zod default values', () => {
const formData = new FormData(); const formData = new FormData();
const input = z.object({ const input = z.object({
name: z.string().default("test"), name: z.string().default('test'),
email: z.string().default('test@test.test'), email: z.string().default('test@test.test'),
favoriteNumbers: z.array(z.number()).default([1,2]) favoriteNumbers: z.array(z.number()).default([1, 2]),
}); });
const res = formDataToObject(formData, input); const res = formDataToObject(formData, input);
assert.equal(res.name, 'test'); assert.equal(res.name, 'test');
assert.equal(res.email, 'test@test.test'); assert.equal(res.email, 'test@test.test');
assert.deepEqual(res.favoriteNumbers, [1, 2]); assert.deepEqual(res.favoriteNumbers, [1, 2]);
}) });
it('should handle zod chaining of optional, default, and nullish values', () => { it('should handle zod chaining of optional, default, and nullish values', () => {
const formData = new FormData(); const formData = new FormData();
formData.set('email', 'test@test.test') formData.set('email', 'test@test.test');
const input = z.object({ const input = z.object({
name: z.string().default("test").optional(), name: z.string().default('test').optional(),
email: z.string().optional().nullish(), email: z.string().optional().nullish(),
favoriteNumbers: z.array(z.number()).default([1,2]).nullish().optional() favoriteNumbers: z.array(z.number()).default([1, 2]).nullish().optional(),
}); });
const res = formDataToObject(formData, input); const res = formDataToObject(formData, input);
assert.equal(res.name, 'test'); assert.equal(res.name, 'test');
assert.equal(res.email, 'test@test.test'); assert.equal(res.email, 'test@test.test');
assert.deepEqual(res.favoriteNumbers, [1, 2]) assert.deepEqual(res.favoriteNumbers, [1, 2]);
}) });
it('should handle File objects', () => { it('should handle File objects', () => {
const formData = new FormData(); const formData = new FormData();
@ -179,17 +179,17 @@ describe('formDataToObject', () => {
it('should handle an array of File objects', () => { it('should handle an array of File objects', () => {
const formData = new FormData(); const formData = new FormData();
const file1 = new File([''], 'test1.txt'); const file1 = new File([''], 'test1.txt');
const file2 = new File([''], 'test2.txt') const file2 = new File([''], 'test2.txt');
formData.append('files', file1); formData.append('files', file1);
formData.append('files', file2); formData.append('files', file2);
const input = z.object({ const input = z.object({
files: z.array(z.instanceof(File)) files: z.array(z.instanceof(File)),
}); });
const res = formDataToObject(formData, input); const res = formDataToObject(formData, input);
assert.equal(res.files instanceof Array, true); assert.equal(res.files instanceof Array, true);
assert.deepEqual(res.files, [file1, file2]) assert.deepEqual(res.files, [file1, file2]);
}); });
}); });