@omer-x/next-form-actions 2.0.0-alpha.1 → 2.0.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -0
- package/dist/index.d.mts +4 -1
- package/dist/index.mjs +66 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -53,6 +53,20 @@ export const createUser = defineFormAction({
|
|
|
53
53
|
});
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
+
### Empty fields
|
|
57
|
+
|
|
58
|
+
A blank input is sent as `""`. When `""` can't be a value of the field's type, it becomes "no value":
|
|
59
|
+
|
|
60
|
+
| Field schema | `""` becomes |
|
|
61
|
+
|---|---|
|
|
62
|
+
| `z.number()`, `z.boolean()`, `z.bigint()`, `z.date()`, `z.enum([...])` | `undefined` |
|
|
63
|
+
| the same, wrapped in `.nullable()` or `.nullish()` | `null` |
|
|
64
|
+
| a string format (`z.email()`, `z.iso.date()`, `z.string().startsWith(...)`, ...) that rejects `""` | `undefined`, or `null` if nullable |
|
|
65
|
+
| `z.string()`, with or without `.nullable()` or `.min()` | `""` (kept: it is a real string value) |
|
|
66
|
+
| an enum or literal that has `""` as a value, or a union | `""` (kept) |
|
|
67
|
+
|
|
68
|
+
Nullable fields get `null` so that a cleared field stays different from a missing one (in patch forms that send only the changed fields, a missing field means "unchanged"). To store `null` for an empty plain string, handle it in the schema, e.g. `z.preprocess(value => value === "" ? null : value, z.string().nullable())`.
|
|
69
|
+
|
|
56
70
|
### Using it in a form
|
|
57
71
|
|
|
58
72
|
```tsx
|
package/dist/index.d.mts
CHANGED
|
@@ -48,8 +48,11 @@ type FormActionState<Data = undefined> = IdleState | SuccessState<Data> | Failur
|
|
|
48
48
|
/**
|
|
49
49
|
* A form action, as returned by `defineFormAction`. With its extra arguments bound
|
|
50
50
|
* (e.g. `action.bind(null, id)`), it becomes a `FormAction<Data>` that `useActionState` accepts.
|
|
51
|
+
*
|
|
52
|
+
* The action never reads `previousState`, so it is typed as `unknown`. That keeps `Data` only
|
|
53
|
+
* in the return type, so `FormAction<unknown>` accepts any form action.
|
|
51
54
|
*/
|
|
52
|
-
type FormAction<Data = undefined, Args extends unknown[] = []> = (...params: [...Args, previousState:
|
|
55
|
+
type FormAction<Data = undefined, Args extends unknown[] = []> = (...params: [...Args, previousState: unknown, formData: FormData]) => Promise<FormActionState<Data>>;
|
|
53
56
|
/**
|
|
54
57
|
* Describes the definition of a type-safe server action for forms.
|
|
55
58
|
*/
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { safeParse } from "zod/v4/core";
|
|
1
2
|
// src/utils/formDataToObject.ts
|
|
2
3
|
function formDataToObject(formData) {
|
|
3
4
|
const result = {};
|
|
@@ -74,40 +75,95 @@ function bundleErrors(issues) {
|
|
|
74
75
|
/**
|
|
75
76
|
* Strips wrappers (optional, default, pipe, ...) to find the schema that decides the value's type.
|
|
76
77
|
* @param schema - The schema of a single field.
|
|
77
|
-
* @
|
|
78
|
+
* @param nullable - Whether a nullable wrapper was already passed.
|
|
79
|
+
* @returns The innermost schema, and whether the field accepts null.
|
|
78
80
|
*/
|
|
79
|
-
function unwrap(schema) {
|
|
81
|
+
function unwrap(schema, nullable = false) {
|
|
80
82
|
const def = schema._zod.def;
|
|
81
83
|
switch (def.type) {
|
|
84
|
+
case "nullable": return def.innerType ? unwrap(def.innerType, true) : {
|
|
85
|
+
schema,
|
|
86
|
+
nullable: true
|
|
87
|
+
};
|
|
82
88
|
case "optional":
|
|
83
|
-
case "nullable":
|
|
84
89
|
case "default":
|
|
85
90
|
case "prefault":
|
|
86
91
|
case "catch":
|
|
87
92
|
case "readonly":
|
|
88
|
-
case "nonoptional": return def.innerType ? unwrap(def.innerType) :
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
93
|
+
case "nonoptional": return def.innerType ? unwrap(def.innerType, nullable) : {
|
|
94
|
+
schema,
|
|
95
|
+
nullable
|
|
96
|
+
};
|
|
97
|
+
case "pipe": return def.in ? unwrap(def.in, nullable) : {
|
|
98
|
+
schema,
|
|
99
|
+
nullable
|
|
100
|
+
};
|
|
101
|
+
case "lazy": return def.getter ? unwrap(def.getter(), nullable) : {
|
|
102
|
+
schema,
|
|
103
|
+
nullable
|
|
104
|
+
};
|
|
105
|
+
default: return {
|
|
106
|
+
schema,
|
|
107
|
+
nullable
|
|
108
|
+
};
|
|
92
109
|
}
|
|
93
110
|
}
|
|
94
111
|
/**
|
|
112
|
+
* Tells whether an empty string can't be a real value of the schema, so an empty input means "no value".
|
|
113
|
+
* Plain strings keep "": it is a real string value. Strings with a format (email, ISO date, ...) count as empty only
|
|
114
|
+
* if "" fails the schema.
|
|
115
|
+
* @param schema - The innermost schema of the field.
|
|
116
|
+
* @returns Whether "" should become null or undefined.
|
|
117
|
+
*/
|
|
118
|
+
function isEmptyMeaningless(schema) {
|
|
119
|
+
const def = schema._zod.def;
|
|
120
|
+
switch (def.type) {
|
|
121
|
+
case "number":
|
|
122
|
+
case "bigint":
|
|
123
|
+
case "boolean":
|
|
124
|
+
case "date": return true;
|
|
125
|
+
case "enum":
|
|
126
|
+
case "literal": return !schema._zod.values?.has("");
|
|
127
|
+
case "string":
|
|
128
|
+
if (!(def.format !== void 0 || !!def.checks?.some((check) => check._zod.def.check === "string_format"))) return false;
|
|
129
|
+
try {
|
|
130
|
+
return !safeParse(schema, "").success;
|
|
131
|
+
} catch {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
default: return false;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
const DECIMAL = /^\s*[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?\s*$/i;
|
|
138
|
+
/**
|
|
139
|
+
* Reads a whole string as a decimal number. Partial numbers ("12abc"), other notations ("0x10", "Infinity") and
|
|
140
|
+
* localized decimals ("1,5") are left as they are, so the schema can report them.
|
|
141
|
+
* @param value - The raw form value.
|
|
142
|
+
* @returns The number, or the original string.
|
|
143
|
+
*/
|
|
144
|
+
function parseDecimal(value) {
|
|
145
|
+
return DECIMAL.test(value) ? Number(value) : value;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
95
148
|
* Converts a single form value to the type its schema expects. Values that can't be converted are left as they are,
|
|
96
|
-
* so the schema can report them.
|
|
149
|
+
* so the schema can report them. An empty string becomes null (nullable fields) or undefined when the field's type
|
|
150
|
+
* can't hold "".
|
|
97
151
|
* @param schema - The schema of the field.
|
|
98
152
|
* @param value - The raw form value.
|
|
99
153
|
* @returns The converted value.
|
|
100
154
|
*/
|
|
101
155
|
function coerceValue(schema, value) {
|
|
102
|
-
const
|
|
156
|
+
const inner = unwrap(schema);
|
|
157
|
+
const def = inner.schema._zod.def;
|
|
103
158
|
if (def.type === "array") {
|
|
104
159
|
if (value === void 0) return value;
|
|
105
160
|
const items = Array.isArray(value) ? value : [value];
|
|
106
161
|
return def.element ? items.map((item) => coerceValue(def.element, item)) : items;
|
|
107
162
|
}
|
|
108
163
|
if (Array.isArray(value)) return coerceValue(schema, value.at(-1));
|
|
164
|
+
if (value === "" && isEmptyMeaningless(inner.schema)) return inner.nullable ? null : void 0;
|
|
109
165
|
switch (def.type) {
|
|
110
|
-
case "number": return typeof value === "string" ?
|
|
166
|
+
case "number": return typeof value === "string" ? parseDecimal(value) : value;
|
|
111
167
|
case "boolean":
|
|
112
168
|
if (value === "true") return true;
|
|
113
169
|
if (value === "false") return false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@omer-x/next-form-actions",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.3",
|
|
4
4
|
"description": "Provides helper functions to define a type-safe React server action for forms, validating form data with Zod and handling errors internally.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"nextjs",
|