Skip to content

Commit

Permalink
fix: empty array handling (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
sahinvardar authored Nov 2, 2023
1 parent 166e33e commit 1f88adb
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/html/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zod-form-validation-html-example",
"version": "0.1.8",
"version": "0.1.9",
"description": "",
"main": "index.html",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion examples/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react",
"version": "0.1.8",
"version": "0.1.9",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
2 changes: 1 addition & 1 deletion library/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vardario/zod-form-validation",
"version": "0.1.8",
"version": "0.1.9",
"description": "",
"main": "dist/index.js",
"type": "module",
Expand Down
10 changes: 10 additions & 0 deletions library/src/tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, test } from 'vitest';
import { z } from 'zod';
import {
flattenObject,
flattenSchema,
Expand Down Expand Up @@ -106,4 +107,13 @@ describe('utils', () => {
expect(result.data).toStrictEqual(EXPECTED_DATA);
}
});

test('emptyArrayHandling', () => {
const schema = z.object({
array: z.array(z.string()),
});
const formData = new FormData();
formData.append('array', '');
expect(parseFormData(formData, schema).success).toBe(true);
});
});
7 changes: 4 additions & 3 deletions library/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ export function preprocess<TSchema extends z.ZodSchema>(values: ObjectType, sche
switch (zodType) {
case z.ZodFirstPartyTypeKind.ZodArray: {
const arraySchema = unboxedSchema._def as z.ZodArrayDef;
return values.length ? values.map((item) => preprocess([item], arraySchema.type)) : undefined;
return values.length
? values.map((item) => preprocess([item], arraySchema.type)).filter((item) => item !== undefined)
: undefined;
}

case z.ZodFirstPartyTypeKind.ZodBigInt: {
Expand Down Expand Up @@ -115,9 +117,8 @@ export function formDataToObject(formData: FormData) {
return result;
}

export function formDataToData<TSchema extends z.ZodSchema>(formData: FormData, schema: TSchema){
export function formDataToData<TSchema extends z.ZodSchema>(formData: FormData, schema: TSchema) {
return preprocess(formDataToObject(formData), schema);

}

export function objectToFormData(obj: any) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "root",
"version": "0.1.8",
"version": "0.1.9",
"license": "MIT",
"repository": {
"type": "git",
Expand Down

0 comments on commit 1f88adb

Please sign in to comment.