Skip to content

Commit

Permalink
fix: handling ZodDefault (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
sahinvardar authored Nov 1, 2023
1 parent a899959 commit 166e33e
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 10 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.7",
"version": "0.1.8",
"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.7",
"version": "0.1.8",
"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.7",
"version": "0.1.8",
"description": "",
"main": "dist/index.js",
"type": "module",
Expand Down
4 changes: 4 additions & 0 deletions library/src/tests/test-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const SCHEMA = z
.object({
string: z.string().optional(),
boolean: z.boolean(),
defaultBoolean: z.boolean().default(true),
bigInt: z.bigint(),
number: z.number(),
stringArray: z.array(z.string()),
Expand All @@ -28,6 +29,7 @@ export const EXPECTED_DATA: DataType = {
bigInt: 1n,
bigIntArray: [0n, 1n, 2n],
boolean: true,
defaultBoolean: true,
booleanArray: [true, false],
number: 1024,
numberArray: [0],
Expand All @@ -53,6 +55,7 @@ export function getDom() {
<option value="2">2</option>
</select>
<input name="boolean" type="checkbox" />
<input name="defaultBoolean" type="checkbox" />
<select multiple name="booleanArray">
<option value="true">true</option>
<option value="false">false</option>
Expand Down Expand Up @@ -94,6 +97,7 @@ export function getFormData() {
formData.append('bigIntArray', '1');
formData.append('bigIntArray', '2');
formData.append('boolean', 'true');
formData.append('defaultBoolean', 'true');
formData.append('booleanArray', 'true');
formData.append('booleanArray', 'false');
formData.append('number', '1024');
Expand Down
8 changes: 3 additions & 5 deletions library/src/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { describe, expect, test } from 'vitest';
import {
flattenObject,
flattenSchema,
formDataToData,
formDataToObject,
objectToFormData,
parseFormData,
Expand All @@ -18,6 +17,7 @@ describe('utils', () => {
bigInt: ['1'],
bigIntArray: ['0', '1', '2'],
boolean: ['true'],
defaultBoolean: ['true'],
booleanArray: ['true', 'false'],
number: ['1024'],
numberArray: ['0'],
Expand Down Expand Up @@ -48,6 +48,7 @@ describe('utils', () => {
'bigInt',
'bigIntArray',
'boolean',
'defaultBoolean',
'booleanArray',
'number',
'numberArray',
Expand Down Expand Up @@ -81,6 +82,7 @@ describe('utils', () => {
'bigInt',
'bigIntArray',
'boolean',
'defaultBoolean',
'booleanArray',
'number',
'numberArray',
Expand All @@ -104,8 +106,4 @@ describe('utils', () => {
expect(result.data).toStrictEqual(EXPECTED_DATA);
}
});

test('formDataToData', () => {
expect(formDataToData(getFormData(), SCHEMA)).toStrictEqual(EXPECTED_DATA);
});
});
6 changes: 5 additions & 1 deletion library/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ZodEffectsDef, ZodFirstPartyTypeKind, ZodOptionalDef, z } from 'zod';
import { ZodDefaultDef, ZodEffectsDef, ZodFirstPartyTypeKind, ZodOptionalDef, z } from 'zod';

import { DATA_VALIDATION_ERROR_MESSAGE_ATTRIBUTE_NAME } from './form-validation.js';

Expand Down Expand Up @@ -27,6 +27,10 @@ export function fullyUnwrap<TSchema extends z.ZodSchema>(schema: TSchema) {
result = fullyUnwrap((schema._def as ZodOptionalDef).innerType);
}

if (type === ZodFirstPartyTypeKind.ZodDefault) {
result = fullyUnwrap((schema._def as ZodDefaultDef).innerType);
}

while (result['unwrap'] !== undefined && typeof result['unwrap'] === 'function') {
result = result['unwrap']();
}
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.7",
"version": "0.1.8",
"license": "MIT",
"repository": {
"type": "git",
Expand Down

0 comments on commit 166e33e

Please sign in to comment.