From 5c9183a8ee9918937cd866a150af99d12987b8f7 Mon Sep 17 00:00:00 2001 From: Billy Le Date: Tue, 13 Aug 2024 10:22:03 +0000 Subject: [PATCH] [ci] format --- .../src/actions/runtime/virtual/server.ts | 20 ++++++----- .../units/actions/form-data-to-object.test.js | 34 +++++++++---------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/packages/astro/src/actions/runtime/virtual/server.ts b/packages/astro/src/actions/runtime/virtual/server.ts index e334cb632c..9bc387d6b8 100644 --- a/packages/astro/src/actions/runtime/virtual/server.ts +++ b/packages/astro/src/actions/runtime/virtual/server.ts @@ -135,20 +135,24 @@ export function formDataToObject( for (const [key, baseValidator] of Object.entries(schema.shape)) { 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 - if(validator instanceof z.ZodDefault && !formData.has(key)) { - obj[key] = validator._def.defaultValue(); + if (validator instanceof z.ZodDefault && !formData.has(key)) { + obj[key] = validator._def.defaultValue(); } 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; - } else if (validator instanceof z.ZodBoolean) { - const val = formData.get(key); - obj[key] = val === 'true' ? true : val === 'false' ? false : formData.has(key) + 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 { diff --git a/packages/astro/test/units/actions/form-data-to-object.test.js b/packages/astro/test/units/actions/form-data-to-object.test.js index b27d50c8c3..1369093056 100644 --- a/packages/astro/test/units/actions/form-data-to-object.test.js +++ b/packages/astro/test/units/actions/form-data-to-object.test.js @@ -46,9 +46,9 @@ 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') + formData.set('isTrue', true); + formData.set('isFalse', false); + formData.set('falseString', 'false'); const input = z.object({ isCool: z.boolean(), @@ -63,7 +63,7 @@ describe('formDataToObject', () => { assert.equal(res.isNotCool, false); assert.equal(res.isTrue, true); assert.equal(res.isFalse, false); - assert.equal(res.falseString, false) + assert.equal(res.falseString, false); }); it('should handle optional values', () => { @@ -102,34 +102,34 @@ describe('formDataToObject', () => { it('should handle zod default values', () => { const formData = new FormData(); - + const input = z.object({ - name: z.string().default("test"), + name: z.string().default('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); assert.equal(res.name, 'test'); assert.equal(res.email, 'test@test.test'); assert.deepEqual(res.favoriteNumbers, [1, 2]); - }) + }); it('should handle zod chaining of optional, default, and nullish values', () => { const formData = new FormData(); - formData.set('email', 'test@test.test') - + formData.set('email', 'test@test.test'); + const input = z.object({ - name: z.string().default("test").optional(), + name: z.string().default('test').optional(), 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); assert.equal(res.name, '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', () => { const formData = new FormData(); @@ -179,17 +179,17 @@ describe('formDataToObject', () => { it('should handle an array of File objects', () => { const formData = new FormData(); 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', file2); const input = z.object({ - files: z.array(z.instanceof(File)) + files: z.array(z.instanceof(File)), }); const res = formDataToObject(formData, input); assert.equal(res.files instanceof Array, true); - assert.deepEqual(res.files, [file1, file2]) + assert.deepEqual(res.files, [file1, file2]); }); });