@conform-to/zod
Advanced tools
Comparing version 0.8.0 to 0.8.1
@@ -30,3 +30,3 @@ 'use strict'; | ||
function coerceFile(file) { | ||
if (file instanceof File && file.name === '' && file.size === 0) { | ||
if (typeof File !== 'undefined' && file instanceof File && file.name === '' && file.size === 0) { | ||
return undefined; | ||
@@ -43,2 +43,5 @@ } | ||
function isFileSchema(schema) { | ||
if (typeof File === 'undefined') { | ||
return false; | ||
} | ||
return schema._def.effect.type === 'refinement' && schema.innerType() instanceof zod.ZodAny && schema.safeParse(new File([], '')).success && !schema.safeParse('').success; | ||
@@ -69,9 +72,9 @@ } | ||
if (type instanceof zod.ZodString || type instanceof zod.ZodLiteral || type instanceof zod.ZodEnum || type instanceof zod.ZodNativeEnum) { | ||
schema = zod.preprocess(value => coerceString(value), type); | ||
schema = zod.any().transform(value => coerceString(value)).pipe(type); | ||
} else if (type instanceof zod.ZodNumber) { | ||
schema = zod.preprocess(value => coerceString(value, Number), type); | ||
schema = zod.any().transform(value => coerceString(value, Number)).pipe(type); | ||
} else if (type instanceof zod.ZodBoolean) { | ||
schema = zod.preprocess(value => coerceString(value, text => text === 'on' ? true : text), type); | ||
schema = zod.any().transform(value => coerceString(value, text => text === 'on' ? true : text)).pipe(type); | ||
} else if (type instanceof zod.ZodDate) { | ||
schema = zod.preprocess(value => coerceString(value, timestamp => { | ||
schema = zod.any().transform(value => coerceString(value, timestamp => { | ||
var date = new Date(timestamp); | ||
@@ -86,7 +89,7 @@ | ||
return date; | ||
}), type); | ||
})).pipe(type); | ||
} else if (type instanceof zod.ZodBigInt) { | ||
schema = zod.preprocess(value => coerceString(value, BigInt), type); | ||
schema = zod.any().transform(value => coerceString(value, BigInt)).pipe(type); | ||
} else if (type instanceof zod.ZodArray) { | ||
schema = zod.preprocess(value => { | ||
schema = zod.any().transform(value => { | ||
// No preprocess needed if the value is already an array | ||
@@ -102,3 +105,3 @@ if (Array.isArray(value)) { | ||
return [value]; | ||
}, new zod.ZodArray(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type._def), {}, { | ||
}).pipe(new zod.ZodArray(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type._def), {}, { | ||
type: enableTypeCoercion(type.element, cache) | ||
@@ -118,15 +121,16 @@ }))); | ||
if (isFileSchema(type)) { | ||
return zod.preprocess(value => coerceFile(value), 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({}, type._def), {}, { | ||
schema: enableTypeCoercion(type.innerType(), cache) | ||
})); | ||
} else if (type instanceof zod.ZodOptional) { | ||
schema = zod.preprocess(value => coerceString(coerceFile(value)), new zod.ZodOptional(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type._def), {}, { | ||
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 (type instanceof zod.ZodDefault) { | ||
schema = new zod.ZodDefault(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type._def), {}, { | ||
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 (type instanceof zod.ZodIntersection) { | ||
@@ -133,0 +137,0 @@ schema = new zod.ZodIntersection(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, type._def), {}, { |
@@ -6,3 +6,3 @@ { | ||
"license": "MIT", | ||
"version": "0.8.0", | ||
"version": "0.8.1", | ||
"main": "index.js", | ||
@@ -29,7 +29,7 @@ "module": "index.mjs", | ||
"peerDependencies": { | ||
"@conform-to/dom": "0.8.0", | ||
"@conform-to/dom": "0.8.1", | ||
"zod": "^3.21.0" | ||
}, | ||
"devDependencies": { | ||
"zod": "^3.21.0" | ||
"zod": "^3.22.2" | ||
}, | ||
@@ -36,0 +36,0 @@ "keywords": [ |
import { type Submission } from '@conform-to/dom'; | ||
import { type output, type RefinementCtx, type ZodTypeAny, type ZodErrorMap, type IssueData } from 'zod'; | ||
import { type output, type RefinementCtx, type ZodCustomIssue, type ZodTypeAny, type ZodErrorMap, type IssueData } from 'zod'; | ||
export declare function parse<Schema extends ZodTypeAny>(payload: FormData | URLSearchParams, config: { | ||
@@ -33,3 +33,3 @@ schema: Schema | ((intent: string) => Schema); | ||
*/ | ||
message: string; | ||
message?: string; | ||
/** | ||
@@ -39,2 +39,6 @@ * The path set to the zod issue. | ||
path?: IssueData['path']; | ||
/** | ||
* Custom parameters | ||
*/ | ||
params?: ZodCustomIssue['params']; | ||
}): void | Promise<void>; |
@@ -71,3 +71,4 @@ 'use strict'; | ||
message: options.message, | ||
path: options.path | ||
path: options.path, | ||
params: options.params | ||
}); | ||
@@ -74,0 +75,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
40270
874