UNPKG

@conform-to/zod

Version:

Conform helpers for integrating with Zod

104 lines (97 loc) 3.5 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _rollupPluginBabelHelpers = require('../_virtual/_rollupPluginBabelHelpers.js'); var coercion = require('./coercion.js'); var constraint = require('./constraint.js'); var format = require('./format.js'); var schema = require('./schema.js'); function defaultDate(text) { var date = new Date(shouldAppendUtcSuffix(text) ? "".concat(text, "Z") : text); if (isNaN(date.getTime())) { throw new Error('Invalid date'); } return date; } function shouldAppendUtcSuffix(datetimeString) { if (datetimeString.includes(' ')) { return false; } var separatorIndex = datetimeString.indexOf('T'); if (separatorIndex < 0) { return false; } var time = datetimeString.slice(separatorIndex + 1); return !(time.toUpperCase().endsWith('Z') || time.includes('+') || time.includes('-')); } function configureCoercion(config) { var _config$type$date, _config$type; return coercion.configureCoercion(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, config), {}, { type: _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, config === null || config === void 0 ? void 0 : config.type), {}, { date: (_config$type$date = config === null || config === void 0 || (_config$type = config.type) === null || _config$type === void 0 ? void 0 : _config$type.date) !== null && _config$type$date !== void 0 ? _config$type$date : defaultDate }) })); } var defaultCoercion = configureCoercion(); /** * Enhances a schema to coerce form values and strip empty values before validation. * Use `configureCoercion` to override empty-string handling and type-specific coercion. * * Results are cached per schema, so this can be called inline. * * **Example:** * * ```tsx * import { coerceFormValue } from '@conform-to/zod/v4/future'; * import { z } from 'zod'; * * const schema = coerceFormValue(z.object({ * age: z.number().optional(), * subscribe: z.boolean(), * })); * * schema.parse({ age: '', subscribe: 'on' }); * // { age: undefined, subscribe: true } * ``` */ var coerceFormValue = defaultCoercion.coerceFormValue; /** * Enhances a schema to coerce form values without running validation. * This is useful for reading current form values as typed data. * * It skips validation, defaults, transforms, and refinements, and does not strip * empty strings to `undefined`. * * For number, boolean, date, and bigint schemas, empty strings and other failed * string coercions still become fallback values: * * - `z.number()` -> `NaN` * - `z.boolean()` -> `false` * - `z.date()` -> `Invalid Date` * - `z.bigint()` -> `0n` * * Use `configureCoercion` to override type-specific coercion. * * Results are cached per schema, so this can be called inline. * * **Example:** * * ```tsx * import { coerceStructure } from '@conform-to/zod/v4/future'; * import { z } from 'zod'; * * const schema = coerceStructure(z.object({ * age: z.number().min(10), * })); * * schema.parse({ age: '3' }); * // { age: 3 } * ``` */ var coerceStructure = defaultCoercion.coerceStructure; exports.getZodConstraint = constraint.getZodConstraint; exports.formatResult = format.formatResult; exports.getConstraints = schema.getConstraints; exports.isSchema = schema.isSchema; exports.coerceFormValue = coerceFormValue; exports.coerceStructure = coerceStructure; exports.configureCoercion = configureCoercion;