UNPKG

@conform-to/zod

Version:

Conform helpers for integrating with Zod

105 lines (100 loc) 4.34 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _rollupPluginBabelHelpers = require('../_virtual/_rollupPluginBabelHelpers.js'); var keys = ['required', 'minLength', 'maxLength', 'min', 'max', 'step', 'multiple', 'pattern']; function getZodConstraint(schema) { function updateConstraint(schema, data) { var _data$name; var name = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; var constraint = name !== '' ? (_data$name = data[name]) !== null && _data$name !== void 0 ? _data$name : data[name] = { required: true } : {}; var def = schema._zod.def; if (def.type === 'object') { for (var key in def.shape) { // @ts-expect-error updateConstraint(def.shape[key], data, name ? "".concat(name, ".").concat(key) : key); } } else if (def.type === 'pipe') { // FIXME: What to do with .pipe()? updateConstraint(def.out, data, name); } else if (def.type === 'intersection') { var leftResult = {}; var rightResult = {}; updateConstraint(def.left, leftResult, name); updateConstraint(def.right, rightResult, name); Object.assign(data, leftResult, rightResult); } else if (def.type === 'union' // || def.type === 'discriminatedUnion' ) { Object.assign(data, def.options.map(option => { var result = {}; updateConstraint(option, result, name); return result; }).reduce((prev, next) => { var list = new Set([...Object.keys(prev), ...Object.keys(next)]); var result = {}; for (var _name of list) { var prevConstraint = prev[_name]; var nextConstraint = next[_name]; if (prevConstraint && nextConstraint) { var _constraint = {}; result[_name] = _constraint; for (var _key of keys) { if (typeof prevConstraint[_key] !== 'undefined' && typeof nextConstraint[_key] !== 'undefined' && prevConstraint[_key] === nextConstraint[_key]) { // @ts-expect-error Both are on the same type _constraint[_key] = prevConstraint[_key]; } } } else { result[_name] = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, prevConstraint), nextConstraint), {}, { required: false }); } } return result; })); } else if (name === '') { // All the cases below are not allowed on root throw new Error('Unsupported schema'); } else if (def.type === 'array') { constraint.multiple = true; updateConstraint(def.element, data, "".concat(name, "[]")); } else if (def.type === 'string') { var _schema = schema; if (_schema._zod.bag.minimum !== null) { var _schema$_zod$bag$mini; constraint.minLength = (_schema$_zod$bag$mini = _schema._zod.bag.minimum) !== null && _schema$_zod$bag$mini !== void 0 ? _schema$_zod$bag$mini : undefined; } if (_schema._zod.bag.maximum !== null) { constraint.maxLength = _schema._zod.bag.maximum; } } else if (def.type === 'optional') { constraint.required = false; updateConstraint(def.innerType, data, name); } else if (def.type === 'default') { constraint.required = false; updateConstraint(def.innerType, data, name); } else if (def.type === 'number') { var _schema2 = schema; if (_schema2._zod.bag.minimum !== null) { constraint.min = _schema2._zod.bag.minimum; } if (_schema2._zod.bag.maximum !== null) { constraint.max = _schema2._zod.bag.maximum; } } else if (def.type === 'enum') { constraint.pattern = Object.keys(def.entries).map(option => // To escape unsafe characters on regex option.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')).join('|'); } else if (def.type === 'tuple') { for (var i = 0; i < def.items.length; i++) { // @ts-expect-error updateConstraint(def.items[i], data, "".concat(name, "[").concat(i, "]")); } } else if (def.type === 'lazy') ; } var result = {}; updateConstraint(schema, result); return result; } exports.getZodConstraint = getZodConstraint;