@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/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,6 +1,28 @@
|
|
|
1
1
|
# @standard-community/standard-form
|
|
2
2
|
|
|
3
|
-
Parse [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) with any [Standard Schema](https://standardschema.dev) validator.
|
|
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.
|
|
4
26
|
|
|
5
27
|
```ts
|
|
6
28
|
import { parseFormData } from '@standard-community/standard-form';
|
|
@@ -13,6 +35,12 @@ const schema = z.object({
|
|
|
13
35
|
address: z.object({ city: z.string() }),
|
|
14
36
|
});
|
|
15
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
|
+
|
|
16
44
|
const result = await parseFormData(schema, formData);
|
|
17
45
|
if (result.issues) {
|
|
18
46
|
handleIssues(result.issues);
|
|
@@ -21,23 +49,47 @@ if (result.issues) {
|
|
|
21
49
|
}
|
|
22
50
|
```
|
|
23
51
|
|
|
24
|
-
|
|
52
|
+
Adapters handle these form values:
|
|
25
53
|
|
|
26
|
-
|
|
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.
|
|
27
59
|
|
|
28
|
-
|
|
29
|
-
| ------ | ------- |
|
|
30
|
-
| `zod` | Built in for Zod 4 and higher |
|
|
60
|
+
## Convert empty strings to null
|
|
31
61
|
|
|
32
|
-
|
|
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):
|
|
33
83
|
|
|
34
84
|
```ts
|
|
35
85
|
import { loadVendor } from '@standard-community/standard-form';
|
|
36
86
|
|
|
37
|
-
loadVendor('
|
|
87
|
+
loadVendor('custom', (schema, formData, options) => {
|
|
88
|
+
return Object.fromEntries(formData.entries());
|
|
89
|
+
});
|
|
38
90
|
```
|
|
39
91
|
|
|
40
|
-
`loadVendor` also
|
|
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:
|
|
41
93
|
|
|
42
94
|
```ts
|
|
43
95
|
import { loadVendor } from '@standard-community/standard-form';
|
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var utils = require('./utils-
|
|
3
|
+
var utils = require('./utils-BoRc22Ql.cjs');
|
|
4
4
|
|
|
5
5
|
function _interopNamespaceDefaultOnly (e) { return Object.freeze({ __proto__: null, default: e }); }
|
|
6
6
|
|
|
@@ -14,6 +14,12 @@ const getToFormInputFn = async (vendor) => {
|
|
|
14
14
|
case "zod":
|
|
15
15
|
vendorFnPromise = (await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespaceDefaultOnly(require('./vendors/zod.cjs')); })).default();
|
|
16
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;
|
|
17
23
|
default:
|
|
18
24
|
throw new utils.UnsupportedVendorError(vendor);
|
|
19
25
|
}
|
|
@@ -22,9 +28,9 @@ const getToFormInputFn = async (vendor) => {
|
|
|
22
28
|
return vendorFn;
|
|
23
29
|
};
|
|
24
30
|
|
|
25
|
-
async function parseFormData(schema, formData) {
|
|
31
|
+
async function parseFormData(schema, formData, options = {}) {
|
|
26
32
|
const fn = await getToFormInputFn(schema["~standard"].vendor);
|
|
27
|
-
return await schema["~standard"].validate(fn(schema, formData));
|
|
33
|
+
return await schema["~standard"].validate(fn(schema, formData, options));
|
|
28
34
|
}
|
|
29
35
|
function loadVendor(vendor, fn) {
|
|
30
36
|
utils.validationMapper.set(vendor, fn);
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
-
import { T as ToFormInputFn } from './utils-
|
|
3
|
-
export { M as MissingDependencyError, U as UnsupportedSchemaError, a as UnsupportedVendorError } from './utils-
|
|
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
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Validates `FormData` against a Standard Schema.
|
|
@@ -9,7 +9,7 @@ export { M as MissingDependencyError, U as UnsupportedSchemaError, a as Unsuppor
|
|
|
9
9
|
* the schema expects — nested objects from dotted keys, numbers, booleans, arrays,
|
|
10
10
|
* defaults — by the handler for the schema's validator, then handed to the schema itself.
|
|
11
11
|
*/
|
|
12
|
-
declare function parseFormData<TSchema extends StandardSchemaV1>(schema: TSchema, formData: FormData): Promise<StandardSchemaV1.Result<StandardSchemaV1.InferOutput<TSchema>>>;
|
|
12
|
+
declare function parseFormData<TSchema extends StandardSchemaV1>(schema: TSchema, formData: FormData, options?: Options): Promise<StandardSchemaV1.Result<StandardSchemaV1.InferOutput<TSchema>>>;
|
|
13
13
|
declare function loadVendor(vendor: string, fn: ToFormInputFn): void;
|
|
14
14
|
|
|
15
|
-
export { ToFormInputFn, loadVendor, parseFormData };
|
|
15
|
+
export { Options, ToFormInputFn, loadVendor, parseFormData };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
-
import { T as ToFormInputFn } from './utils-
|
|
3
|
-
export { M as MissingDependencyError, U as UnsupportedSchemaError, a as UnsupportedVendorError } from './utils-
|
|
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
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Validates `FormData` against a Standard Schema.
|
|
@@ -9,7 +9,7 @@ export { M as MissingDependencyError, U as UnsupportedSchemaError, a as Unsuppor
|
|
|
9
9
|
* the schema expects — nested objects from dotted keys, numbers, booleans, arrays,
|
|
10
10
|
* defaults — by the handler for the schema's validator, then handed to the schema itself.
|
|
11
11
|
*/
|
|
12
|
-
declare function parseFormData<TSchema extends StandardSchemaV1>(schema: TSchema, formData: FormData): Promise<StandardSchemaV1.Result<StandardSchemaV1.InferOutput<TSchema>>>;
|
|
12
|
+
declare function parseFormData<TSchema extends StandardSchemaV1>(schema: TSchema, formData: FormData, options?: Options): Promise<StandardSchemaV1.Result<StandardSchemaV1.InferOutput<TSchema>>>;
|
|
13
13
|
declare function loadVendor(vendor: string, fn: ToFormInputFn): void;
|
|
14
14
|
|
|
15
|
-
export { ToFormInputFn, loadVendor, parseFormData };
|
|
15
|
+
export { Options, ToFormInputFn, loadVendor, parseFormData };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { v as validationMapper, U as UnsupportedVendorError } from './utils-
|
|
2
|
-
export { M as MissingDependencyError, a as UnsupportedSchemaError } from './utils-
|
|
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
3
|
|
|
4
4
|
const getToFormInputFn = async (vendor) => {
|
|
5
5
|
const cached = validationMapper.get(vendor);
|
|
@@ -11,6 +11,12 @@ const getToFormInputFn = async (vendor) => {
|
|
|
11
11
|
case "zod":
|
|
12
12
|
vendorFnPromise = (await import('./vendors/zod.js')).default();
|
|
13
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;
|
|
14
20
|
default:
|
|
15
21
|
throw new UnsupportedVendorError(vendor);
|
|
16
22
|
}
|
|
@@ -19,9 +25,9 @@ const getToFormInputFn = async (vendor) => {
|
|
|
19
25
|
return vendorFn;
|
|
20
26
|
};
|
|
21
27
|
|
|
22
|
-
async function parseFormData(schema, formData) {
|
|
28
|
+
async function parseFormData(schema, formData, options = {}) {
|
|
23
29
|
const fn = await getToFormInputFn(schema["~standard"].vendor);
|
|
24
|
-
return await schema["~standard"].validate(fn(schema, formData));
|
|
30
|
+
return await schema["~standard"].validate(fn(schema, formData, options));
|
|
25
31
|
}
|
|
26
32
|
function loadVendor(vendor, fn) {
|
|
27
33
|
validationMapper.set(vendor, fn);
|
|
@@ -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 };
|
|
@@ -10,7 +10,10 @@
|
|
|
10
10
|
* Implementations should return `formData` untouched when handed a schema they cannot
|
|
11
11
|
* introspect, so that validation — rather than the coercion — reports the mismatch.
|
|
12
12
|
*/
|
|
13
|
-
type ToFormInputFn = (schema: unknown, formData: FormData) => unknown;
|
|
13
|
+
type ToFormInputFn = (schema: unknown, formData: FormData, options: Options) => unknown;
|
|
14
|
+
interface Options {
|
|
15
|
+
convertEmptyStringsToNull?: boolean | undefined;
|
|
16
|
+
}
|
|
14
17
|
declare class UnsupportedVendorError extends Error {
|
|
15
18
|
constructor(vendor: string);
|
|
16
19
|
}
|
|
@@ -22,4 +25,4 @@ declare class UnsupportedSchemaError extends Error {
|
|
|
22
25
|
}
|
|
23
26
|
|
|
24
27
|
export { MissingDependencyError as M, UnsupportedSchemaError as U, UnsupportedVendorError as a };
|
|
25
|
-
export type { ToFormInputFn as T };
|
|
28
|
+
export type { Options as O, ToFormInputFn as T };
|
|
@@ -10,7 +10,10 @@
|
|
|
10
10
|
* Implementations should return `formData` untouched when handed a schema they cannot
|
|
11
11
|
* introspect, so that validation — rather than the coercion — reports the mismatch.
|
|
12
12
|
*/
|
|
13
|
-
type ToFormInputFn = (schema: unknown, formData: FormData) => unknown;
|
|
13
|
+
type ToFormInputFn = (schema: unknown, formData: FormData, options: Options) => unknown;
|
|
14
|
+
interface Options {
|
|
15
|
+
convertEmptyStringsToNull?: boolean | undefined;
|
|
16
|
+
}
|
|
14
17
|
declare class UnsupportedVendorError extends Error {
|
|
15
18
|
constructor(vendor: string);
|
|
16
19
|
}
|
|
@@ -22,4 +25,4 @@ declare class UnsupportedSchemaError extends Error {
|
|
|
22
25
|
}
|
|
23
26
|
|
|
24
27
|
export { MissingDependencyError as M, UnsupportedSchemaError as U, UnsupportedVendorError as a };
|
|
25
|
-
export type { ToFormInputFn as T };
|
|
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 };
|