@conform-to/valibot 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/LICENSE +21 -0
- package/dist/_virtual/_rollupPluginBabelHelpers.js +58 -0
- package/dist/_virtual/_rollupPluginBabelHelpers.mjs +51 -0
- package/dist/coercion.d.ts +62 -0
- package/dist/coercion.js +445 -0
- package/dist/coercion.mjs +441 -0
- package/dist/constraint.d.ts +3 -0
- package/dist/constraint.js +129 -0
- package/dist/constraint.mjs +125 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +14 -0
- package/dist/index.mjs +3 -0
- package/dist/parse.d.ts +16 -0
- package/dist/parse.js +45 -0
- package/dist/parse.mjs +40 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Edmund Hung
|
|
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.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
function ownKeys(e, r) {
|
|
6
|
+
var t = Object.keys(e);
|
|
7
|
+
if (Object.getOwnPropertySymbols) {
|
|
8
|
+
var o = Object.getOwnPropertySymbols(e);
|
|
9
|
+
r && (o = o.filter(function (r) {
|
|
10
|
+
return Object.getOwnPropertyDescriptor(e, r).enumerable;
|
|
11
|
+
})), t.push.apply(t, o);
|
|
12
|
+
}
|
|
13
|
+
return t;
|
|
14
|
+
}
|
|
15
|
+
function _objectSpread2(e) {
|
|
16
|
+
for (var r = 1; r < arguments.length; r++) {
|
|
17
|
+
var t = null != arguments[r] ? arguments[r] : {};
|
|
18
|
+
r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
|
|
19
|
+
_defineProperty(e, r, t[r]);
|
|
20
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
|
|
21
|
+
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return e;
|
|
25
|
+
}
|
|
26
|
+
function _defineProperty(obj, key, value) {
|
|
27
|
+
key = _toPropertyKey(key);
|
|
28
|
+
if (key in obj) {
|
|
29
|
+
Object.defineProperty(obj, key, {
|
|
30
|
+
value: value,
|
|
31
|
+
enumerable: true,
|
|
32
|
+
configurable: true,
|
|
33
|
+
writable: true
|
|
34
|
+
});
|
|
35
|
+
} else {
|
|
36
|
+
obj[key] = value;
|
|
37
|
+
}
|
|
38
|
+
return obj;
|
|
39
|
+
}
|
|
40
|
+
function _toPrimitive(input, hint) {
|
|
41
|
+
if (typeof input !== "object" || input === null) return input;
|
|
42
|
+
var prim = input[Symbol.toPrimitive];
|
|
43
|
+
if (prim !== undefined) {
|
|
44
|
+
var res = prim.call(input, hint || "default");
|
|
45
|
+
if (typeof res !== "object") return res;
|
|
46
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
47
|
+
}
|
|
48
|
+
return (hint === "string" ? String : Number)(input);
|
|
49
|
+
}
|
|
50
|
+
function _toPropertyKey(arg) {
|
|
51
|
+
var key = _toPrimitive(arg, "string");
|
|
52
|
+
return typeof key === "symbol" ? key : String(key);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
exports.defineProperty = _defineProperty;
|
|
56
|
+
exports.objectSpread2 = _objectSpread2;
|
|
57
|
+
exports.toPrimitive = _toPrimitive;
|
|
58
|
+
exports.toPropertyKey = _toPropertyKey;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
function ownKeys(e, r) {
|
|
2
|
+
var t = Object.keys(e);
|
|
3
|
+
if (Object.getOwnPropertySymbols) {
|
|
4
|
+
var o = Object.getOwnPropertySymbols(e);
|
|
5
|
+
r && (o = o.filter(function (r) {
|
|
6
|
+
return Object.getOwnPropertyDescriptor(e, r).enumerable;
|
|
7
|
+
})), t.push.apply(t, o);
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
}
|
|
11
|
+
function _objectSpread2(e) {
|
|
12
|
+
for (var r = 1; r < arguments.length; r++) {
|
|
13
|
+
var t = null != arguments[r] ? arguments[r] : {};
|
|
14
|
+
r % 2 ? ownKeys(Object(t), !0).forEach(function (r) {
|
|
15
|
+
_defineProperty(e, r, t[r]);
|
|
16
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
|
|
17
|
+
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return e;
|
|
21
|
+
}
|
|
22
|
+
function _defineProperty(obj, key, value) {
|
|
23
|
+
key = _toPropertyKey(key);
|
|
24
|
+
if (key in obj) {
|
|
25
|
+
Object.defineProperty(obj, key, {
|
|
26
|
+
value: value,
|
|
27
|
+
enumerable: true,
|
|
28
|
+
configurable: true,
|
|
29
|
+
writable: true
|
|
30
|
+
});
|
|
31
|
+
} else {
|
|
32
|
+
obj[key] = value;
|
|
33
|
+
}
|
|
34
|
+
return obj;
|
|
35
|
+
}
|
|
36
|
+
function _toPrimitive(input, hint) {
|
|
37
|
+
if (typeof input !== "object" || input === null) return input;
|
|
38
|
+
var prim = input[Symbol.toPrimitive];
|
|
39
|
+
if (prim !== undefined) {
|
|
40
|
+
var res = prim.call(input, hint || "default");
|
|
41
|
+
if (typeof res !== "object") return res;
|
|
42
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
43
|
+
}
|
|
44
|
+
return (hint === "string" ? String : Number)(input);
|
|
45
|
+
}
|
|
46
|
+
function _toPropertyKey(arg) {
|
|
47
|
+
var key = _toPrimitive(arg, "string");
|
|
48
|
+
return typeof key === "symbol" ? key : String(key);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export { _defineProperty as defineProperty, _objectSpread2 as objectSpread2, _toPrimitive as toPrimitive, _toPropertyKey as toPropertyKey };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { type GenericSchema, type GenericSchemaAsync, type TransformAction } from 'valibot';
|
|
2
|
+
export type CoercionFunction = (value: unknown) => unknown;
|
|
3
|
+
export type DefaultCoercionType = 'string' | 'file' | 'number' | 'boolean' | 'date' | 'bigint';
|
|
4
|
+
type EnableTypeCoercionOptions = {
|
|
5
|
+
defaultCoercion: Record<DefaultCoercionType, CoercionFunction>;
|
|
6
|
+
defineCoercion: (type: GenericSchema | GenericSchemaAsync) => CoercionFunction | null;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Reconstruct the provided schema with additional preprocessing steps
|
|
10
|
+
* This coerce empty values to undefined and transform strings to the correct type
|
|
11
|
+
*/
|
|
12
|
+
declare function enableTypeCoercion<T extends GenericSchema | GenericSchemaAsync>(type: T, options: EnableTypeCoercionOptions): {
|
|
13
|
+
transformAction: TransformAction<unknown, unknown> | undefined;
|
|
14
|
+
schema: T extends GenericSchema<unknown, infer Output, infer Issue> ? GenericSchema<unknown, Output, Issue> : T extends GenericSchemaAsync<unknown, infer OutputAsync, infer IssueAsync> ? GenericSchemaAsync<unknown, OutputAsync, IssueAsync> : never;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* A helper that enhance the valibot schema to strip empty value and coerce form value to the expected type with option to customize type coercion.
|
|
18
|
+
* @example
|
|
19
|
+
*
|
|
20
|
+
* ```tsx
|
|
21
|
+
* import { parseWithValibot, unstable_coerceFormValue as coerceFormValue } from '@conform-to/valibot';
|
|
22
|
+
* import { object, number, date, boolean } from 'valibot';
|
|
23
|
+
*
|
|
24
|
+
* // To coerce the form value with default behaviour
|
|
25
|
+
* const schema = coerceFormValue(
|
|
26
|
+
* object({
|
|
27
|
+
* ref: number(),
|
|
28
|
+
* date: date(),
|
|
29
|
+
* amount: number(),
|
|
30
|
+
* confirm: boolean(),
|
|
31
|
+
* })
|
|
32
|
+
* );
|
|
33
|
+
*
|
|
34
|
+
* // To coerce the form value with number type disabled
|
|
35
|
+
* const schema = coerceFormValue(
|
|
36
|
+
* object({
|
|
37
|
+
* ref: number(),
|
|
38
|
+
* date: date(),
|
|
39
|
+
* amount: number(),
|
|
40
|
+
* confirm: boolean(),
|
|
41
|
+
* }),
|
|
42
|
+
* {
|
|
43
|
+
* defaultCoercion: {
|
|
44
|
+
* number: false,
|
|
45
|
+
* },
|
|
46
|
+
* defineCoercion: (schema) => {
|
|
47
|
+
* if (schema.type === 'string') {
|
|
48
|
+
* return (value) => value.trim();
|
|
49
|
+
* }
|
|
50
|
+
* return null;
|
|
51
|
+
* },
|
|
52
|
+
* },
|
|
53
|
+
* );
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export declare function coerceFormValue<T extends GenericSchema | GenericSchemaAsync>(type: T, options?: {
|
|
57
|
+
defaultCoercion?: {
|
|
58
|
+
[key in DefaultCoercionType]?: CoercionFunction | boolean;
|
|
59
|
+
};
|
|
60
|
+
defineCoercion?: (type: GenericSchema | GenericSchemaAsync) => CoercionFunction | null;
|
|
61
|
+
}): ReturnType<typeof enableTypeCoercion>;
|
|
62
|
+
export {};
|
package/dist/coercion.js
ADDED
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js');
|
|
6
|
+
var valibot = require('valibot');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Reconstruct the provided schema with additional preprocessing steps
|
|
10
|
+
* This coerce empty values to undefined and transform strings to the correct type
|
|
11
|
+
* @param type The schema to be coerced
|
|
12
|
+
* @param transform The transformation function
|
|
13
|
+
* @returns The transform action and the coerced schema
|
|
14
|
+
*/
|
|
15
|
+
function coerce(type, transformFn) {
|
|
16
|
+
// `expects` is required to generate error messages for `TupleSchema`, so it is passed to `UnknownSchema` for coercion.
|
|
17
|
+
var unknown = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, valibot.unknown()), {}, {
|
|
18
|
+
expects: type.expects
|
|
19
|
+
});
|
|
20
|
+
var transformAction = valibot.transform(transformFn);
|
|
21
|
+
var schema = type.async ? valibot.pipeAsync(unknown, transformAction, type) : valibot.pipe(unknown, transformAction, type);
|
|
22
|
+
return {
|
|
23
|
+
transformAction,
|
|
24
|
+
schema
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Strip empty strings and convert them to undefined.
|
|
30
|
+
*
|
|
31
|
+
* @param value The value to be checked and coerced.
|
|
32
|
+
* @returns The coerced value or undefined if the input is an empty string.
|
|
33
|
+
*/
|
|
34
|
+
function stripEmptyString(value) {
|
|
35
|
+
if (typeof value !== 'string') {
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
if (value === '') {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Strip empty files and convert them to undefined.
|
|
46
|
+
*
|
|
47
|
+
* @param file The file to be checked and coerced.
|
|
48
|
+
* @returns The coerced value or undefined if the input is an empty file.
|
|
49
|
+
*/
|
|
50
|
+
function stripEmptyFile(file) {
|
|
51
|
+
if (typeof File !== 'undefined' && file instanceof File && file.name === '' && file.size === 0) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
return file;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Helpers for coercing number value
|
|
59
|
+
* Modify the value only if it's a string, otherwise return the value as-is
|
|
60
|
+
*/
|
|
61
|
+
function coerceNumber(value) {
|
|
62
|
+
if (typeof value !== 'string') {
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
return value.trim() === '' ? value : Number(value);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Helpers for coercing boolean value
|
|
70
|
+
* Modify the value only if it's a string, otherwise return the value as-is
|
|
71
|
+
*/
|
|
72
|
+
function coerceBoolean(value) {
|
|
73
|
+
if (typeof value !== 'string') {
|
|
74
|
+
return value;
|
|
75
|
+
}
|
|
76
|
+
return value === 'on' ? true : value;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Helpers for coercing date value
|
|
81
|
+
* Modify the value only if it's a string, otherwise return the value as-is
|
|
82
|
+
*/
|
|
83
|
+
function coerceDate(value) {
|
|
84
|
+
if (typeof value !== 'string') {
|
|
85
|
+
return value;
|
|
86
|
+
}
|
|
87
|
+
var date = new Date(value);
|
|
88
|
+
if (Number.isNaN(date.getTime())) {
|
|
89
|
+
return value;
|
|
90
|
+
}
|
|
91
|
+
return date;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Helpers for coercing bigint value
|
|
96
|
+
* Modify the value only if it's a string, otherwise return the value as-is
|
|
97
|
+
*/
|
|
98
|
+
function coerceBigInt(value) {
|
|
99
|
+
if (typeof value !== 'string') {
|
|
100
|
+
return value;
|
|
101
|
+
}
|
|
102
|
+
if (value.trim() === '') {
|
|
103
|
+
return value;
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
return BigInt(value);
|
|
107
|
+
} catch (_unused) {
|
|
108
|
+
return value;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Helpers for coercing array value
|
|
114
|
+
* Modify the value only if it's an array, otherwise return the value as-is
|
|
115
|
+
*/
|
|
116
|
+
function coerceArray(type) {
|
|
117
|
+
// `expects` is required to generate error messages for `TupleSchema`, so it is passed to `UnkonwSchema` for coercion.
|
|
118
|
+
var unknown = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, valibot.unknown()), {}, {
|
|
119
|
+
expects: type.expects
|
|
120
|
+
});
|
|
121
|
+
var transformFunction = output => {
|
|
122
|
+
if (Array.isArray(output)) {
|
|
123
|
+
return output;
|
|
124
|
+
}
|
|
125
|
+
if (typeof output === 'undefined' || typeof stripEmptyFile(stripEmptyString(output)) === 'undefined') {
|
|
126
|
+
return [];
|
|
127
|
+
}
|
|
128
|
+
return [output];
|
|
129
|
+
};
|
|
130
|
+
if (type.async) {
|
|
131
|
+
return valibot.pipeAsync(unknown, valibot.transform(transformFunction), type);
|
|
132
|
+
}
|
|
133
|
+
return valibot.pipe(unknown, valibot.transform(transformFunction), type);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Compose two coercion functions
|
|
138
|
+
* @param a The first coercion function
|
|
139
|
+
* @param b The second coercion function
|
|
140
|
+
* @returns The composed coercion function
|
|
141
|
+
*/
|
|
142
|
+
function compose(a, b) {
|
|
143
|
+
return value => b(a(value));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Generate a wrapped schema with coercion
|
|
148
|
+
* @param type The schema to be coerced
|
|
149
|
+
* @param options The options for coercion
|
|
150
|
+
* @param rewrap Whether the result schema should be rewrapped with the `type` schema.
|
|
151
|
+
* See <https://github.com/chimame/conform-to-valibot/issues/53> for cases that need rewrapping.
|
|
152
|
+
* @returns The coerced schema
|
|
153
|
+
*/
|
|
154
|
+
function generateWrappedSchema(type, options) {
|
|
155
|
+
var rewrap = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
156
|
+
var {
|
|
157
|
+
transformAction,
|
|
158
|
+
schema: wrapSchema
|
|
159
|
+
} = enableTypeCoercion(
|
|
160
|
+
// @ts-expect-error
|
|
161
|
+
type.wrapped, options);
|
|
162
|
+
if (transformAction) {
|
|
163
|
+
// `expects` is required to generate error messages for `TupleSchema`, so it is passed to `UnkonwSchema` for coercion.
|
|
164
|
+
var unknown = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, valibot.unknown()), {}, {
|
|
165
|
+
expects: type.expects
|
|
166
|
+
});
|
|
167
|
+
var schema = type.async ? valibot.pipeAsync(unknown, transformAction, type) : valibot.pipe(unknown, transformAction, type);
|
|
168
|
+
var default_ = 'default' in type ? type.default : undefined;
|
|
169
|
+
if (rewrap) {
|
|
170
|
+
return {
|
|
171
|
+
transformAction: undefined,
|
|
172
|
+
schema: type.reference(schema, default_)
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
transformAction,
|
|
177
|
+
schema
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
var wrappedSchema = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type), {}, {
|
|
181
|
+
wrapped: wrapSchema
|
|
182
|
+
});
|
|
183
|
+
return {
|
|
184
|
+
transformAction: undefined,
|
|
185
|
+
schema: wrappedSchema
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Reconstruct the provided schema with additional preprocessing steps
|
|
191
|
+
* This coerce empty values to undefined and transform strings to the correct type
|
|
192
|
+
*/
|
|
193
|
+
|
|
194
|
+
function enableTypeCoercion(type, options) {
|
|
195
|
+
if ('pipe' in type) {
|
|
196
|
+
var {
|
|
197
|
+
transformAction,
|
|
198
|
+
schema: coercedSchema
|
|
199
|
+
} = enableTypeCoercion(type.pipe[0], options);
|
|
200
|
+
var schema = type.async ? valibot.pipeAsync(coercedSchema, ...type.pipe.slice(1)) :
|
|
201
|
+
// @ts-expect-error `coercedSchema` must be sync here but TypeScript can't infer that.
|
|
202
|
+
valibot.pipe(coercedSchema, ...type.pipe.slice(1));
|
|
203
|
+
return {
|
|
204
|
+
transformAction,
|
|
205
|
+
schema
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
var defineCoercionFn = options.defineCoercion(type);
|
|
209
|
+
if (defineCoercionFn) {
|
|
210
|
+
return coerce(type, defineCoercionFn);
|
|
211
|
+
}
|
|
212
|
+
switch (type.type) {
|
|
213
|
+
case 'string':
|
|
214
|
+
case 'literal':
|
|
215
|
+
case 'enum':
|
|
216
|
+
case 'undefined':
|
|
217
|
+
{
|
|
218
|
+
return coerce(type, options.defaultCoercion.string);
|
|
219
|
+
}
|
|
220
|
+
case 'number':
|
|
221
|
+
{
|
|
222
|
+
return coerce(type, options.defaultCoercion.number);
|
|
223
|
+
}
|
|
224
|
+
case 'boolean':
|
|
225
|
+
{
|
|
226
|
+
return coerce(type, options.defaultCoercion.boolean);
|
|
227
|
+
}
|
|
228
|
+
case 'date':
|
|
229
|
+
{
|
|
230
|
+
return coerce(type, options.defaultCoercion.date);
|
|
231
|
+
}
|
|
232
|
+
case 'bigint':
|
|
233
|
+
{
|
|
234
|
+
return coerce(type, options.defaultCoercion.bigint);
|
|
235
|
+
}
|
|
236
|
+
case 'file':
|
|
237
|
+
case 'blob':
|
|
238
|
+
{
|
|
239
|
+
return coerce(type, options.defaultCoercion.file);
|
|
240
|
+
}
|
|
241
|
+
case 'array':
|
|
242
|
+
{
|
|
243
|
+
var arraySchema = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type), {}, {
|
|
244
|
+
// @ts-expect-error
|
|
245
|
+
item: enableTypeCoercion(type.item, options).schema
|
|
246
|
+
});
|
|
247
|
+
return {
|
|
248
|
+
transformAction: undefined,
|
|
249
|
+
schema: coerceArray(arraySchema)
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
case 'exact_optional':
|
|
253
|
+
{
|
|
254
|
+
// @ts-expect-error
|
|
255
|
+
var {
|
|
256
|
+
schema: wrapSchema
|
|
257
|
+
} = enableTypeCoercion(type.wrapped, options);
|
|
258
|
+
var exactOptionalSchema = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type), {}, {
|
|
259
|
+
wrapped: wrapSchema
|
|
260
|
+
});
|
|
261
|
+
return {
|
|
262
|
+
transformAction: undefined,
|
|
263
|
+
schema: exactOptionalSchema
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
case 'nullish':
|
|
267
|
+
case 'optional':
|
|
268
|
+
{
|
|
269
|
+
return generateWrappedSchema(type, options, true);
|
|
270
|
+
}
|
|
271
|
+
case 'undefinedable':
|
|
272
|
+
case 'nullable':
|
|
273
|
+
case 'non_optional':
|
|
274
|
+
case 'non_nullish':
|
|
275
|
+
case 'non_nullable':
|
|
276
|
+
{
|
|
277
|
+
return generateWrappedSchema(type, options);
|
|
278
|
+
}
|
|
279
|
+
case 'union':
|
|
280
|
+
case 'intersect':
|
|
281
|
+
{
|
|
282
|
+
var unionSchema = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type), {}, {
|
|
283
|
+
// @ts-expect-error
|
|
284
|
+
options: type.options.map(
|
|
285
|
+
// @ts-expect-error
|
|
286
|
+
option => enableTypeCoercion(option, options).schema)
|
|
287
|
+
});
|
|
288
|
+
return {
|
|
289
|
+
transformAction: undefined,
|
|
290
|
+
schema: unionSchema
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
case 'variant':
|
|
294
|
+
{
|
|
295
|
+
var variantSchema = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type), {}, {
|
|
296
|
+
// @ts-expect-error
|
|
297
|
+
options: type.options.map(
|
|
298
|
+
// @ts-expect-error
|
|
299
|
+
option => enableTypeCoercion(option, options).schema)
|
|
300
|
+
});
|
|
301
|
+
return {
|
|
302
|
+
transformAction: undefined,
|
|
303
|
+
schema: variantSchema
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
case 'tuple':
|
|
307
|
+
{
|
|
308
|
+
var tupleSchema = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type), {}, {
|
|
309
|
+
// @ts-expect-error
|
|
310
|
+
items: type.items.map(
|
|
311
|
+
// @ts-expect-error
|
|
312
|
+
option => enableTypeCoercion(option, options).schema)
|
|
313
|
+
});
|
|
314
|
+
return {
|
|
315
|
+
transformAction: undefined,
|
|
316
|
+
schema: tupleSchema
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
case 'tuple_with_rest':
|
|
320
|
+
{
|
|
321
|
+
var tupleWithRestSchema = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type), {}, {
|
|
322
|
+
// @ts-expect-error
|
|
323
|
+
items: type.items.map(
|
|
324
|
+
// @ts-expect-error
|
|
325
|
+
option => enableTypeCoercion(option, options).schema),
|
|
326
|
+
// @ts-expect-error
|
|
327
|
+
rest: enableTypeCoercion(type.rest, options).schema
|
|
328
|
+
});
|
|
329
|
+
return {
|
|
330
|
+
transformAction: undefined,
|
|
331
|
+
schema: tupleWithRestSchema
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
case 'loose_object':
|
|
335
|
+
case 'strict_object':
|
|
336
|
+
case 'object':
|
|
337
|
+
{
|
|
338
|
+
var objectSchema = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type), {}, {
|
|
339
|
+
entries: Object.fromEntries(
|
|
340
|
+
// @ts-expect-error
|
|
341
|
+
Object.entries(type.entries).map(_ref => {
|
|
342
|
+
var [key, def] = _ref;
|
|
343
|
+
return [key, enableTypeCoercion(def, options).schema];
|
|
344
|
+
}))
|
|
345
|
+
});
|
|
346
|
+
return {
|
|
347
|
+
transformAction: undefined,
|
|
348
|
+
schema: objectSchema
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
case 'object_with_rest':
|
|
352
|
+
{
|
|
353
|
+
var objectWithRestSchema = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type), {}, {
|
|
354
|
+
entries: Object.fromEntries(
|
|
355
|
+
// @ts-expect-error
|
|
356
|
+
Object.entries(type.entries).map(_ref2 => {
|
|
357
|
+
var [key, def] = _ref2;
|
|
358
|
+
return [key, enableTypeCoercion(def, options).schema];
|
|
359
|
+
})),
|
|
360
|
+
// @ts-expect-error
|
|
361
|
+
rest: enableTypeCoercion(type.rest, options).schema
|
|
362
|
+
});
|
|
363
|
+
return {
|
|
364
|
+
transformAction: undefined,
|
|
365
|
+
schema: objectWithRestSchema
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return coerce(type, value => value);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* A helper that enhance the valibot schema to strip empty value and coerce form value to the expected type with option to customize type coercion.
|
|
374
|
+
* @example
|
|
375
|
+
*
|
|
376
|
+
* ```tsx
|
|
377
|
+
* import { parseWithValibot, unstable_coerceFormValue as coerceFormValue } from '@conform-to/valibot';
|
|
378
|
+
* import { object, number, date, boolean } from 'valibot';
|
|
379
|
+
*
|
|
380
|
+
* // To coerce the form value with default behaviour
|
|
381
|
+
* const schema = coerceFormValue(
|
|
382
|
+
* object({
|
|
383
|
+
* ref: number(),
|
|
384
|
+
* date: date(),
|
|
385
|
+
* amount: number(),
|
|
386
|
+
* confirm: boolean(),
|
|
387
|
+
* })
|
|
388
|
+
* );
|
|
389
|
+
*
|
|
390
|
+
* // To coerce the form value with number type disabled
|
|
391
|
+
* const schema = coerceFormValue(
|
|
392
|
+
* object({
|
|
393
|
+
* ref: number(),
|
|
394
|
+
* date: date(),
|
|
395
|
+
* amount: number(),
|
|
396
|
+
* confirm: boolean(),
|
|
397
|
+
* }),
|
|
398
|
+
* {
|
|
399
|
+
* defaultCoercion: {
|
|
400
|
+
* number: false,
|
|
401
|
+
* },
|
|
402
|
+
* defineCoercion: (schema) => {
|
|
403
|
+
* if (schema.type === 'string') {
|
|
404
|
+
* return (value) => value.trim();
|
|
405
|
+
* }
|
|
406
|
+
* return null;
|
|
407
|
+
* },
|
|
408
|
+
* },
|
|
409
|
+
* );
|
|
410
|
+
* ```
|
|
411
|
+
*/
|
|
412
|
+
function coerceFormValue(type, options) {
|
|
413
|
+
var _options$defaultCoerc, _options$defaultCoerc2, _options$defaultCoerc3, _options$defaultCoerc4, _options$defaultCoerc5, _options$defaultCoerc6, _options$defineCoerci;
|
|
414
|
+
return enableTypeCoercion(type, {
|
|
415
|
+
defaultCoercion: {
|
|
416
|
+
string: compose(stripEmptyString, getCoercion(options === null || options === void 0 || (_options$defaultCoerc = options.defaultCoercion) === null || _options$defaultCoerc === void 0 ? void 0 : _options$defaultCoerc.string)),
|
|
417
|
+
file: compose(stripEmptyFile, getCoercion(options === null || options === void 0 || (_options$defaultCoerc2 = options.defaultCoercion) === null || _options$defaultCoerc2 === void 0 ? void 0 : _options$defaultCoerc2.file)),
|
|
418
|
+
number: compose(stripEmptyString, getCoercion(options === null || options === void 0 || (_options$defaultCoerc3 = options.defaultCoercion) === null || _options$defaultCoerc3 === void 0 ? void 0 : _options$defaultCoerc3.number, coerceNumber)),
|
|
419
|
+
boolean: compose(stripEmptyString, getCoercion(options === null || options === void 0 || (_options$defaultCoerc4 = options.defaultCoercion) === null || _options$defaultCoerc4 === void 0 ? void 0 : _options$defaultCoerc4.boolean, coerceBoolean)),
|
|
420
|
+
date: compose(stripEmptyString, getCoercion(options === null || options === void 0 || (_options$defaultCoerc5 = options.defaultCoercion) === null || _options$defaultCoerc5 === void 0 ? void 0 : _options$defaultCoerc5.date, coerceDate)),
|
|
421
|
+
bigint: compose(stripEmptyString, getCoercion(options === null || options === void 0 || (_options$defaultCoerc6 = options.defaultCoercion) === null || _options$defaultCoerc6 === void 0 ? void 0 : _options$defaultCoerc6.bigint, coerceBigInt))
|
|
422
|
+
},
|
|
423
|
+
defineCoercion: (_options$defineCoerci = options === null || options === void 0 ? void 0 : options.defineCoercion) !== null && _options$defineCoerci !== void 0 ? _options$defineCoerci : () => null
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/**
|
|
428
|
+
* Get the coercion function from the provided coercion option
|
|
429
|
+
* @param providedCoercion The provided coercion option
|
|
430
|
+
* @param fallbackCoercion The fallback coercion function
|
|
431
|
+
* @returns The coercion function
|
|
432
|
+
*/
|
|
433
|
+
var getCoercion = (providedCoercion, fallbackCoercion) => {
|
|
434
|
+
if (typeof providedCoercion === 'function') {
|
|
435
|
+
return providedCoercion;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// If the user explicitly disabled the coercion or no fallback coercion, return a noop function
|
|
439
|
+
if (providedCoercion === false || fallbackCoercion === undefined) {
|
|
440
|
+
return value => value;
|
|
441
|
+
}
|
|
442
|
+
return fallbackCoercion;
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
exports.coerceFormValue = coerceFormValue;
|