@standard-community/standard-form 0.0.0-stage → 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 +98 -2
- package/dist/index.cjs +43 -0
- package/dist/index.d.cts +15 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +36 -0
- package/dist/utils-BoRc22Ql.cjs +45 -0
- package/dist/utils-Cr64FEDS.js +36 -0
- package/dist/utils-DihtL_2q.d.cts +28 -0
- package/dist/utils-DihtL_2q.d.ts +28 -0
- 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 +115 -0
- package/dist/vendors/zod.d.cts +5 -0
- package/dist/vendors/zod.d.ts +5 -0
- package/dist/vendors/zod.js +113 -0
- package/package.json +86 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Standard Schema Community
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,99 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @standard-community/standard-form
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Parse browser [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) with any [Standard Schema](https://standardschema.dev)-compatible validator.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
Install this package and one supported validator:
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @standard-community/standard-form zod
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
yarn add @standard-community/standard-form zod
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
pnpm add @standard-community/standard-form zod
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
The built-in adapters support Zod 4 and higher, Valibot, and ArkType. Install only the validator your project uses.
|
|
22
|
+
|
|
23
|
+
## Parse form data
|
|
24
|
+
|
|
25
|
+
`FormData` stores string values and repeated keys. `parseFormData` uses the schema's validator adapter to build the input shape, coerce values, and then validate the result.
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { parseFormData } from '@standard-community/standard-form';
|
|
29
|
+
import { z } from 'zod';
|
|
30
|
+
|
|
31
|
+
const schema = z.object({
|
|
32
|
+
name: z.string(),
|
|
33
|
+
age: z.number(),
|
|
34
|
+
newsletter: z.boolean(),
|
|
35
|
+
address: z.object({ city: z.string() }),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const formData = new FormData();
|
|
39
|
+
formData.set('name', 'Ben');
|
|
40
|
+
formData.set('age', '32');
|
|
41
|
+
formData.set('newsletter', 'true');
|
|
42
|
+
formData.set('address.city', 'Paris');
|
|
43
|
+
|
|
44
|
+
const result = await parseFormData(schema, formData);
|
|
45
|
+
if (result.issues) {
|
|
46
|
+
handleIssues(result.issues);
|
|
47
|
+
} else {
|
|
48
|
+
result.value;
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Adapters handle these form values:
|
|
53
|
+
|
|
54
|
+
- Numeric strings become numbers.
|
|
55
|
+
- Boolean strings become booleans.
|
|
56
|
+
- Checkbox values `"on"`, `"1"`, `"yes"`, and `"true"` become `true`. An omitted checkbox field becomes `false`.
|
|
57
|
+
- Dotted names such as `address.city` become nested objects.
|
|
58
|
+
- Repeated names become array values.
|
|
59
|
+
|
|
60
|
+
## Convert empty strings to null
|
|
61
|
+
|
|
62
|
+
Pass an `Options` object as the third argument to convert empty values for schema-declared string fields to `null` before validation. This applies to nested string fields and string array items. It does not modify unknown fields or non-object inputs such as `FormData`.
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { parseFormData } from '@standard-community/standard-form';
|
|
66
|
+
import type { Options } from '@standard-community/standard-form';
|
|
67
|
+
|
|
68
|
+
const options: Options = { convertEmptyStringsToNull: true };
|
|
69
|
+
const result = await parseFormData(schema, formData, options);
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Register a vendor handler
|
|
73
|
+
|
|
74
|
+
The package includes built-in handlers for these validators:
|
|
75
|
+
|
|
76
|
+
| Validator | Support |
|
|
77
|
+
| --- | --- |
|
|
78
|
+
| Zod | Version 4 and higher |
|
|
79
|
+
| Valibot | Built in |
|
|
80
|
+
| ArkType | Built in |
|
|
81
|
+
|
|
82
|
+
For another Standard Schema validator, register a handler with `loadVendor`. If you want built-in support for a validator, [open an issue](https://github.com/standard-community/standard-form/issues):
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import { loadVendor } from '@standard-community/standard-form';
|
|
86
|
+
|
|
87
|
+
loadVendor('custom', (schema, formData, options) => {
|
|
88
|
+
return Object.fromEntries(formData.entries());
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The handler receives the schema, the original `FormData`, and the `Options` object. Implement schema-specific coercion in the handler. `loadVendor` can also replace a built-in handler:
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { loadVendor } from '@standard-community/standard-form';
|
|
96
|
+
import zodHandler from '@standard-community/standard-form/zod';
|
|
97
|
+
|
|
98
|
+
loadVendor('zod', await zodHandler());
|
|
99
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('./utils-BoRc22Ql.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
|
+
case "valibot":
|
|
18
|
+
vendorFnPromise = (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefaultOnly(require('./vendors/valibot.cjs')); })).default();
|
|
19
|
+
break;
|
|
20
|
+
case "arktype":
|
|
21
|
+
vendorFnPromise = (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefaultOnly(require('./vendors/arktype.cjs')); })).default();
|
|
22
|
+
break;
|
|
23
|
+
default:
|
|
24
|
+
throw new utils.UnsupportedVendorError(vendor);
|
|
25
|
+
}
|
|
26
|
+
const vendorFn = await vendorFnPromise;
|
|
27
|
+
utils.validationMapper.set(vendor, vendorFn);
|
|
28
|
+
return vendorFn;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
async function parseFormData(schema, formData, options = {}) {
|
|
32
|
+
const fn = await getToFormInputFn(schema["~standard"].vendor);
|
|
33
|
+
return await schema["~standard"].validate(fn(schema, formData, options));
|
|
34
|
+
}
|
|
35
|
+
function loadVendor(vendor, fn) {
|
|
36
|
+
utils.validationMapper.set(vendor, fn);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
exports.MissingDependencyError = utils.MissingDependencyError;
|
|
40
|
+
exports.UnsupportedSchemaError = utils.UnsupportedSchemaError;
|
|
41
|
+
exports.UnsupportedVendorError = utils.UnsupportedVendorError;
|
|
42
|
+
exports.loadVendor = loadVendor;
|
|
43
|
+
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, O as Options } from './utils-DihtL_2q.cjs';
|
|
3
|
+
export { M as MissingDependencyError, U as UnsupportedSchemaError, a as UnsupportedVendorError } from './utils-DihtL_2q.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, options?: Options): Promise<StandardSchemaV1.Result<StandardSchemaV1.InferOutput<TSchema>>>;
|
|
13
|
+
declare function loadVendor(vendor: string, fn: ToFormInputFn): void;
|
|
14
|
+
|
|
15
|
+
export { Options, 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, O as Options } from './utils-DihtL_2q.js';
|
|
3
|
+
export { M as MissingDependencyError, U as UnsupportedSchemaError, a as UnsupportedVendorError } from './utils-DihtL_2q.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, options?: Options): Promise<StandardSchemaV1.Result<StandardSchemaV1.InferOutput<TSchema>>>;
|
|
13
|
+
declare function loadVendor(vendor: string, fn: ToFormInputFn): void;
|
|
14
|
+
|
|
15
|
+
export { Options, ToFormInputFn, loadVendor, parseFormData };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { v as validationMapper, U as UnsupportedVendorError } from './utils-Cr64FEDS.js';
|
|
2
|
+
export { M as MissingDependencyError, a as UnsupportedSchemaError } from './utils-Cr64FEDS.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
|
+
case "valibot":
|
|
15
|
+
vendorFnPromise = (await import('./vendors/valibot.js')).default();
|
|
16
|
+
break;
|
|
17
|
+
case "arktype":
|
|
18
|
+
vendorFnPromise = (await import('./vendors/arktype.js')).default();
|
|
19
|
+
break;
|
|
20
|
+
default:
|
|
21
|
+
throw new UnsupportedVendorError(vendor);
|
|
22
|
+
}
|
|
23
|
+
const vendorFn = await vendorFnPromise;
|
|
24
|
+
validationMapper.set(vendor, vendorFn);
|
|
25
|
+
return vendorFn;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
async function parseFormData(schema, formData, options = {}) {
|
|
29
|
+
const fn = await getToFormInputFn(schema["~standard"].vendor);
|
|
30
|
+
return await schema["~standard"].validate(fn(schema, formData, options));
|
|
31
|
+
}
|
|
32
|
+
function loadVendor(vendor, fn) {
|
|
33
|
+
validationMapper.set(vendor, fn);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { UnsupportedVendorError, loadVendor, parseFormData };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const trueFormValues = /* @__PURE__ */ new Set(["on", "1", "yes", "true"]);
|
|
4
|
+
function isTrueFormValue(value) {
|
|
5
|
+
return typeof value === "string" && trueFormValues.has(value);
|
|
6
|
+
}
|
|
7
|
+
function coerceFormNumber(value) {
|
|
8
|
+
return value === null || value === "" ? value : Number(value);
|
|
9
|
+
}
|
|
10
|
+
function hasFormDataKeyWithPrefix(keys, prefix) {
|
|
11
|
+
for (const key of keys) {
|
|
12
|
+
if (key.startsWith(prefix)) return true;
|
|
13
|
+
}
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
function hasFormDataKeyOrNested(keys, key) {
|
|
17
|
+
return hasFormDataKeyWithPrefix(keys, `${key}.`) || hasFormDataKeyWithPrefix(keys, key);
|
|
18
|
+
}
|
|
19
|
+
const validationMapper = /* @__PURE__ */ new Map();
|
|
20
|
+
class UnsupportedVendorError extends Error {
|
|
21
|
+
constructor(vendor) {
|
|
22
|
+
super(
|
|
23
|
+
`standard-form: Unsupported schema vendor "${vendor}". Request built-in support at https://github.com/standard-community/standard-form/issues or register a handler with loadVendor().`
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
class MissingDependencyError extends Error {
|
|
28
|
+
constructor(packageName) {
|
|
29
|
+
super(`standard-form: Missing dependencies "${packageName}".`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
class UnsupportedSchemaError extends Error {
|
|
33
|
+
constructor(vendor, requirement) {
|
|
34
|
+
super(`standard-form: Unsupported schema for vendor "${vendor}". ${requirement}.`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
exports.MissingDependencyError = MissingDependencyError;
|
|
39
|
+
exports.UnsupportedSchemaError = UnsupportedSchemaError;
|
|
40
|
+
exports.UnsupportedVendorError = UnsupportedVendorError;
|
|
41
|
+
exports.coerceFormNumber = coerceFormNumber;
|
|
42
|
+
exports.hasFormDataKeyOrNested = hasFormDataKeyOrNested;
|
|
43
|
+
exports.hasFormDataKeyWithPrefix = hasFormDataKeyWithPrefix;
|
|
44
|
+
exports.isTrueFormValue = isTrueFormValue;
|
|
45
|
+
exports.validationMapper = validationMapper;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const trueFormValues = /* @__PURE__ */ new Set(["on", "1", "yes", "true"]);
|
|
2
|
+
function isTrueFormValue(value) {
|
|
3
|
+
return typeof value === "string" && trueFormValues.has(value);
|
|
4
|
+
}
|
|
5
|
+
function coerceFormNumber(value) {
|
|
6
|
+
return value === null || value === "" ? value : Number(value);
|
|
7
|
+
}
|
|
8
|
+
function hasFormDataKeyWithPrefix(keys, prefix) {
|
|
9
|
+
for (const key of keys) {
|
|
10
|
+
if (key.startsWith(prefix)) return true;
|
|
11
|
+
}
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
function hasFormDataKeyOrNested(keys, key) {
|
|
15
|
+
return hasFormDataKeyWithPrefix(keys, `${key}.`) || hasFormDataKeyWithPrefix(keys, key);
|
|
16
|
+
}
|
|
17
|
+
const validationMapper = /* @__PURE__ */ new Map();
|
|
18
|
+
class UnsupportedVendorError extends Error {
|
|
19
|
+
constructor(vendor) {
|
|
20
|
+
super(
|
|
21
|
+
`standard-form: Unsupported schema vendor "${vendor}". Request built-in support at https://github.com/standard-community/standard-form/issues or register a handler with loadVendor().`
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
class MissingDependencyError extends Error {
|
|
26
|
+
constructor(packageName) {
|
|
27
|
+
super(`standard-form: Missing dependencies "${packageName}".`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
class UnsupportedSchemaError extends Error {
|
|
31
|
+
constructor(vendor, requirement) {
|
|
32
|
+
super(`standard-form: Unsupported schema for vendor "${vendor}". ${requirement}.`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { MissingDependencyError as M, UnsupportedVendorError as U, UnsupportedSchemaError as a, hasFormDataKeyOrNested as b, coerceFormNumber as c, hasFormDataKeyWithPrefix as h, isTrueFormValue as i, validationMapper as v };
|
|
@@ -0,0 +1,28 @@
|
|
|
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, options: Options) => unknown;
|
|
14
|
+
interface Options {
|
|
15
|
+
convertEmptyStringsToNull?: boolean | undefined;
|
|
16
|
+
}
|
|
17
|
+
declare class UnsupportedVendorError extends Error {
|
|
18
|
+
constructor(vendor: string);
|
|
19
|
+
}
|
|
20
|
+
declare class MissingDependencyError extends Error {
|
|
21
|
+
constructor(packageName: string);
|
|
22
|
+
}
|
|
23
|
+
declare class UnsupportedSchemaError extends Error {
|
|
24
|
+
constructor(vendor: string, requirement: string);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { MissingDependencyError as M, UnsupportedSchemaError as U, UnsupportedVendorError as a };
|
|
28
|
+
export type { Options as O, ToFormInputFn as T };
|
|
@@ -0,0 +1,28 @@
|
|
|
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, options: Options) => unknown;
|
|
14
|
+
interface Options {
|
|
15
|
+
convertEmptyStringsToNull?: boolean | undefined;
|
|
16
|
+
}
|
|
17
|
+
declare class UnsupportedVendorError extends Error {
|
|
18
|
+
constructor(vendor: string);
|
|
19
|
+
}
|
|
20
|
+
declare class MissingDependencyError extends Error {
|
|
21
|
+
constructor(packageName: string);
|
|
22
|
+
}
|
|
23
|
+
declare class UnsupportedSchemaError extends Error {
|
|
24
|
+
constructor(vendor: string, requirement: string);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { MissingDependencyError as M, UnsupportedSchemaError as U, UnsupportedVendorError as a };
|
|
28
|
+
export type { Options as O, ToFormInputFn as T };
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('../utils-BoRc22Ql.cjs');
|
|
4
|
+
|
|
5
|
+
async function arktype() {
|
|
6
|
+
return (schema, formData, options) => {
|
|
7
|
+
if (!isArkTypeSchema(schema)) return formData;
|
|
8
|
+
let structure = schema.internal;
|
|
9
|
+
while (structure && getValue(structure, "kind") === "morph") {
|
|
10
|
+
const inner = getValue(structure, "inner");
|
|
11
|
+
const input = getValue(inner, "in");
|
|
12
|
+
structure = isNode(input) ? input : void 0;
|
|
13
|
+
}
|
|
14
|
+
const nestedStructure = structure && getValue(structure, "structure");
|
|
15
|
+
if (isNode(nestedStructure)) structure = nestedStructure;
|
|
16
|
+
return !structure || !hasObjectProperties(structure) ? formData : convertObject(formData, structure, "", options);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
function isArkTypeSchema(schema) {
|
|
20
|
+
return typeof schema === "function" && "internal" in schema;
|
|
21
|
+
}
|
|
22
|
+
function isNode(value) {
|
|
23
|
+
return (typeof value === "object" || typeof value === "function") && value !== null && "kind" in value;
|
|
24
|
+
}
|
|
25
|
+
function isRecord(value) {
|
|
26
|
+
return (typeof value === "object" || typeof value === "function") && value !== null;
|
|
27
|
+
}
|
|
28
|
+
function getValue(value, key) {
|
|
29
|
+
return isRecord(value) ? value[key] : void 0;
|
|
30
|
+
}
|
|
31
|
+
function convertObject(formData, node, prefix = "", options) {
|
|
32
|
+
const result = {};
|
|
33
|
+
const structure = getValue(node, "structure");
|
|
34
|
+
const properties = isNode(structure) ? structure : node;
|
|
35
|
+
const propsByKey = getValue(properties, "propsByKey");
|
|
36
|
+
const required = getValue(node, "required");
|
|
37
|
+
const inner = getValue(node, "inner");
|
|
38
|
+
const innerRequired = getValue(inner, "required");
|
|
39
|
+
const entries = [];
|
|
40
|
+
if (isRecord(propsByKey)) {
|
|
41
|
+
for (const [key, property] of Object.entries(propsByKey)) {
|
|
42
|
+
const value = getValue(property, "value");
|
|
43
|
+
if (isNode(value)) entries.push([key, value, property]);
|
|
44
|
+
}
|
|
45
|
+
} else {
|
|
46
|
+
const requiredEntries = Array.isArray(required) ? required : Array.isArray(innerRequired) ? innerRequired : [];
|
|
47
|
+
for (const entry of requiredEntries) {
|
|
48
|
+
const key = getValue(entry, "key");
|
|
49
|
+
const value = getValue(entry, "value");
|
|
50
|
+
if (typeof key === "string" && isNode(value)) entries.push([key, value, void 0]);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
for (const [key, childNode, property] of entries) {
|
|
54
|
+
const name = prefix + key;
|
|
55
|
+
const branches = getValue(childNode, "branches");
|
|
56
|
+
const sequence = getValue(childNode, "sequence");
|
|
57
|
+
if (getValue(childNode, "kind") === "union" && Array.isArray(branches) && branches.some((branch) => isNode(getValue(branch, "structure")))) {
|
|
58
|
+
const variant = formData.get(`${name}.type`);
|
|
59
|
+
const branch = branches.find((option) => {
|
|
60
|
+
const branchStructure = getValue(option, "structure");
|
|
61
|
+
const branchProps = getValue(branchStructure, "propsByKey");
|
|
62
|
+
const typeProperty = getValue(branchProps, "type");
|
|
63
|
+
const typeValue = getValue(typeProperty, "value");
|
|
64
|
+
return getValue(typeValue, "unit") === variant;
|
|
65
|
+
});
|
|
66
|
+
result[key] = isNode(branch) ? convertObject(formData, branch, `${name}.`, options) : null;
|
|
67
|
+
} else if (Array.isArray(sequence)) {
|
|
68
|
+
result[key] = formData.getAll(name).map((value) => coerce(value, sequence[0], options));
|
|
69
|
+
} else if (isArrayExpression(childNode)) {
|
|
70
|
+
result[key] = formData.getAll(name).map((value) => coerce(value, childNode, options));
|
|
71
|
+
} else if (isNode(getValue(childNode, "structure")) || hasObjectProperties(childNode)) {
|
|
72
|
+
const nestedPrefix = `${name}.`;
|
|
73
|
+
const hasNestedKeys = utils.hasFormDataKeyWithPrefix(formData.keys(), nestedPrefix);
|
|
74
|
+
const nested = getValue(childNode, "structure");
|
|
75
|
+
const defaultValue = getValue(property, "default");
|
|
76
|
+
result[key] = hasNestedKeys && isNode(nested) ? convertObject(formData, nested, nestedPrefix, options) : typeof defaultValue === "function" ? defaultValue() : {};
|
|
77
|
+
} else if (matches(childNode, "boolean")) {
|
|
78
|
+
const value = formData.get(name);
|
|
79
|
+
result[key] = utils.isTrueFormValue(value) ? true : value === "false" ? false : formData.has(name);
|
|
80
|
+
} else if (matches(childNode, "number")) {
|
|
81
|
+
const value = formData.get(name);
|
|
82
|
+
result[key] = value === null ? getValue(childNode, "default") ?? value : utils.coerceFormNumber(value);
|
|
83
|
+
} else {
|
|
84
|
+
const value = formData.get(name);
|
|
85
|
+
result[key] = options.convertEmptyStringsToNull && value === "" && matches(childNode, "string") ? null : value;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
function hasObjectProperties(node) {
|
|
91
|
+
const inner = getValue(node, "inner");
|
|
92
|
+
return Boolean(
|
|
93
|
+
getValue(node, "propsByKey") || Array.isArray(getValue(node, "required")) || Array.isArray(getValue(inner, "required"))
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
function isArrayExpression(node) {
|
|
97
|
+
const expression = getValue(node, "expression");
|
|
98
|
+
return typeof expression === "string" && expression.endsWith("[]");
|
|
99
|
+
}
|
|
100
|
+
function matches(node, kind) {
|
|
101
|
+
const expression = getValue(node, "expression");
|
|
102
|
+
return getValue(node, "domain") === kind || expression === kind || typeof expression === "string" && expression.includes(kind);
|
|
103
|
+
}
|
|
104
|
+
function coerce(value, node, options) {
|
|
105
|
+
if (options.convertEmptyStringsToNull && value === "" && matches(node, "string")) return null;
|
|
106
|
+
if (matches(node, "number")) return utils.coerceFormNumber(value);
|
|
107
|
+
if (matches(node, "boolean")) {
|
|
108
|
+
return utils.isTrueFormValue(value);
|
|
109
|
+
}
|
|
110
|
+
return value;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
module.exports = arktype;
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { h as hasFormDataKeyWithPrefix, i as isTrueFormValue, c as coerceFormNumber } from '../utils-Cr64FEDS.js';
|
|
2
|
+
|
|
3
|
+
async function arktype() {
|
|
4
|
+
return (schema, formData, options) => {
|
|
5
|
+
if (!isArkTypeSchema(schema)) return formData;
|
|
6
|
+
let structure = schema.internal;
|
|
7
|
+
while (structure && getValue(structure, "kind") === "morph") {
|
|
8
|
+
const inner = getValue(structure, "inner");
|
|
9
|
+
const input = getValue(inner, "in");
|
|
10
|
+
structure = isNode(input) ? input : void 0;
|
|
11
|
+
}
|
|
12
|
+
const nestedStructure = structure && getValue(structure, "structure");
|
|
13
|
+
if (isNode(nestedStructure)) structure = nestedStructure;
|
|
14
|
+
return !structure || !hasObjectProperties(structure) ? formData : convertObject(formData, structure, "", options);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function isArkTypeSchema(schema) {
|
|
18
|
+
return typeof schema === "function" && "internal" in schema;
|
|
19
|
+
}
|
|
20
|
+
function isNode(value) {
|
|
21
|
+
return (typeof value === "object" || typeof value === "function") && value !== null && "kind" in value;
|
|
22
|
+
}
|
|
23
|
+
function isRecord(value) {
|
|
24
|
+
return (typeof value === "object" || typeof value === "function") && value !== null;
|
|
25
|
+
}
|
|
26
|
+
function getValue(value, key) {
|
|
27
|
+
return isRecord(value) ? value[key] : void 0;
|
|
28
|
+
}
|
|
29
|
+
function convertObject(formData, node, prefix = "", options) {
|
|
30
|
+
const result = {};
|
|
31
|
+
const structure = getValue(node, "structure");
|
|
32
|
+
const properties = isNode(structure) ? structure : node;
|
|
33
|
+
const propsByKey = getValue(properties, "propsByKey");
|
|
34
|
+
const required = getValue(node, "required");
|
|
35
|
+
const inner = getValue(node, "inner");
|
|
36
|
+
const innerRequired = getValue(inner, "required");
|
|
37
|
+
const entries = [];
|
|
38
|
+
if (isRecord(propsByKey)) {
|
|
39
|
+
for (const [key, property] of Object.entries(propsByKey)) {
|
|
40
|
+
const value = getValue(property, "value");
|
|
41
|
+
if (isNode(value)) entries.push([key, value, property]);
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
const requiredEntries = Array.isArray(required) ? required : Array.isArray(innerRequired) ? innerRequired : [];
|
|
45
|
+
for (const entry of requiredEntries) {
|
|
46
|
+
const key = getValue(entry, "key");
|
|
47
|
+
const value = getValue(entry, "value");
|
|
48
|
+
if (typeof key === "string" && isNode(value)) entries.push([key, value, void 0]);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
for (const [key, childNode, property] of entries) {
|
|
52
|
+
const name = prefix + key;
|
|
53
|
+
const branches = getValue(childNode, "branches");
|
|
54
|
+
const sequence = getValue(childNode, "sequence");
|
|
55
|
+
if (getValue(childNode, "kind") === "union" && Array.isArray(branches) && branches.some((branch) => isNode(getValue(branch, "structure")))) {
|
|
56
|
+
const variant = formData.get(`${name}.type`);
|
|
57
|
+
const branch = branches.find((option) => {
|
|
58
|
+
const branchStructure = getValue(option, "structure");
|
|
59
|
+
const branchProps = getValue(branchStructure, "propsByKey");
|
|
60
|
+
const typeProperty = getValue(branchProps, "type");
|
|
61
|
+
const typeValue = getValue(typeProperty, "value");
|
|
62
|
+
return getValue(typeValue, "unit") === variant;
|
|
63
|
+
});
|
|
64
|
+
result[key] = isNode(branch) ? convertObject(formData, branch, `${name}.`, options) : null;
|
|
65
|
+
} else if (Array.isArray(sequence)) {
|
|
66
|
+
result[key] = formData.getAll(name).map((value) => coerce(value, sequence[0], options));
|
|
67
|
+
} else if (isArrayExpression(childNode)) {
|
|
68
|
+
result[key] = formData.getAll(name).map((value) => coerce(value, childNode, options));
|
|
69
|
+
} else if (isNode(getValue(childNode, "structure")) || hasObjectProperties(childNode)) {
|
|
70
|
+
const nestedPrefix = `${name}.`;
|
|
71
|
+
const hasNestedKeys = hasFormDataKeyWithPrefix(formData.keys(), nestedPrefix);
|
|
72
|
+
const nested = getValue(childNode, "structure");
|
|
73
|
+
const defaultValue = getValue(property, "default");
|
|
74
|
+
result[key] = hasNestedKeys && isNode(nested) ? convertObject(formData, nested, nestedPrefix, options) : typeof defaultValue === "function" ? defaultValue() : {};
|
|
75
|
+
} else if (matches(childNode, "boolean")) {
|
|
76
|
+
const value = formData.get(name);
|
|
77
|
+
result[key] = isTrueFormValue(value) ? true : value === "false" ? false : formData.has(name);
|
|
78
|
+
} else if (matches(childNode, "number")) {
|
|
79
|
+
const value = formData.get(name);
|
|
80
|
+
result[key] = value === null ? getValue(childNode, "default") ?? value : coerceFormNumber(value);
|
|
81
|
+
} else {
|
|
82
|
+
const value = formData.get(name);
|
|
83
|
+
result[key] = options.convertEmptyStringsToNull && value === "" && matches(childNode, "string") ? null : value;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
function hasObjectProperties(node) {
|
|
89
|
+
const inner = getValue(node, "inner");
|
|
90
|
+
return Boolean(
|
|
91
|
+
getValue(node, "propsByKey") || Array.isArray(getValue(node, "required")) || Array.isArray(getValue(inner, "required"))
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
function isArrayExpression(node) {
|
|
95
|
+
const expression = getValue(node, "expression");
|
|
96
|
+
return typeof expression === "string" && expression.endsWith("[]");
|
|
97
|
+
}
|
|
98
|
+
function matches(node, kind) {
|
|
99
|
+
const expression = getValue(node, "expression");
|
|
100
|
+
return getValue(node, "domain") === kind || expression === kind || typeof expression === "string" && expression.includes(kind);
|
|
101
|
+
}
|
|
102
|
+
function coerce(value, node, options) {
|
|
103
|
+
if (options.convertEmptyStringsToNull && value === "" && matches(node, "string")) return null;
|
|
104
|
+
if (matches(node, "number")) return coerceFormNumber(value);
|
|
105
|
+
if (matches(node, "boolean")) {
|
|
106
|
+
return isTrueFormValue(value);
|
|
107
|
+
}
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export { arktype as default };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('../utils-BoRc22Ql.cjs');
|
|
4
|
+
|
|
5
|
+
async function valibot() {
|
|
6
|
+
return (schema, formData, options) => {
|
|
7
|
+
if (!isValibotSchema(schema)) return formData;
|
|
8
|
+
return isObjectSchema(schema) ? convertObject(formData, schema, "", options) : formData;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
function isValibotSchema(schema) {
|
|
12
|
+
return typeof schema === "object" && schema !== null && "~standard" in schema;
|
|
13
|
+
}
|
|
14
|
+
function isObjectSchema(schema) {
|
|
15
|
+
return schema.type === "object" || schema.type === "object_with_rest" || schema.type === "loose_object" || schema.type === "strict_object";
|
|
16
|
+
}
|
|
17
|
+
function isSchema(schema) {
|
|
18
|
+
return typeof schema === "object" && schema !== null && "kind" in schema && "type" in schema;
|
|
19
|
+
}
|
|
20
|
+
function isArraySchema(schema) {
|
|
21
|
+
return schema.type === "array";
|
|
22
|
+
}
|
|
23
|
+
function isVariantSchema(schema) {
|
|
24
|
+
return schema.type === "variant";
|
|
25
|
+
}
|
|
26
|
+
function isLiteralSchema(schema) {
|
|
27
|
+
return schema.type === "literal";
|
|
28
|
+
}
|
|
29
|
+
function convertObject(formData, schema, prefix = "", options) {
|
|
30
|
+
const result = {};
|
|
31
|
+
for (const [key, child] of Object.entries(schema.entries)) {
|
|
32
|
+
const name = prefix + key;
|
|
33
|
+
if (!isSchema(child)) continue;
|
|
34
|
+
const base = unwrap(child);
|
|
35
|
+
if (isObjectSchema(base)) {
|
|
36
|
+
const nested = convertObject(formData, base, `${name}.`, options);
|
|
37
|
+
const hasNestedKeys = utils.hasFormDataKeyWithPrefix(formData.keys(), `${name}.`);
|
|
38
|
+
result[key] = hasNestedKeys ? nested : getDefault(child) ?? nested;
|
|
39
|
+
} else if (base.type === "boolean") {
|
|
40
|
+
const value = formData.get(name);
|
|
41
|
+
result[key] = utils.isTrueFormValue(value) ? true : value === "false" ? false : formData.has(name);
|
|
42
|
+
} else if (base.type === "number") {
|
|
43
|
+
const value = formData.get(name);
|
|
44
|
+
result[key] = utils.coerceFormNumber(value);
|
|
45
|
+
} else if (base.type === "array") {
|
|
46
|
+
if (!isArraySchema(base)) continue;
|
|
47
|
+
result[key] = formData.getAll(name).map((value) => convertValue(value, base.item, options));
|
|
48
|
+
} else if (base.type === "variant") {
|
|
49
|
+
if (!isVariantSchema(base)) continue;
|
|
50
|
+
const variant = base;
|
|
51
|
+
const discriminator = formData.get(`${name}.${variant.key}`);
|
|
52
|
+
const option = variant.options.find((candidate) => {
|
|
53
|
+
if (!isObjectSchema(candidate)) return false;
|
|
54
|
+
const discriminatorSchema = candidate.entries[variant.key];
|
|
55
|
+
return isSchema(discriminatorSchema) && isLiteralSchema(discriminatorSchema) && discriminatorSchema.literal === discriminator;
|
|
56
|
+
});
|
|
57
|
+
result[key] = option && isObjectSchema(option) ? convertObject(formData, option, `${name}.`, options) : null;
|
|
58
|
+
} else {
|
|
59
|
+
result[key] = convertValue(formData.get(name), base, options);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (schema.type === "object_with_rest") {
|
|
63
|
+
formData.forEach((value, key) => {
|
|
64
|
+
if (key.startsWith(prefix) && !(key.slice(prefix.length) in result)) {
|
|
65
|
+
result[key.slice(prefix.length)] = value;
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
function unwrap(schema) {
|
|
72
|
+
if ("wrapped" in schema && isSchema(schema.wrapped)) return unwrap(schema.wrapped);
|
|
73
|
+
if ("pipe" in schema) {
|
|
74
|
+
const pipe = schema.pipe;
|
|
75
|
+
if (Array.isArray(pipe) && isSchema(pipe[0])) return unwrap(pipe[0]);
|
|
76
|
+
}
|
|
77
|
+
return schema;
|
|
78
|
+
}
|
|
79
|
+
function getDefault(schema) {
|
|
80
|
+
return "default" in schema ? schema.default : void 0;
|
|
81
|
+
}
|
|
82
|
+
function convertValue(value, schema, options) {
|
|
83
|
+
const base = unwrap(schema);
|
|
84
|
+
if (options.convertEmptyStringsToNull && value === "" && base.type === "string") return null;
|
|
85
|
+
if (value === null) return getDefault(schema) ?? null;
|
|
86
|
+
if (base.type === "number") return utils.coerceFormNumber(value);
|
|
87
|
+
if (base.type === "boolean") {
|
|
88
|
+
return utils.isTrueFormValue(value) ? true : value === "false" ? false : Boolean(value);
|
|
89
|
+
}
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = valibot;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { h as hasFormDataKeyWithPrefix, i as isTrueFormValue, c as coerceFormNumber } from '../utils-Cr64FEDS.js';
|
|
2
|
+
|
|
3
|
+
async function valibot() {
|
|
4
|
+
return (schema, formData, options) => {
|
|
5
|
+
if (!isValibotSchema(schema)) return formData;
|
|
6
|
+
return isObjectSchema(schema) ? convertObject(formData, schema, "", options) : formData;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
function isValibotSchema(schema) {
|
|
10
|
+
return typeof schema === "object" && schema !== null && "~standard" in schema;
|
|
11
|
+
}
|
|
12
|
+
function isObjectSchema(schema) {
|
|
13
|
+
return schema.type === "object" || schema.type === "object_with_rest" || schema.type === "loose_object" || schema.type === "strict_object";
|
|
14
|
+
}
|
|
15
|
+
function isSchema(schema) {
|
|
16
|
+
return typeof schema === "object" && schema !== null && "kind" in schema && "type" in schema;
|
|
17
|
+
}
|
|
18
|
+
function isArraySchema(schema) {
|
|
19
|
+
return schema.type === "array";
|
|
20
|
+
}
|
|
21
|
+
function isVariantSchema(schema) {
|
|
22
|
+
return schema.type === "variant";
|
|
23
|
+
}
|
|
24
|
+
function isLiteralSchema(schema) {
|
|
25
|
+
return schema.type === "literal";
|
|
26
|
+
}
|
|
27
|
+
function convertObject(formData, schema, prefix = "", options) {
|
|
28
|
+
const result = {};
|
|
29
|
+
for (const [key, child] of Object.entries(schema.entries)) {
|
|
30
|
+
const name = prefix + key;
|
|
31
|
+
if (!isSchema(child)) continue;
|
|
32
|
+
const base = unwrap(child);
|
|
33
|
+
if (isObjectSchema(base)) {
|
|
34
|
+
const nested = convertObject(formData, base, `${name}.`, options);
|
|
35
|
+
const hasNestedKeys = hasFormDataKeyWithPrefix(formData.keys(), `${name}.`);
|
|
36
|
+
result[key] = hasNestedKeys ? nested : getDefault(child) ?? nested;
|
|
37
|
+
} else if (base.type === "boolean") {
|
|
38
|
+
const value = formData.get(name);
|
|
39
|
+
result[key] = isTrueFormValue(value) ? true : value === "false" ? false : formData.has(name);
|
|
40
|
+
} else if (base.type === "number") {
|
|
41
|
+
const value = formData.get(name);
|
|
42
|
+
result[key] = coerceFormNumber(value);
|
|
43
|
+
} else if (base.type === "array") {
|
|
44
|
+
if (!isArraySchema(base)) continue;
|
|
45
|
+
result[key] = formData.getAll(name).map((value) => convertValue(value, base.item, options));
|
|
46
|
+
} else if (base.type === "variant") {
|
|
47
|
+
if (!isVariantSchema(base)) continue;
|
|
48
|
+
const variant = base;
|
|
49
|
+
const discriminator = formData.get(`${name}.${variant.key}`);
|
|
50
|
+
const option = variant.options.find((candidate) => {
|
|
51
|
+
if (!isObjectSchema(candidate)) return false;
|
|
52
|
+
const discriminatorSchema = candidate.entries[variant.key];
|
|
53
|
+
return isSchema(discriminatorSchema) && isLiteralSchema(discriminatorSchema) && discriminatorSchema.literal === discriminator;
|
|
54
|
+
});
|
|
55
|
+
result[key] = option && isObjectSchema(option) ? convertObject(formData, option, `${name}.`, options) : null;
|
|
56
|
+
} else {
|
|
57
|
+
result[key] = convertValue(formData.get(name), base, options);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (schema.type === "object_with_rest") {
|
|
61
|
+
formData.forEach((value, key) => {
|
|
62
|
+
if (key.startsWith(prefix) && !(key.slice(prefix.length) in result)) {
|
|
63
|
+
result[key.slice(prefix.length)] = value;
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
function unwrap(schema) {
|
|
70
|
+
if ("wrapped" in schema && isSchema(schema.wrapped)) return unwrap(schema.wrapped);
|
|
71
|
+
if ("pipe" in schema) {
|
|
72
|
+
const pipe = schema.pipe;
|
|
73
|
+
if (Array.isArray(pipe) && isSchema(pipe[0])) return unwrap(pipe[0]);
|
|
74
|
+
}
|
|
75
|
+
return schema;
|
|
76
|
+
}
|
|
77
|
+
function getDefault(schema) {
|
|
78
|
+
return "default" in schema ? schema.default : void 0;
|
|
79
|
+
}
|
|
80
|
+
function convertValue(value, schema, options) {
|
|
81
|
+
const base = unwrap(schema);
|
|
82
|
+
if (options.convertEmptyStringsToNull && value === "" && base.type === "string") return null;
|
|
83
|
+
if (value === null) return getDefault(schema) ?? null;
|
|
84
|
+
if (base.type === "number") return coerceFormNumber(value);
|
|
85
|
+
if (base.type === "boolean") {
|
|
86
|
+
return isTrueFormValue(value) ? true : value === "false" ? false : Boolean(value);
|
|
87
|
+
}
|
|
88
|
+
return value;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export { valibot as default };
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var utils = require('../utils-BoRc22Ql.cjs');
|
|
4
|
+
|
|
5
|
+
async function zod() {
|
|
6
|
+
return (schema, formData, options) => {
|
|
7
|
+
if (!isZodSchema(schema)) {
|
|
8
|
+
throw new utils.UnsupportedSchemaError("zod", "Zod 4 and higher is required");
|
|
9
|
+
}
|
|
10
|
+
const baseSchema = unwrapBaseObjectSchema(schema, formData);
|
|
11
|
+
return hasZodType(baseSchema, "object") ? formDataToObject(formData, baseSchema, "", options) : formData;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function isZodSchema(schema) {
|
|
15
|
+
return typeof schema === "object" && schema !== null && "_zod" in schema;
|
|
16
|
+
}
|
|
17
|
+
function hasZodType(schema, type) {
|
|
18
|
+
return isZodSchema(schema) && schema._zod.def.type === type;
|
|
19
|
+
}
|
|
20
|
+
function isDiscriminatedUnion(schema) {
|
|
21
|
+
return schema._zod.def.type === "union" && "discriminator" in schema._zod.def;
|
|
22
|
+
}
|
|
23
|
+
function formDataToObject(formData, schema, prefix = "", options = {}) {
|
|
24
|
+
const formKeys = [...formData.keys()];
|
|
25
|
+
const obj = schema._zod.def.catchall ? Object.fromEntries(
|
|
26
|
+
[...formData.entries()].filter(([k]) => k.startsWith(prefix)).map(([k, v]) => [k.slice(prefix.length), v])
|
|
27
|
+
) : {};
|
|
28
|
+
for (const [key, baseValidator] of Object.entries(schema._zod.def.shape)) {
|
|
29
|
+
const prefixedKey = prefix + key;
|
|
30
|
+
let validator = baseValidator;
|
|
31
|
+
while (hasZodType(validator, "optional") || hasZodType(validator, "nullable") || hasZodType(validator, "default")) {
|
|
32
|
+
if (hasZodType(validator, "default") && !utils.hasFormDataKeyOrNested(formKeys, prefixedKey)) {
|
|
33
|
+
obj[key] = validator._zod.def.defaultValue instanceof Function ? validator._zod.def.defaultValue() : validator._zod.def.defaultValue;
|
|
34
|
+
}
|
|
35
|
+
validator = validator._zod.def.innerType;
|
|
36
|
+
}
|
|
37
|
+
while (hasZodType(validator, "pipe")) {
|
|
38
|
+
validator = validator._zod.def.in;
|
|
39
|
+
}
|
|
40
|
+
if (isDiscriminatedUnion(validator)) {
|
|
41
|
+
const typeKey = validator._zod.def.discriminator;
|
|
42
|
+
const typeValue = formData.get(prefixedKey + "." + typeKey);
|
|
43
|
+
if (typeof typeValue === "string") {
|
|
44
|
+
const match = validator._zod.def.options.find(
|
|
45
|
+
(option) => option.def.shape[typeKey].values.has(typeValue)
|
|
46
|
+
);
|
|
47
|
+
if (match) {
|
|
48
|
+
validator = match;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (hasZodType(validator, "object")) {
|
|
53
|
+
const nestedPrefix = prefixedKey + ".";
|
|
54
|
+
const hasNestedKeys = utils.hasFormDataKeyWithPrefix(formKeys, nestedPrefix);
|
|
55
|
+
if (hasNestedKeys) {
|
|
56
|
+
obj[key] = formDataToObject(formData, validator, nestedPrefix, options);
|
|
57
|
+
} else if (!(key in obj)) {
|
|
58
|
+
obj[key] = hasZodType(baseValidator, "nullable") ? null : void 0;
|
|
59
|
+
}
|
|
60
|
+
} else if (!formData.has(prefixedKey) && key in obj) {
|
|
61
|
+
continue;
|
|
62
|
+
} else if (hasZodType(validator, "boolean")) {
|
|
63
|
+
const val = formData.get(prefixedKey);
|
|
64
|
+
obj[key] = utils.isTrueFormValue(val) ? true : val === "false" ? false : formData.has(prefixedKey);
|
|
65
|
+
} else if (hasZodType(validator, "array")) {
|
|
66
|
+
obj[key] = handleFormDataGetAll(prefixedKey, formData, validator, options);
|
|
67
|
+
} else {
|
|
68
|
+
obj[key] = handleFormDataGet(prefixedKey, formData, validator, baseValidator, options);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return obj;
|
|
72
|
+
}
|
|
73
|
+
function handleFormDataGetAll(key, formData, validator, options) {
|
|
74
|
+
const entries = Array.from(formData.getAll(key));
|
|
75
|
+
const elementValidator = validator._zod.def.element;
|
|
76
|
+
if (hasZodType(elementValidator, "number")) {
|
|
77
|
+
return entries.map((value) => utils.coerceFormNumber(value));
|
|
78
|
+
} else if (hasZodType(elementValidator, "boolean")) {
|
|
79
|
+
return entries.map(
|
|
80
|
+
(v) => utils.isTrueFormValue(v) ? true : v === "false" ? false : Boolean(v)
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
if (options.convertEmptyStringsToNull && hasZodType(elementValidator, "string")) {
|
|
84
|
+
return entries.map((value) => value === "" ? null : value);
|
|
85
|
+
}
|
|
86
|
+
return entries;
|
|
87
|
+
}
|
|
88
|
+
function handleFormDataGet(key, formData, validator, baseValidator, options) {
|
|
89
|
+
const value = formData.get(key);
|
|
90
|
+
if (!value) {
|
|
91
|
+
return hasZodType(baseValidator, "optional") ? void 0 : null;
|
|
92
|
+
}
|
|
93
|
+
if (options.convertEmptyStringsToNull && value === "" && hasZodType(validator, "string")) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
return hasZodType(validator, "number") ? utils.coerceFormNumber(value) : value;
|
|
97
|
+
}
|
|
98
|
+
function unwrapBaseObjectSchema(schema, unparsedInput) {
|
|
99
|
+
if (hasZodType(schema, "pipe")) {
|
|
100
|
+
return unwrapBaseObjectSchema(schema._zod.def.in, unparsedInput);
|
|
101
|
+
}
|
|
102
|
+
if (isDiscriminatedUnion(schema)) {
|
|
103
|
+
const typeKey = schema._zod.def.discriminator;
|
|
104
|
+
const typeValue = unparsedInput.get(typeKey);
|
|
105
|
+
if (typeof typeValue !== "string") return schema;
|
|
106
|
+
const objSchema = schema._zod.def.options.find(
|
|
107
|
+
(option) => option.def.shape[typeKey].values.has(typeValue)
|
|
108
|
+
);
|
|
109
|
+
if (!objSchema) return schema;
|
|
110
|
+
return objSchema;
|
|
111
|
+
}
|
|
112
|
+
return schema;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
module.exports = zod;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { a as UnsupportedSchemaError, b as hasFormDataKeyOrNested, h as hasFormDataKeyWithPrefix, i as isTrueFormValue, c as coerceFormNumber } from '../utils-Cr64FEDS.js';
|
|
2
|
+
|
|
3
|
+
async function zod() {
|
|
4
|
+
return (schema, formData, options) => {
|
|
5
|
+
if (!isZodSchema(schema)) {
|
|
6
|
+
throw new UnsupportedSchemaError("zod", "Zod 4 and higher is required");
|
|
7
|
+
}
|
|
8
|
+
const baseSchema = unwrapBaseObjectSchema(schema, formData);
|
|
9
|
+
return hasZodType(baseSchema, "object") ? formDataToObject(formData, baseSchema, "", options) : formData;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
function isZodSchema(schema) {
|
|
13
|
+
return typeof schema === "object" && schema !== null && "_zod" in schema;
|
|
14
|
+
}
|
|
15
|
+
function hasZodType(schema, type) {
|
|
16
|
+
return isZodSchema(schema) && schema._zod.def.type === type;
|
|
17
|
+
}
|
|
18
|
+
function isDiscriminatedUnion(schema) {
|
|
19
|
+
return schema._zod.def.type === "union" && "discriminator" in schema._zod.def;
|
|
20
|
+
}
|
|
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;
|
|
34
|
+
}
|
|
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
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
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;
|
|
57
|
+
}
|
|
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);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return obj;
|
|
70
|
+
}
|
|
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;
|
|
85
|
+
}
|
|
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;
|
|
95
|
+
}
|
|
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;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export { zod as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,89 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@standard-community/standard-form",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"description": "Parse FormData with any Standard Schema validator.",
|
|
4
|
+
"version": "0.1.0",
|
|
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
|
+
"./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
|
+
}
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
"peerDependencies": {
|
|
73
|
+
"@standard-schema/spec": "^1.1.0"
|
|
74
|
+
},
|
|
75
|
+
"devDependencies": {
|
|
76
|
+
"@standard-schema/spec": "^1.1.0",
|
|
77
|
+
"arktype": "^2.2.10",
|
|
78
|
+
"pkgroll": "^2.29.1",
|
|
79
|
+
"typescript": "6.0.3",
|
|
80
|
+
"valibot": "^1.5.0",
|
|
81
|
+
"vitest": "^5.1.1",
|
|
82
|
+
"zod": "^4.6.7"
|
|
83
|
+
},
|
|
84
|
+
"scripts": {
|
|
85
|
+
"build": "pnpm typecheck && pkgroll --clean-dist",
|
|
86
|
+
"typecheck": "tsc --noEmit",
|
|
87
|
+
"test": "pnpm build && vitest run"
|
|
88
|
+
}
|
|
6
89
|
}
|