@conform-to/zod
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -1,2 +0,3 @@ | ||
import { type ZodType, type ZodTypeAny, type output, ZodEffects } from 'zod'; | ||
import { ZodEffects } from 'zod'; | ||
import type { ZodType, ZodTypeAny, output } from 'zod'; | ||
/** | ||
@@ -3,0 +4,0 @@ * Helpers for coercing string value |
@@ -45,3 +45,3 @@ 'use strict'; | ||
} | ||
return schema._def.effect.type === 'refinement' && schema.innerType() instanceof zod.ZodAny && schema.safeParse(new File([], '')).success && !schema.safeParse('').success; | ||
return schema._def.effect.type === 'refinement' && schema.innerType()._def.typeName === 'ZodAny' && schema.safeParse(new File([], '')).success && !schema.safeParse('').success; | ||
} | ||
@@ -63,9 +63,10 @@ | ||
var schema = type; | ||
if (type instanceof zod.ZodString || type instanceof zod.ZodLiteral || type instanceof zod.ZodEnum || type instanceof zod.ZodNativeEnum) { | ||
var def = type._def; | ||
if (def.typeName === 'ZodString' || def.typeName === 'ZodLiteral' || def.typeName === 'ZodEnum' || def.typeName === 'ZodNativeEnum') { | ||
schema = zod.any().transform(value => coerceString(value)).pipe(type); | ||
} else if (type instanceof zod.ZodNumber) { | ||
} else if (def.typeName === 'ZodNumber') { | ||
schema = zod.any().transform(value => coerceString(value, text => text.trim() === '' ? Number.NaN : Number(text))).pipe(type); | ||
} else if (type instanceof zod.ZodBoolean) { | ||
} else if (def.typeName === 'ZodBoolean') { | ||
schema = zod.any().transform(value => coerceString(value, text => text === 'on' ? true : text)).pipe(type); | ||
} else if (type instanceof zod.ZodDate) { | ||
} else if (def.typeName === 'ZodDate') { | ||
schema = zod.any().transform(value => coerceString(value, timestamp => { | ||
@@ -82,5 +83,5 @@ var date = new Date(timestamp); | ||
})).pipe(type); | ||
} else if (type instanceof zod.ZodBigInt) { | ||
} else if (def.typeName === 'ZodBigInt') { | ||
schema = zod.any().transform(value => coerceString(value, BigInt)).pipe(type); | ||
} else if (type instanceof zod.ZodArray) { | ||
} else if (def.typeName === 'ZodArray') { | ||
schema = zod.any().transform(value => { | ||
@@ -97,7 +98,7 @@ // No preprocess needed if the value is already an array | ||
return [value]; | ||
}).pipe(new zod.ZodArray(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type._def), {}, { | ||
type: enableTypeCoercion(type.element, cache) | ||
}).pipe(new zod.ZodArray(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, def), {}, { | ||
type: enableTypeCoercion(def.type, cache) | ||
}))); | ||
} else if (type instanceof zod.ZodObject) { | ||
var _shape = Object.fromEntries(Object.entries(type.shape).map(_ref => { | ||
} else if (def.typeName === 'ZodObject') { | ||
var _shape = Object.fromEntries(Object.entries(def.shape()).map(_ref => { | ||
var [key, def] = _ref; | ||
@@ -108,38 +109,38 @@ return [key, | ||
})); | ||
schema = new zod.ZodObject(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type._def), {}, { | ||
schema = new zod.ZodObject(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, def), {}, { | ||
shape: () => _shape | ||
})); | ||
} else if (type instanceof zod.ZodEffects) { | ||
} else if (def.typeName === 'ZodEffects') { | ||
if (isFileSchema(type)) { | ||
schema = zod.any().transform(value => coerceFile(value)).pipe(type); | ||
} else { | ||
schema = new zod.ZodEffects(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type._def), {}, { | ||
schema: enableTypeCoercion(type.innerType(), cache) | ||
schema = new zod.ZodEffects(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, def), {}, { | ||
schema: enableTypeCoercion(def.schema, cache) | ||
})); | ||
} | ||
} else if (type instanceof zod.ZodOptional) { | ||
schema = zod.any().transform(value => coerceFile(coerceString(value))).pipe(new zod.ZodOptional(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type._def), {}, { | ||
innerType: enableTypeCoercion(type.unwrap(), cache) | ||
} else if (def.typeName === 'ZodOptional') { | ||
schema = zod.any().transform(value => coerceFile(coerceString(value))).pipe(new zod.ZodOptional(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, def), {}, { | ||
innerType: enableTypeCoercion(def.innerType, cache) | ||
}))); | ||
} else if (type instanceof zod.ZodDefault) { | ||
schema = zod.any().transform(value => coerceFile(coerceString(value))).pipe(new zod.ZodDefault(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type._def), {}, { | ||
innerType: enableTypeCoercion(type.removeDefault(), cache) | ||
} else if (def.typeName === 'ZodDefault') { | ||
schema = zod.any().transform(value => coerceFile(coerceString(value))).pipe(new zod.ZodDefault(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, def), {}, { | ||
innerType: enableTypeCoercion(def.innerType, cache) | ||
}))); | ||
} else if (type instanceof zod.ZodCatch) { | ||
schema = new zod.ZodCatch(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type._def), {}, { | ||
innerType: enableTypeCoercion(type.removeCatch(), cache) | ||
} else if (def.typeName === 'ZodCatch') { | ||
schema = new zod.ZodCatch(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, def), {}, { | ||
innerType: enableTypeCoercion(def.innerType, cache) | ||
})); | ||
} else if (type instanceof zod.ZodIntersection) { | ||
schema = new zod.ZodIntersection(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type._def), {}, { | ||
left: enableTypeCoercion(type._def.left, cache), | ||
right: enableTypeCoercion(type._def.right, cache) | ||
} else if (def.typeName === 'ZodIntersection') { | ||
schema = new zod.ZodIntersection(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, def), {}, { | ||
left: enableTypeCoercion(def.left, cache), | ||
right: enableTypeCoercion(def.right, cache) | ||
})); | ||
} else if (type instanceof zod.ZodUnion) { | ||
schema = new zod.ZodUnion(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type._def), {}, { | ||
options: type.options.map(option => enableTypeCoercion(option, cache)) | ||
} else if (def.typeName === 'ZodUnion') { | ||
schema = new zod.ZodUnion(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, def), {}, { | ||
options: def.options.map(option => enableTypeCoercion(option, cache)) | ||
})); | ||
} else if (type instanceof zod.ZodDiscriminatedUnion) { | ||
schema = new zod.ZodDiscriminatedUnion(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type._def), {}, { | ||
options: type.options.map(option => enableTypeCoercion(option, cache)), | ||
optionsMap: new Map(Array.from(type.optionsMap.entries()).map(_ref2 => { | ||
} else if (def.typeName === 'ZodDiscriminatedUnion') { | ||
schema = new zod.ZodDiscriminatedUnion(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, def), {}, { | ||
options: def.options.map(option => enableTypeCoercion(option, cache)), | ||
optionsMap: new Map(Array.from(def.optionsMap.entries()).map(_ref2 => { | ||
var [discriminator, option] = _ref2; | ||
@@ -149,17 +150,18 @@ return [discriminator, enableTypeCoercion(option, cache)]; | ||
})); | ||
} else if (type instanceof zod.ZodTuple) { | ||
schema = new zod.ZodTuple(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type._def), {}, { | ||
items: type.items.map(item => enableTypeCoercion(item, cache)) | ||
} else if (def.typeName === 'ZodTuple') { | ||
schema = new zod.ZodTuple(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, def), {}, { | ||
items: def.items.map(item => enableTypeCoercion(item, cache)) | ||
})); | ||
} else if (type instanceof zod.ZodNullable) { | ||
schema = new zod.ZodNullable(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type._def), {}, { | ||
innerType: enableTypeCoercion(type.unwrap(), cache) | ||
} else if (def.typeName === 'ZodNullable') { | ||
schema = new zod.ZodNullable(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, def), {}, { | ||
innerType: enableTypeCoercion(def.innerType, cache) | ||
})); | ||
} else if (type instanceof zod.ZodPipeline) { | ||
schema = new zod.ZodPipeline(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type._def), {}, { | ||
in: enableTypeCoercion(type._def.in, cache), | ||
out: enableTypeCoercion(type._def.out, cache) | ||
} else if (def.typeName === 'ZodPipeline') { | ||
schema = new zod.ZodPipeline(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, def), {}, { | ||
in: enableTypeCoercion(def.in, cache), | ||
out: enableTypeCoercion(def.out, cache) | ||
})); | ||
} else if (type instanceof zod.ZodLazy) { | ||
schema = zod.lazy(() => enableTypeCoercion(type.schema, cache)); | ||
} else if (def.typeName === 'ZodLazy') { | ||
var inner = def.getter(); | ||
schema = zod.lazy(() => enableTypeCoercion(inner, cache)); | ||
} | ||
@@ -166,0 +168,0 @@ if (type !== schema) { |
import type { Constraint } from '@conform-to/dom'; | ||
import { type ZodTypeAny } from 'zod'; | ||
import type { ZodTypeAny } from 'zod'; | ||
export declare function getZodConstraint(schema: ZodTypeAny): Record<string, Constraint>; |
@@ -6,3 +6,2 @@ 'use strict'; | ||
var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js'); | ||
var zod = require('zod'); | ||
@@ -17,19 +16,20 @@ var keys = ['required', 'minLength', 'maxLength', 'min', 'max', 'step', 'multiple', 'pattern']; | ||
} : {}; | ||
if (schema instanceof zod.ZodObject) { | ||
for (var key in schema.shape) { | ||
updateConstraint(schema.shape[key], data, name ? "".concat(name, ".").concat(key) : key); | ||
var def = schema['_def']; | ||
if (def.typeName === 'ZodObject') { | ||
for (var key in def.shape()) { | ||
updateConstraint(def.shape()[key], data, name ? "".concat(name, ".").concat(key) : key); | ||
} | ||
} else if (schema instanceof zod.ZodEffects) { | ||
updateConstraint(schema.innerType(), data, name); | ||
} else if (schema instanceof zod.ZodPipeline) { | ||
} else if (def.typeName === 'ZodEffects') { | ||
updateConstraint(def.schema, data, name); | ||
} else if (def.typeName === 'ZodPipeline') { | ||
// FIXME: What to do with .pipe()? | ||
updateConstraint(schema._def.out, data, name); | ||
} else if (schema instanceof zod.ZodIntersection) { | ||
updateConstraint(def.out, data, name); | ||
} else if (def.typeName === 'ZodIntersection') { | ||
var leftResult = {}; | ||
var rightResult = {}; | ||
updateConstraint(schema._def.left, leftResult, name); | ||
updateConstraint(schema._def.right, rightResult, name); | ||
updateConstraint(def.left, leftResult, name); | ||
updateConstraint(def.right, rightResult, name); | ||
Object.assign(data, leftResult, rightResult); | ||
} else if (schema instanceof zod.ZodUnion || schema instanceof zod.ZodDiscriminatedUnion) { | ||
Object.assign(data, schema.options.map(option => { | ||
} else if (def.typeName === 'ZodUnion' || def.typeName === 'ZodDiscriminatedUnion') { | ||
Object.assign(data, def.options.map(option => { | ||
var result = {}; | ||
@@ -64,34 +64,37 @@ updateConstraint(option, result, name); | ||
throw new Error('Unsupported schema'); | ||
} else if (schema instanceof zod.ZodArray) { | ||
} else if (def.typeName === 'ZodArray') { | ||
constraint.multiple = true; | ||
updateConstraint(schema.element, data, "".concat(name, "[]")); | ||
} else if (schema instanceof zod.ZodString) { | ||
if (schema.minLength !== null) { | ||
constraint.minLength = schema.minLength; | ||
updateConstraint(def.type, data, "".concat(name, "[]")); | ||
} else if (def.typeName === 'ZodString') { | ||
var _schema = schema; | ||
if (_schema.minLength !== null) { | ||
var _schema$minLength; | ||
constraint.minLength = (_schema$minLength = _schema.minLength) !== null && _schema$minLength !== void 0 ? _schema$minLength : undefined; | ||
} | ||
if (schema.maxLength !== null) { | ||
constraint.maxLength = schema.maxLength; | ||
if (_schema.maxLength !== null) { | ||
constraint.maxLength = _schema.maxLength; | ||
} | ||
} else if (schema instanceof zod.ZodOptional) { | ||
} else if (def.typeName === 'ZodOptional') { | ||
constraint.required = false; | ||
updateConstraint(schema.unwrap(), data, name); | ||
} else if (schema instanceof zod.ZodDefault) { | ||
updateConstraint(def.innerType, data, name); | ||
} else if (def.typeName === 'ZodDefault') { | ||
constraint.required = false; | ||
updateConstraint(schema.removeDefault(), data, name); | ||
} else if (schema instanceof zod.ZodNumber) { | ||
if (schema.minValue !== null) { | ||
constraint.min = schema.minValue; | ||
updateConstraint(def.innerType, data, name); | ||
} else if (def.typeName === 'ZodNumber') { | ||
var _schema2 = schema; | ||
if (_schema2.minValue !== null) { | ||
constraint.min = _schema2.minValue; | ||
} | ||
if (schema.maxValue !== null) { | ||
constraint.max = schema.maxValue; | ||
if (_schema2.maxValue !== null) { | ||
constraint.max = _schema2.maxValue; | ||
} | ||
} else if (schema instanceof zod.ZodEnum) { | ||
constraint.pattern = schema.options.map(option => | ||
} else if (def.typeName === 'ZodEnum') { | ||
constraint.pattern = def.values.map(option => | ||
// To escape unsafe characters on regex | ||
option.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')).join('|'); | ||
} else if (schema instanceof zod.ZodTuple) { | ||
for (var i = 0; i < schema.items.length; i++) { | ||
updateConstraint(schema.items[i], data, "".concat(name, "[").concat(i, "]")); | ||
} else if (def.typeName === 'ZodTuple') { | ||
for (var i = 0; i < def.items.length; i++) { | ||
updateConstraint(def.items[i], data, "".concat(name, "[").concat(i, "]")); | ||
} | ||
} else ; | ||
} else if (def.typeName === 'ZodLazy') ; | ||
} | ||
@@ -98,0 +101,0 @@ var result = {}; |
@@ -6,3 +6,3 @@ { | ||
"license": "MIT", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"main": "index.js", | ||
@@ -20,2 +20,11 @@ "module": "index.mjs", | ||
}, | ||
"scripts": { | ||
"build:js": "rollup -c", | ||
"build:ts": "tsc", | ||
"build": "pnpm run \"/^build:.*/\"", | ||
"dev:js": "pnpm run build:js --watch", | ||
"dev:ts": "pnpm run build:ts --watch", | ||
"dev": "pnpm run \"/^dev:.*/\"", | ||
"prepare": "pnpm run build" | ||
}, | ||
"repository": { | ||
@@ -30,3 +39,3 @@ "type": "git", | ||
"dependencies": { | ||
"@conform-to/dom": "1.1.2" | ||
"@conform-to/dom": "1.1.3" | ||
}, | ||
@@ -37,2 +46,9 @@ "peerDependencies": { | ||
"devDependencies": { | ||
"@babel/core": "^7.17.8", | ||
"@babel/preset-env": "^7.20.2", | ||
"@babel/preset-typescript": "^7.20.2", | ||
"@rollup/plugin-babel": "^5.3.1", | ||
"@rollup/plugin-node-resolve": "^13.3.0", | ||
"rollup-plugin-copy": "^3.4.0", | ||
"rollup": "^2.79.1", | ||
"zod": "3.21.4" | ||
@@ -39,0 +55,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
40113
18
899
8
+ Added@conform-to/dom@1.1.3(transitive)
- Removed@conform-to/dom@1.1.2(transitive)
Updated@conform-to/dom@1.1.3