@standard-community/standard-form 0.0.0-stage → 0.0.1
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 +46 -2
- package/dist/index.cjs +37 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +30 -0
- package/dist/utils-BxGdbpqk.js +18 -0
- package/dist/utils-QJKenv_Z.d.cts +25 -0
- package/dist/utils-QJKenv_Z.d.ts +25 -0
- package/dist/utils-f6FAxdZk.cjs +23 -0
- package/dist/vendors/zod.cjs +141 -0
- package/dist/vendors/zod.d.cts +5 -0
- package/dist/vendors/zod.d.ts +5 -0
- package/dist/vendors/zod.js +139 -0
- package/package.json +70 -3
package/README.md
CHANGED
|
@@ -1,3 +1,47 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @standard-community/standard-form
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Parse [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) with any [Standard Schema](https://standardschema.dev) validator.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { parseFormData } from '@standard-community/standard-form';
|
|
7
|
+
import { z } from 'zod';
|
|
8
|
+
|
|
9
|
+
const schema = z.object({
|
|
10
|
+
name: z.string(),
|
|
11
|
+
age: z.number(),
|
|
12
|
+
newsletter: z.boolean(),
|
|
13
|
+
address: z.object({ city: z.string() }),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const result = await parseFormData(schema, formData);
|
|
17
|
+
if (result.issues) {
|
|
18
|
+
handleIssues(result.issues);
|
|
19
|
+
} else {
|
|
20
|
+
result.value;
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
`FormData` is flat and stringly typed, and the Standard Schema specification covers validation but not schema introspection. Each validator therefore provides its own handler for coercion. Validation uses the Standard Schema interface.
|
|
25
|
+
|
|
26
|
+
Handlers are resolved from the schema's `~standard.vendor` and imported lazily, so a project only needs to install the validators it uses.
|
|
27
|
+
|
|
28
|
+
| Vendor | Support |
|
|
29
|
+
| ------ | ------- |
|
|
30
|
+
| `zod` | Built in for Zod 4 and higher |
|
|
31
|
+
|
|
32
|
+
Other vendors throw an `UnsupportedVendorError` until you register a handler:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { loadVendor } from '@standard-community/standard-form';
|
|
36
|
+
|
|
37
|
+
loadVendor('valibot', (_schema, formData) => Object.fromEntries(formData.entries()));
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
`loadVendor` also replaces a built-in handler:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { loadVendor } from '@standard-community/standard-form';
|
|
44
|
+
import zodHandler from '@standard-community/standard-form/zod';
|
|
45
|
+
|
|
46
|
+
loadVendor('zod', await zodHandler());
|
|
47
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('./utils-f6FAxdZk.cjs');
|
|
4
|
+
|
|
5
|
+
function _interopNamespaceDefaultOnly (e) { return Object.freeze({ __proto__: null, default: e }); }
|
|
6
|
+
|
|
7
|
+
const getToFormInputFn = async (vendor) => {
|
|
8
|
+
const cached = utils.validationMapper.get(vendor);
|
|
9
|
+
if (cached) {
|
|
10
|
+
return cached;
|
|
11
|
+
}
|
|
12
|
+
let vendorFnPromise;
|
|
13
|
+
switch (vendor) {
|
|
14
|
+
case "zod":
|
|
15
|
+
vendorFnPromise = (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefaultOnly(require('./vendors/zod.cjs')); })).default();
|
|
16
|
+
break;
|
|
17
|
+
default:
|
|
18
|
+
throw new utils.UnsupportedVendorError(vendor);
|
|
19
|
+
}
|
|
20
|
+
const vendorFn = await vendorFnPromise;
|
|
21
|
+
utils.validationMapper.set(vendor, vendorFn);
|
|
22
|
+
return vendorFn;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
async function parseFormData(schema, formData) {
|
|
26
|
+
const fn = await getToFormInputFn(schema["~standard"].vendor);
|
|
27
|
+
return await schema["~standard"].validate(fn(schema, formData));
|
|
28
|
+
}
|
|
29
|
+
function loadVendor(vendor, fn) {
|
|
30
|
+
utils.validationMapper.set(vendor, fn);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
exports.MissingDependencyError = utils.MissingDependencyError;
|
|
34
|
+
exports.UnsupportedSchemaError = utils.UnsupportedSchemaError;
|
|
35
|
+
exports.UnsupportedVendorError = utils.UnsupportedVendorError;
|
|
36
|
+
exports.loadVendor = loadVendor;
|
|
37
|
+
exports.parseFormData = parseFormData;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import { T as ToFormInputFn } from './utils-QJKenv_Z.cjs';
|
|
3
|
+
export { M as MissingDependencyError, U as UnsupportedSchemaError, a as UnsupportedVendorError } from './utils-QJKenv_Z.cjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Validates `FormData` against a Standard Schema.
|
|
7
|
+
*
|
|
8
|
+
* `FormData` is flat and stringly typed, so the entries are first coerced into the shape
|
|
9
|
+
* the schema expects — nested objects from dotted keys, numbers, booleans, arrays,
|
|
10
|
+
* defaults — by the handler for the schema's validator, then handed to the schema itself.
|
|
11
|
+
*/
|
|
12
|
+
declare function parseFormData<TSchema extends StandardSchemaV1>(schema: TSchema, formData: FormData): Promise<StandardSchemaV1.Result<StandardSchemaV1.InferOutput<TSchema>>>;
|
|
13
|
+
declare function loadVendor(vendor: string, fn: ToFormInputFn): void;
|
|
14
|
+
|
|
15
|
+
export { ToFormInputFn, loadVendor, parseFormData };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import { T as ToFormInputFn } from './utils-QJKenv_Z.js';
|
|
3
|
+
export { M as MissingDependencyError, U as UnsupportedSchemaError, a as UnsupportedVendorError } from './utils-QJKenv_Z.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Validates `FormData` against a Standard Schema.
|
|
7
|
+
*
|
|
8
|
+
* `FormData` is flat and stringly typed, so the entries are first coerced into the shape
|
|
9
|
+
* the schema expects — nested objects from dotted keys, numbers, booleans, arrays,
|
|
10
|
+
* defaults — by the handler for the schema's validator, then handed to the schema itself.
|
|
11
|
+
*/
|
|
12
|
+
declare function parseFormData<TSchema extends StandardSchemaV1>(schema: TSchema, formData: FormData): Promise<StandardSchemaV1.Result<StandardSchemaV1.InferOutput<TSchema>>>;
|
|
13
|
+
declare function loadVendor(vendor: string, fn: ToFormInputFn): void;
|
|
14
|
+
|
|
15
|
+
export { ToFormInputFn, loadVendor, parseFormData };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { v as validationMapper, U as UnsupportedVendorError } from './utils-BxGdbpqk.js';
|
|
2
|
+
export { M as MissingDependencyError, a as UnsupportedSchemaError } from './utils-BxGdbpqk.js';
|
|
3
|
+
|
|
4
|
+
const getToFormInputFn = async (vendor) => {
|
|
5
|
+
const cached = validationMapper.get(vendor);
|
|
6
|
+
if (cached) {
|
|
7
|
+
return cached;
|
|
8
|
+
}
|
|
9
|
+
let vendorFnPromise;
|
|
10
|
+
switch (vendor) {
|
|
11
|
+
case "zod":
|
|
12
|
+
vendorFnPromise = (await import('./vendors/zod.js')).default();
|
|
13
|
+
break;
|
|
14
|
+
default:
|
|
15
|
+
throw new UnsupportedVendorError(vendor);
|
|
16
|
+
}
|
|
17
|
+
const vendorFn = await vendorFnPromise;
|
|
18
|
+
validationMapper.set(vendor, vendorFn);
|
|
19
|
+
return vendorFn;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
async function parseFormData(schema, formData) {
|
|
23
|
+
const fn = await getToFormInputFn(schema["~standard"].vendor);
|
|
24
|
+
return await schema["~standard"].validate(fn(schema, formData));
|
|
25
|
+
}
|
|
26
|
+
function loadVendor(vendor, fn) {
|
|
27
|
+
validationMapper.set(vendor, fn);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { UnsupportedVendorError, loadVendor, parseFormData };
|
|
@@ -0,0 +1,18 @@
|
|
|
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 };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns `formData` into a value the validator's schema can validate.
|
|
3
|
+
*
|
|
4
|
+
* The Standard Schema spec covers validation, but not introspection: there is no
|
|
5
|
+
* portable way to ask a schema "is this field a number?", which is exactly what turning
|
|
6
|
+
* the flat, stringly typed shape of `FormData` into the value a schema expects requires.
|
|
7
|
+
* Each validator therefore contributes its own coercion step, and the spec takes over
|
|
8
|
+
* from there.
|
|
9
|
+
*
|
|
10
|
+
* Implementations should return `formData` untouched when handed a schema they cannot
|
|
11
|
+
* introspect, so that validation — rather than the coercion — reports the mismatch.
|
|
12
|
+
*/
|
|
13
|
+
type ToFormInputFn = (schema: unknown, formData: FormData) => unknown;
|
|
14
|
+
declare class UnsupportedVendorError extends Error {
|
|
15
|
+
constructor(vendor: string);
|
|
16
|
+
}
|
|
17
|
+
declare class MissingDependencyError extends Error {
|
|
18
|
+
constructor(packageName: string);
|
|
19
|
+
}
|
|
20
|
+
declare class UnsupportedSchemaError extends Error {
|
|
21
|
+
constructor(vendor: string, requirement: string);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { MissingDependencyError as M, UnsupportedSchemaError as U, UnsupportedVendorError as a };
|
|
25
|
+
export type { ToFormInputFn as T };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turns `formData` into a value the validator's schema can validate.
|
|
3
|
+
*
|
|
4
|
+
* The Standard Schema spec covers validation, but not introspection: there is no
|
|
5
|
+
* portable way to ask a schema "is this field a number?", which is exactly what turning
|
|
6
|
+
* the flat, stringly typed shape of `FormData` into the value a schema expects requires.
|
|
7
|
+
* Each validator therefore contributes its own coercion step, and the spec takes over
|
|
8
|
+
* from there.
|
|
9
|
+
*
|
|
10
|
+
* Implementations should return `formData` untouched when handed a schema they cannot
|
|
11
|
+
* introspect, so that validation — rather than the coercion — reports the mismatch.
|
|
12
|
+
*/
|
|
13
|
+
type ToFormInputFn = (schema: unknown, formData: FormData) => unknown;
|
|
14
|
+
declare class UnsupportedVendorError extends Error {
|
|
15
|
+
constructor(vendor: string);
|
|
16
|
+
}
|
|
17
|
+
declare class MissingDependencyError extends Error {
|
|
18
|
+
constructor(packageName: string);
|
|
19
|
+
}
|
|
20
|
+
declare class UnsupportedSchemaError extends Error {
|
|
21
|
+
constructor(vendor: string, requirement: string);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { MissingDependencyError as M, UnsupportedSchemaError as U, UnsupportedVendorError as a };
|
|
25
|
+
export type { ToFormInputFn as T };
|
|
@@ -0,0 +1,23 @@
|
|
|
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;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('../utils-f6FAxdZk.cjs');
|
|
4
|
+
|
|
5
|
+
async function zod() {
|
|
6
|
+
return (schema, formData) => {
|
|
7
|
+
if (!isSchema(schema)) {
|
|
8
|
+
throw new utils.UnsupportedSchemaError("zod", "Zod 4 and higher is required");
|
|
9
|
+
}
|
|
10
|
+
const objectSchema = getObjectSchema(schema, formData);
|
|
11
|
+
return objectSchema ? convertObject(formData, objectSchema) : formData;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function isRecord(value) {
|
|
15
|
+
return typeof value === "object" && value !== null;
|
|
16
|
+
}
|
|
17
|
+
function get(value, key) {
|
|
18
|
+
return isRecord(value) ? value[key] : void 0;
|
|
19
|
+
}
|
|
20
|
+
function definition(schema) {
|
|
21
|
+
const internal = get(schema, "_zod");
|
|
22
|
+
const def = get(internal, "def");
|
|
23
|
+
return isRecord(def) ? def : void 0;
|
|
24
|
+
}
|
|
25
|
+
function isSchema(value) {
|
|
26
|
+
return typeof get(definition(value), "type") === "string";
|
|
27
|
+
}
|
|
28
|
+
function schemaType(schema) {
|
|
29
|
+
const type = get(definition(schema), "type");
|
|
30
|
+
return typeof type === "string" ? type : void 0;
|
|
31
|
+
}
|
|
32
|
+
function unwrap(schema) {
|
|
33
|
+
while (true) {
|
|
34
|
+
const kind = schemaType(schema);
|
|
35
|
+
const def = definition(schema);
|
|
36
|
+
if (!def) return schema;
|
|
37
|
+
if (kind === "optional" || kind === "nullable" || kind === "default") {
|
|
38
|
+
schema = def.innerType;
|
|
39
|
+
} else if (kind === "pipe") {
|
|
40
|
+
schema = def.in;
|
|
41
|
+
} else {
|
|
42
|
+
return schema;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function isDiscriminatedUnion(schema) {
|
|
47
|
+
return schemaType(schema) === "union" && typeof get(definition(schema), "discriminator") === "string";
|
|
48
|
+
}
|
|
49
|
+
function objectShape(schema) {
|
|
50
|
+
const shape = get(definition(schema), "shape");
|
|
51
|
+
return isRecord(shape) ? shape : void 0;
|
|
52
|
+
}
|
|
53
|
+
function getObjectSchema(schema, formData) {
|
|
54
|
+
const base = unwrap(schema);
|
|
55
|
+
if (schemaType(base) === "object") return base;
|
|
56
|
+
if (!isDiscriminatedUnion(base)) return;
|
|
57
|
+
const def = definition(base);
|
|
58
|
+
if (!def) return;
|
|
59
|
+
const discriminator = def.discriminator;
|
|
60
|
+
if (typeof discriminator !== "string") return;
|
|
61
|
+
const value = formData.get(discriminator);
|
|
62
|
+
if (typeof value !== "string" || !Array.isArray(def.options)) return;
|
|
63
|
+
return def.options.find((option) => {
|
|
64
|
+
const literal = objectShape(option)?.[discriminator];
|
|
65
|
+
return schemaType(literal) === "literal" && containsLiteral(literal, value);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
function convertObject(formData, schema, prefix = "") {
|
|
69
|
+
const result = {};
|
|
70
|
+
const keys = [...formData.keys()];
|
|
71
|
+
const def = definition(schema);
|
|
72
|
+
const shape = objectShape(schema);
|
|
73
|
+
if (!def || !shape) return result;
|
|
74
|
+
if (def.catchall) {
|
|
75
|
+
for (const [key, value] of formData.entries()) {
|
|
76
|
+
if (key.startsWith(prefix)) result[key.slice(prefix.length)] = value;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
for (const [key, original] of Object.entries(shape)) {
|
|
80
|
+
const name = prefix + key;
|
|
81
|
+
let child = original;
|
|
82
|
+
let defaultValue;
|
|
83
|
+
while (isSchema(child)) {
|
|
84
|
+
const kind = schemaType(child);
|
|
85
|
+
const childDef = definition(child);
|
|
86
|
+
if (!childDef) break;
|
|
87
|
+
if (kind === "default") {
|
|
88
|
+
const value = childDef.defaultValue;
|
|
89
|
+
defaultValue = typeof value === "function" ? value() : value;
|
|
90
|
+
child = childDef.innerType;
|
|
91
|
+
} else if (kind === "optional" || kind === "nullable" || kind === "pipe") {
|
|
92
|
+
child = childDef.innerType ?? childDef.in;
|
|
93
|
+
} else {
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (defaultValue !== void 0 && !hasKeyOrChild(keys, name)) result[key] = defaultValue;
|
|
98
|
+
if (schemaType(child) === "object") {
|
|
99
|
+
const nestedPrefix = `${name}.`;
|
|
100
|
+
if (keys.some((formKey) => formKey.startsWith(nestedPrefix))) {
|
|
101
|
+
result[key] = convertObject(formData, child, nestedPrefix);
|
|
102
|
+
} else if (!(key in result)) {
|
|
103
|
+
result[key] = schemaType(original) === "nullable" ? null : void 0;
|
|
104
|
+
}
|
|
105
|
+
} else if (isDiscriminatedUnion(child)) {
|
|
106
|
+
const union = definition(child);
|
|
107
|
+
const discriminator = union?.discriminator;
|
|
108
|
+
const value = typeof discriminator === "string" ? formData.get(`${name}.${discriminator}`) : null;
|
|
109
|
+
const options = union?.options;
|
|
110
|
+
const variant = Array.isArray(options) && typeof discriminator === "string" ? options.find((option) => {
|
|
111
|
+
const literal = objectShape(option)?.[discriminator];
|
|
112
|
+
return schemaType(literal) === "literal" && containsLiteral(literal, value);
|
|
113
|
+
}) : void 0;
|
|
114
|
+
result[key] = variant ? convertObject(formData, variant, `${name}.`) : null;
|
|
115
|
+
} else if (schemaType(child) === "boolean") {
|
|
116
|
+
const value = formData.get(name);
|
|
117
|
+
result[key] = value === "true" ? true : value === "false" ? false : formData.has(name);
|
|
118
|
+
} else if (schemaType(child) === "array") {
|
|
119
|
+
const element = get(definition(child), "element");
|
|
120
|
+
result[key] = formData.getAll(name).map((value) => convertValue(value, element));
|
|
121
|
+
} else if (!(defaultValue !== void 0 && !formData.has(name))) {
|
|
122
|
+
const value = formData.get(name);
|
|
123
|
+
result[key] = value === null ? schemaType(original) === "optional" ? void 0 : null : convertValue(value, child);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return result;
|
|
127
|
+
}
|
|
128
|
+
function hasKeyOrChild(keys, name) {
|
|
129
|
+
return keys.some((key) => key === name || key.startsWith(`${name}.`));
|
|
130
|
+
}
|
|
131
|
+
function containsLiteral(schema, value) {
|
|
132
|
+
const values = get(definition(schema), "values");
|
|
133
|
+
return Array.isArray(values) ? values.includes(value) : values instanceof Set && values.has(value);
|
|
134
|
+
}
|
|
135
|
+
function convertValue(value, schema) {
|
|
136
|
+
if (schemaType(schema) === "number") return value === "" ? value : Number(value);
|
|
137
|
+
if (schemaType(schema) === "boolean") return value === "true";
|
|
138
|
+
return value;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
module.exports = zod;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { a as UnsupportedSchemaError } from '../utils-BxGdbpqk.js';
|
|
2
|
+
|
|
3
|
+
async function zod() {
|
|
4
|
+
return (schema, formData) => {
|
|
5
|
+
if (!isSchema(schema)) {
|
|
6
|
+
throw new UnsupportedSchemaError("zod", "Zod 4 and higher is required");
|
|
7
|
+
}
|
|
8
|
+
const objectSchema = getObjectSchema(schema, formData);
|
|
9
|
+
return objectSchema ? convertObject(formData, objectSchema) : formData;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function isRecord(value) {
|
|
13
|
+
return typeof value === "object" && value !== null;
|
|
14
|
+
}
|
|
15
|
+
function get(value, key) {
|
|
16
|
+
return isRecord(value) ? value[key] : void 0;
|
|
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
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function isDiscriminatedUnion(schema) {
|
|
45
|
+
return schemaType(schema) === "union" && typeof get(definition(schema), "discriminator") === "string";
|
|
46
|
+
}
|
|
47
|
+
function objectShape(schema) {
|
|
48
|
+
const shape = get(definition(schema), "shape");
|
|
49
|
+
return isRecord(shape) ? shape : void 0;
|
|
50
|
+
}
|
|
51
|
+
function getObjectSchema(schema, formData) {
|
|
52
|
+
const base = unwrap(schema);
|
|
53
|
+
if (schemaType(base) === "object") return base;
|
|
54
|
+
if (!isDiscriminatedUnion(base)) return;
|
|
55
|
+
const def = definition(base);
|
|
56
|
+
if (!def) return;
|
|
57
|
+
const discriminator = def.discriminator;
|
|
58
|
+
if (typeof discriminator !== "string") return;
|
|
59
|
+
const value = formData.get(discriminator);
|
|
60
|
+
if (typeof value !== "string" || !Array.isArray(def.options)) return;
|
|
61
|
+
return def.options.find((option) => {
|
|
62
|
+
const literal = objectShape(option)?.[discriminator];
|
|
63
|
+
return schemaType(literal) === "literal" && containsLiteral(literal, value);
|
|
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;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
for (const [key, original] of Object.entries(shape)) {
|
|
78
|
+
const name = prefix + key;
|
|
79
|
+
let child = original;
|
|
80
|
+
let defaultValue;
|
|
81
|
+
while (isSchema(child)) {
|
|
82
|
+
const kind = schemaType(child);
|
|
83
|
+
const childDef = definition(child);
|
|
84
|
+
if (!childDef) break;
|
|
85
|
+
if (kind === "default") {
|
|
86
|
+
const value = childDef.defaultValue;
|
|
87
|
+
defaultValue = typeof value === "function" ? value() : value;
|
|
88
|
+
child = childDef.innerType;
|
|
89
|
+
} else if (kind === "optional" || kind === "nullable" || kind === "pipe") {
|
|
90
|
+
child = childDef.innerType ?? childDef.in;
|
|
91
|
+
} else {
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (defaultValue !== void 0 && !hasKeyOrChild(keys, name)) result[key] = defaultValue;
|
|
96
|
+
if (schemaType(child) === "object") {
|
|
97
|
+
const nestedPrefix = `${name}.`;
|
|
98
|
+
if (keys.some((formKey) => formKey.startsWith(nestedPrefix))) {
|
|
99
|
+
result[key] = convertObject(formData, child, nestedPrefix);
|
|
100
|
+
} else if (!(key in result)) {
|
|
101
|
+
result[key] = schemaType(original) === "nullable" ? null : void 0;
|
|
102
|
+
}
|
|
103
|
+
} else if (isDiscriminatedUnion(child)) {
|
|
104
|
+
const union = definition(child);
|
|
105
|
+
const discriminator = union?.discriminator;
|
|
106
|
+
const value = typeof discriminator === "string" ? formData.get(`${name}.${discriminator}`) : null;
|
|
107
|
+
const options = union?.options;
|
|
108
|
+
const variant = Array.isArray(options) && typeof discriminator === "string" ? options.find((option) => {
|
|
109
|
+
const literal = objectShape(option)?.[discriminator];
|
|
110
|
+
return schemaType(literal) === "literal" && containsLiteral(literal, value);
|
|
111
|
+
}) : void 0;
|
|
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);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
126
|
+
function hasKeyOrChild(keys, name) {
|
|
127
|
+
return keys.some((key) => key === name || key.startsWith(`${name}.`));
|
|
128
|
+
}
|
|
129
|
+
function containsLiteral(schema, value) {
|
|
130
|
+
const values = get(definition(schema), "values");
|
|
131
|
+
return Array.isArray(values) ? values.includes(value) : values instanceof Set && values.has(value);
|
|
132
|
+
}
|
|
133
|
+
function convertValue(value, schema) {
|
|
134
|
+
if (schemaType(schema) === "number") return value === "" ? value : Number(value);
|
|
135
|
+
if (schemaType(schema) === "boolean") return value === "true";
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export { zod as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,73 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@standard-community/standard-form",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"description": "Parse FormData with any Standard Schema validator.",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "dist/index.cjs",
|
|
8
|
+
"module": "dist/index.js",
|
|
9
|
+
"types": "dist/index.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"keywords": [
|
|
14
|
+
"standard-schema",
|
|
15
|
+
"standard-community",
|
|
16
|
+
"formdata",
|
|
17
|
+
"form"
|
|
18
|
+
],
|
|
19
|
+
"homepage": "https://github.com/standard-community",
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/standard-community/standard-form.git"
|
|
26
|
+
},
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/standard-community/standard-form/issues"
|
|
29
|
+
},
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"import": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"default": "./dist/index.js"
|
|
35
|
+
},
|
|
36
|
+
"require": {
|
|
37
|
+
"types": "./dist/index.d.cts",
|
|
38
|
+
"default": "./dist/index.cjs"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"./zod": {
|
|
42
|
+
"import": {
|
|
43
|
+
"types": "./dist/vendors/zod.d.ts",
|
|
44
|
+
"default": "./dist/vendors/zod.js"
|
|
45
|
+
},
|
|
46
|
+
"require": {
|
|
47
|
+
"types": "./dist/vendors/zod.d.cts",
|
|
48
|
+
"default": "./dist/vendors/zod.cjs"
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"peerDependencies": {
|
|
53
|
+
"@standard-schema/spec": "^1.1.0",
|
|
54
|
+
"zod": "^4.3.6"
|
|
55
|
+
},
|
|
56
|
+
"peerDependenciesMeta": {
|
|
57
|
+
"zod": {
|
|
58
|
+
"optional": true
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@standard-schema/spec": "^1.1.0",
|
|
63
|
+
"pkgroll": "^2.29.1",
|
|
64
|
+
"typescript": "6.0.3",
|
|
65
|
+
"vitest": "^5.1.1",
|
|
66
|
+
"zod": "^4.6.7"
|
|
67
|
+
},
|
|
68
|
+
"scripts": {
|
|
69
|
+
"build": "pnpm typecheck && pkgroll --clean-dist",
|
|
70
|
+
"typecheck": "tsc --noEmit",
|
|
71
|
+
"test": "pnpm build && vitest run"
|
|
72
|
+
}
|
|
6
73
|
}
|