@standard-community/standard-form 0.0.1 → 0.1.0
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/LICENSE +21 -0
- package/README.md +61 -9
- package/dist/index.cjs +9 -3
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +10 -4
- package/dist/utils-BoRc22Ql.cjs +45 -0
- package/dist/utils-Cr64FEDS.js +36 -0
- package/dist/{utils-QJKenv_Z.d.cts → utils-DihtL_2q.d.cts} +5 -2
- package/dist/{utils-QJKenv_Z.d.ts → utils-DihtL_2q.d.ts} +5 -2
- package/dist/vendors/arktype.cjs +113 -0
- package/dist/vendors/arktype.d.cts +5 -0
- package/dist/vendors/arktype.d.ts +5 -0
- package/dist/vendors/arktype.js +111 -0
- package/dist/vendors/valibot.cjs +93 -0
- package/dist/vendors/valibot.d.cts +5 -0
- package/dist/vendors/valibot.d.ts +5 -0
- package/dist/vendors/valibot.js +91 -0
- package/dist/vendors/zod.cjs +91 -117
- package/dist/vendors/zod.js +91 -117
- package/package.json +24 -8
- package/dist/utils-BxGdbpqk.js +0 -18
- package/dist/utils-f6FAxdZk.cjs +0 -23
package/dist/vendors/zod.js
CHANGED
|
@@ -1,139 +1,113 @@
|
|
|
1
|
-
import { a as UnsupportedSchemaError } from '../utils-
|
|
1
|
+
import { a as UnsupportedSchemaError, b as hasFormDataKeyOrNested, h as hasFormDataKeyWithPrefix, i as isTrueFormValue, c as coerceFormNumber } from '../utils-Cr64FEDS.js';
|
|
2
2
|
|
|
3
3
|
async function zod() {
|
|
4
|
-
return (schema, formData) => {
|
|
5
|
-
if (!
|
|
4
|
+
return (schema, formData, options) => {
|
|
5
|
+
if (!isZodSchema(schema)) {
|
|
6
6
|
throw new UnsupportedSchemaError("zod", "Zod 4 and higher is required");
|
|
7
7
|
}
|
|
8
|
-
const
|
|
9
|
-
return
|
|
8
|
+
const baseSchema = unwrapBaseObjectSchema(schema, formData);
|
|
9
|
+
return hasZodType(baseSchema, "object") ? formDataToObject(formData, baseSchema, "", options) : formData;
|
|
10
10
|
};
|
|
11
11
|
}
|
|
12
|
-
function
|
|
13
|
-
return typeof
|
|
12
|
+
function isZodSchema(schema) {
|
|
13
|
+
return typeof schema === "object" && schema !== null && "_zod" in schema;
|
|
14
14
|
}
|
|
15
|
-
function
|
|
16
|
-
return
|
|
17
|
-
}
|
|
18
|
-
function definition(schema) {
|
|
19
|
-
const internal = get(schema, "_zod");
|
|
20
|
-
const def = get(internal, "def");
|
|
21
|
-
return isRecord(def) ? def : void 0;
|
|
22
|
-
}
|
|
23
|
-
function isSchema(value) {
|
|
24
|
-
return typeof get(definition(value), "type") === "string";
|
|
25
|
-
}
|
|
26
|
-
function schemaType(schema) {
|
|
27
|
-
const type = get(definition(schema), "type");
|
|
28
|
-
return typeof type === "string" ? type : void 0;
|
|
29
|
-
}
|
|
30
|
-
function unwrap(schema) {
|
|
31
|
-
while (true) {
|
|
32
|
-
const kind = schemaType(schema);
|
|
33
|
-
const def = definition(schema);
|
|
34
|
-
if (!def) return schema;
|
|
35
|
-
if (kind === "optional" || kind === "nullable" || kind === "default") {
|
|
36
|
-
schema = def.innerType;
|
|
37
|
-
} else if (kind === "pipe") {
|
|
38
|
-
schema = def.in;
|
|
39
|
-
} else {
|
|
40
|
-
return schema;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
15
|
+
function hasZodType(schema, type) {
|
|
16
|
+
return isZodSchema(schema) && schema._zod.def.type === type;
|
|
43
17
|
}
|
|
44
18
|
function isDiscriminatedUnion(schema) {
|
|
45
|
-
return
|
|
46
|
-
}
|
|
47
|
-
function objectShape(schema) {
|
|
48
|
-
const shape = get(definition(schema), "shape");
|
|
49
|
-
return isRecord(shape) ? shape : void 0;
|
|
19
|
+
return schema._zod.def.type === "union" && "discriminator" in schema._zod.def;
|
|
50
20
|
}
|
|
51
|
-
function
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
function convertObject(formData, schema, prefix = "") {
|
|
67
|
-
const result = {};
|
|
68
|
-
const keys = [...formData.keys()];
|
|
69
|
-
const def = definition(schema);
|
|
70
|
-
const shape = objectShape(schema);
|
|
71
|
-
if (!def || !shape) return result;
|
|
72
|
-
if (def.catchall) {
|
|
73
|
-
for (const [key, value] of formData.entries()) {
|
|
74
|
-
if (key.startsWith(prefix)) result[key.slice(prefix.length)] = value;
|
|
21
|
+
function formDataToObject(formData, schema, prefix = "", options = {}) {
|
|
22
|
+
const formKeys = [...formData.keys()];
|
|
23
|
+
const obj = schema._zod.def.catchall ? Object.fromEntries(
|
|
24
|
+
[...formData.entries()].filter(([k]) => k.startsWith(prefix)).map(([k, v]) => [k.slice(prefix.length), v])
|
|
25
|
+
) : {};
|
|
26
|
+
for (const [key, baseValidator] of Object.entries(schema._zod.def.shape)) {
|
|
27
|
+
const prefixedKey = prefix + key;
|
|
28
|
+
let validator = baseValidator;
|
|
29
|
+
while (hasZodType(validator, "optional") || hasZodType(validator, "nullable") || hasZodType(validator, "default")) {
|
|
30
|
+
if (hasZodType(validator, "default") && !hasFormDataKeyOrNested(formKeys, prefixedKey)) {
|
|
31
|
+
obj[key] = validator._zod.def.defaultValue instanceof Function ? validator._zod.def.defaultValue() : validator._zod.def.defaultValue;
|
|
32
|
+
}
|
|
33
|
+
validator = validator._zod.def.innerType;
|
|
75
34
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
} else if (kind === "optional" || kind === "nullable" || kind === "pipe") {
|
|
90
|
-
child = childDef.innerType ?? childDef.in;
|
|
91
|
-
} else {
|
|
92
|
-
break;
|
|
35
|
+
while (hasZodType(validator, "pipe")) {
|
|
36
|
+
validator = validator._zod.def.in;
|
|
37
|
+
}
|
|
38
|
+
if (isDiscriminatedUnion(validator)) {
|
|
39
|
+
const typeKey = validator._zod.def.discriminator;
|
|
40
|
+
const typeValue = formData.get(prefixedKey + "." + typeKey);
|
|
41
|
+
if (typeof typeValue === "string") {
|
|
42
|
+
const match = validator._zod.def.options.find(
|
|
43
|
+
(option) => option.def.shape[typeKey].values.has(typeValue)
|
|
44
|
+
);
|
|
45
|
+
if (match) {
|
|
46
|
+
validator = match;
|
|
47
|
+
}
|
|
93
48
|
}
|
|
94
49
|
}
|
|
95
|
-
if (
|
|
96
|
-
|
|
97
|
-
const
|
|
98
|
-
if (
|
|
99
|
-
|
|
100
|
-
} else if (!(key in
|
|
101
|
-
|
|
50
|
+
if (hasZodType(validator, "object")) {
|
|
51
|
+
const nestedPrefix = prefixedKey + ".";
|
|
52
|
+
const hasNestedKeys = hasFormDataKeyWithPrefix(formKeys, nestedPrefix);
|
|
53
|
+
if (hasNestedKeys) {
|
|
54
|
+
obj[key] = formDataToObject(formData, validator, nestedPrefix, options);
|
|
55
|
+
} else if (!(key in obj)) {
|
|
56
|
+
obj[key] = hasZodType(baseValidator, "nullable") ? null : void 0;
|
|
102
57
|
}
|
|
103
|
-
} else if (
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
result[key] = variant ? convertObject(formData, variant, `${name}.`) : null;
|
|
113
|
-
} else if (schemaType(child) === "boolean") {
|
|
114
|
-
const value = formData.get(name);
|
|
115
|
-
result[key] = value === "true" ? true : value === "false" ? false : formData.has(name);
|
|
116
|
-
} else if (schemaType(child) === "array") {
|
|
117
|
-
const element = get(definition(child), "element");
|
|
118
|
-
result[key] = formData.getAll(name).map((value) => convertValue(value, element));
|
|
119
|
-
} else if (!(defaultValue !== void 0 && !formData.has(name))) {
|
|
120
|
-
const value = formData.get(name);
|
|
121
|
-
result[key] = value === null ? schemaType(original) === "optional" ? void 0 : null : convertValue(value, child);
|
|
58
|
+
} else if (!formData.has(prefixedKey) && key in obj) {
|
|
59
|
+
continue;
|
|
60
|
+
} else if (hasZodType(validator, "boolean")) {
|
|
61
|
+
const val = formData.get(prefixedKey);
|
|
62
|
+
obj[key] = isTrueFormValue(val) ? true : val === "false" ? false : formData.has(prefixedKey);
|
|
63
|
+
} else if (hasZodType(validator, "array")) {
|
|
64
|
+
obj[key] = handleFormDataGetAll(prefixedKey, formData, validator, options);
|
|
65
|
+
} else {
|
|
66
|
+
obj[key] = handleFormDataGet(prefixedKey, formData, validator, baseValidator, options);
|
|
122
67
|
}
|
|
123
68
|
}
|
|
124
|
-
return
|
|
69
|
+
return obj;
|
|
125
70
|
}
|
|
126
|
-
function
|
|
127
|
-
|
|
71
|
+
function handleFormDataGetAll(key, formData, validator, options) {
|
|
72
|
+
const entries = Array.from(formData.getAll(key));
|
|
73
|
+
const elementValidator = validator._zod.def.element;
|
|
74
|
+
if (hasZodType(elementValidator, "number")) {
|
|
75
|
+
return entries.map((value) => coerceFormNumber(value));
|
|
76
|
+
} else if (hasZodType(elementValidator, "boolean")) {
|
|
77
|
+
return entries.map(
|
|
78
|
+
(v) => isTrueFormValue(v) ? true : v === "false" ? false : Boolean(v)
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
if (options.convertEmptyStringsToNull && hasZodType(elementValidator, "string")) {
|
|
82
|
+
return entries.map((value) => value === "" ? null : value);
|
|
83
|
+
}
|
|
84
|
+
return entries;
|
|
128
85
|
}
|
|
129
|
-
function
|
|
130
|
-
const
|
|
131
|
-
|
|
86
|
+
function handleFormDataGet(key, formData, validator, baseValidator, options) {
|
|
87
|
+
const value = formData.get(key);
|
|
88
|
+
if (!value) {
|
|
89
|
+
return hasZodType(baseValidator, "optional") ? void 0 : null;
|
|
90
|
+
}
|
|
91
|
+
if (options.convertEmptyStringsToNull && value === "" && hasZodType(validator, "string")) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
return hasZodType(validator, "number") ? coerceFormNumber(value) : value;
|
|
132
95
|
}
|
|
133
|
-
function
|
|
134
|
-
if (
|
|
135
|
-
|
|
136
|
-
|
|
96
|
+
function unwrapBaseObjectSchema(schema, unparsedInput) {
|
|
97
|
+
if (hasZodType(schema, "pipe")) {
|
|
98
|
+
return unwrapBaseObjectSchema(schema._zod.def.in, unparsedInput);
|
|
99
|
+
}
|
|
100
|
+
if (isDiscriminatedUnion(schema)) {
|
|
101
|
+
const typeKey = schema._zod.def.discriminator;
|
|
102
|
+
const typeValue = unparsedInput.get(typeKey);
|
|
103
|
+
if (typeof typeValue !== "string") return schema;
|
|
104
|
+
const objSchema = schema._zod.def.options.find(
|
|
105
|
+
(option) => option.def.shape[typeKey].values.has(typeValue)
|
|
106
|
+
);
|
|
107
|
+
if (!objSchema) return schema;
|
|
108
|
+
return objSchema;
|
|
109
|
+
}
|
|
110
|
+
return schema;
|
|
137
111
|
}
|
|
138
112
|
|
|
139
113
|
export { zod as default };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@standard-community/standard-form",
|
|
3
3
|
"description": "Parse FormData with any Standard Schema validator.",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "dist/index.cjs",
|
|
@@ -47,21 +47,37 @@
|
|
|
47
47
|
"types": "./dist/vendors/zod.d.cts",
|
|
48
48
|
"default": "./dist/vendors/zod.cjs"
|
|
49
49
|
}
|
|
50
|
+
},
|
|
51
|
+
"./valibot": {
|
|
52
|
+
"import": {
|
|
53
|
+
"types": "./dist/vendors/valibot.d.ts",
|
|
54
|
+
"default": "./dist/vendors/valibot.js"
|
|
55
|
+
},
|
|
56
|
+
"require": {
|
|
57
|
+
"types": "./dist/vendors/valibot.d.cts",
|
|
58
|
+
"default": "./dist/vendors/valibot.cjs"
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"./arktype": {
|
|
62
|
+
"import": {
|
|
63
|
+
"types": "./dist/vendors/arktype.d.ts",
|
|
64
|
+
"default": "./dist/vendors/arktype.js"
|
|
65
|
+
},
|
|
66
|
+
"require": {
|
|
67
|
+
"types": "./dist/vendors/arktype.d.cts",
|
|
68
|
+
"default": "./dist/vendors/arktype.cjs"
|
|
69
|
+
}
|
|
50
70
|
}
|
|
51
71
|
},
|
|
52
72
|
"peerDependencies": {
|
|
53
|
-
"@standard-schema/spec": "^1.1.0"
|
|
54
|
-
"zod": "^4.3.6"
|
|
55
|
-
},
|
|
56
|
-
"peerDependenciesMeta": {
|
|
57
|
-
"zod": {
|
|
58
|
-
"optional": true
|
|
59
|
-
}
|
|
73
|
+
"@standard-schema/spec": "^1.1.0"
|
|
60
74
|
},
|
|
61
75
|
"devDependencies": {
|
|
62
76
|
"@standard-schema/spec": "^1.1.0",
|
|
77
|
+
"arktype": "^2.2.10",
|
|
63
78
|
"pkgroll": "^2.29.1",
|
|
64
79
|
"typescript": "6.0.3",
|
|
80
|
+
"valibot": "^1.5.0",
|
|
65
81
|
"vitest": "^5.1.1",
|
|
66
82
|
"zod": "^4.6.7"
|
|
67
83
|
},
|
package/dist/utils-BxGdbpqk.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
const validationMapper = /* @__PURE__ */ new Map();
|
|
2
|
-
class UnsupportedVendorError extends Error {
|
|
3
|
-
constructor(vendor) {
|
|
4
|
-
super(`standard-form: Unsupported schema vendor "${vendor}".`);
|
|
5
|
-
}
|
|
6
|
-
}
|
|
7
|
-
class MissingDependencyError extends Error {
|
|
8
|
-
constructor(packageName) {
|
|
9
|
-
super(`standard-form: Missing dependencies "${packageName}".`);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
class UnsupportedSchemaError extends Error {
|
|
13
|
-
constructor(vendor, requirement) {
|
|
14
|
-
super(`standard-form: Unsupported schema for vendor "${vendor}". ${requirement}.`);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export { MissingDependencyError as M, UnsupportedVendorError as U, UnsupportedSchemaError as a, validationMapper as v };
|
package/dist/utils-f6FAxdZk.cjs
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const validationMapper = /* @__PURE__ */ new Map();
|
|
4
|
-
class UnsupportedVendorError extends Error {
|
|
5
|
-
constructor(vendor) {
|
|
6
|
-
super(`standard-form: Unsupported schema vendor "${vendor}".`);
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
class MissingDependencyError extends Error {
|
|
10
|
-
constructor(packageName) {
|
|
11
|
-
super(`standard-form: Missing dependencies "${packageName}".`);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
class UnsupportedSchemaError extends Error {
|
|
15
|
-
constructor(vendor, requirement) {
|
|
16
|
-
super(`standard-form: Unsupported schema for vendor "${vendor}". ${requirement}.`);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
exports.MissingDependencyError = MissingDependencyError;
|
|
21
|
-
exports.UnsupportedSchemaError = UnsupportedSchemaError;
|
|
22
|
-
exports.UnsupportedVendorError = UnsupportedVendorError;
|
|
23
|
-
exports.validationMapper = validationMapper;
|