@conform-to/zod
Advanced tools
Comparing version 0.9.1 to 1.0.0-pre.0
@@ -26,2 +26,2 @@ import { type ZodType, type ZodTypeAny, type output, ZodEffects } from 'zod'; | ||
*/ | ||
export declare function enableTypeCoercion<Type extends ZodTypeAny>(type: Type, cache?: Map<ZodTypeAny, ZodTypeAny>): ZodType<output<Type>>; | ||
export declare function enableTypeCoercion<Schema extends ZodTypeAny>(type: Schema, cache?: Map<ZodTypeAny, ZodTypeAny>): ZodType<output<Schema>>; |
@@ -49,9 +49,2 @@ 'use strict'; | ||
/** | ||
* @deprecated Conform coerce empty strings to undefined by default | ||
*/ | ||
function ifNonEmptyString(fn) { | ||
return value => coerceString(value, fn); | ||
} | ||
/** | ||
* Reconstruct the provided schema with additional preprocessing steps | ||
@@ -169,3 +162,2 @@ * This coerce empty values to undefined and transform strings to the correct type | ||
exports.enableTypeCoercion = enableTypeCoercion; | ||
exports.ifNonEmptyString = ifNonEmptyString; | ||
exports.isFileSchema = isFileSchema; |
@@ -1,3 +0,3 @@ | ||
import { type FieldsetConstraint } from '@conform-to/dom'; | ||
import { type input, type ZodTypeAny } from 'zod'; | ||
export declare function getConstraint<Schema extends ZodTypeAny>(schema: Schema): FieldsetConstraint<input<Schema>>; | ||
import { type Constraint } from '@conform-to/dom'; | ||
import { type ZodTypeAny } from 'zod'; | ||
export declare function getConstraint(schema: ZodTypeAny): Record<string, Constraint>; |
@@ -6,2 +6,3 @@ 'use strict'; | ||
var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js'); | ||
var dom = require('@conform-to/dom'); | ||
var zod = require('zod'); | ||
@@ -52,3 +53,4 @@ | ||
case 'min': | ||
if (!constraint.min || constraint.min < _check.value) { | ||
dom.invariant(typeof constraint.min !== 'string', 'min is not a number'); | ||
if (typeof constraint.min === 'undefined' || constraint.min < _check.value) { | ||
constraint.min = _check.value; | ||
@@ -58,3 +60,4 @@ } | ||
case 'max': | ||
if (!constraint.max || constraint.max > _check.value) { | ||
dom.invariant(typeof constraint.max !== 'string', 'max is not a number'); | ||
if (typeof constraint.max === 'undefined' || constraint.max > _check.value) { | ||
constraint.max = _check.value; | ||
@@ -97,5 +100,3 @@ } | ||
for (var name of list) { | ||
// @ts-expect-error | ||
var prevConstraint = prev[name]; | ||
// @ts-expect-error | ||
var nextConstraint = next[name]; | ||
@@ -102,0 +103,0 @@ if (prevConstraint && nextConstraint) { |
export { getConstraint as getFieldsetConstraint } from './constraint'; | ||
export { parse, refine } from './parse'; | ||
export { ifNonEmptyString } from './coercion'; |
@@ -7,3 +7,2 @@ 'use strict'; | ||
var parse = require('./parse.js'); | ||
var coercion = require('./coercion.js'); | ||
@@ -15,2 +14,1 @@ | ||
exports.refine = parse.refine; | ||
exports.ifNonEmptyString = coercion.ifNonEmptyString; |
@@ -6,3 +6,3 @@ { | ||
"license": "MIT", | ||
"version": "0.9.1", | ||
"version": "1.0.0-pre.0", | ||
"main": "index.js", | ||
@@ -29,7 +29,7 @@ "module": "index.mjs", | ||
"peerDependencies": { | ||
"@conform-to/dom": "0.9.1", | ||
"@conform-to/dom": "1.0.0-pre.0", | ||
"zod": "^3.21.0" | ||
}, | ||
"devDependencies": { | ||
"zod": "^3.22.2" | ||
"zod": "3.21.4" | ||
}, | ||
@@ -36,0 +36,0 @@ "keywords": [ |
import { type Submission } from '@conform-to/dom'; | ||
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: { | ||
import { type IssueData, type output, type RefinementCtx, type ZodTypeAny, type ZodErrorMap } from 'zod'; | ||
export declare function parse<Schema extends ZodTypeAny>(payload: FormData | URLSearchParams, options: { | ||
schema: Schema | ((intent: string) => Schema); | ||
@@ -8,3 +8,3 @@ async?: false; | ||
}): Submission<output<Schema>>; | ||
export declare function parse<Schema extends ZodTypeAny>(payload: FormData | URLSearchParams, config: { | ||
export declare function parse<Schema extends ZodTypeAny>(payload: FormData | URLSearchParams, options: { | ||
schema: Schema | ((intent: string) => Schema); | ||
@@ -34,3 +34,3 @@ async: true; | ||
*/ | ||
message?: string; | ||
message: string; | ||
/** | ||
@@ -40,6 +40,2 @@ * The path set to the zod issue. | ||
path?: IssueData['path']; | ||
/** | ||
* Custom parameters | ||
*/ | ||
params?: ZodCustomIssue['params']; | ||
}): void | Promise<void>; |
44
parse.js
@@ -9,25 +9,30 @@ 'use strict'; | ||
function parse(payload, config) { | ||
function getError(_ref) { | ||
var { | ||
errors | ||
} = _ref; | ||
return errors.reduce((result, error) => { | ||
var _result$name; | ||
var name = dom.formatPaths(error.path); | ||
var messages = (_result$name = result[name]) !== null && _result$name !== void 0 ? _result$name : []; | ||
messages.push(error.message); | ||
result[name] = messages; | ||
return result; | ||
}, {}); | ||
} | ||
function parse(payload, options) { | ||
return dom.parse(payload, { | ||
resolve(payload, intent) { | ||
var schema = coercion.enableTypeCoercion(typeof config.schema === 'function' ? config.schema(intent) : config.schema); | ||
var resolveResult = result => { | ||
if (result.success) { | ||
return { | ||
value: result.data | ||
}; | ||
} | ||
var errorMap = options.errorMap; | ||
var schema = coercion.enableTypeCoercion(typeof options.schema === 'function' ? options.schema(intent) : options.schema); | ||
var resolveSubmission = result => { | ||
return { | ||
error: result.error.errors.reduce((result, e) => { | ||
var _result$name; | ||
var name = dom.getName(e.path); | ||
result[name] = [...((_result$name = result[name]) !== null && _result$name !== void 0 ? _result$name : []), e.message]; | ||
return result; | ||
}, {}) | ||
value: result.success ? result.data : null, | ||
error: !result.success ? getError(result.error) : {} | ||
}; | ||
}; | ||
return config.async ? schema.safeParseAsync(payload, { | ||
errorMap: config.errorMap | ||
}).then(resolveResult) : resolveResult(schema.safeParse(payload, { | ||
errorMap: config.errorMap | ||
return options.async ? schema.safeParseAsync(payload, { | ||
errorMap | ||
}).then(result => resolveSubmission(result)) : resolveSubmission(schema.safeParse(payload, { | ||
errorMap | ||
})); | ||
@@ -72,4 +77,3 @@ } | ||
message: options.message, | ||
path: options.path, | ||
params: options.params | ||
path: options.path | ||
}); | ||
@@ -76,0 +80,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
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40124
863