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