@conform-to/zod
Advanced tools
Comparing version 1.0.0-pre.6 to 1.0.0-pre.7
export { getZodConstraint } from './constraint'; | ||
export { parseWithZod, refine } from './parse'; | ||
export { parseWithZod, conformZodMessage } from './parse'; |
@@ -11,3 +11,3 @@ 'use strict'; | ||
exports.getZodConstraint = constraint.getZodConstraint; | ||
exports.conformZodMessage = parse.conformZodMessage; | ||
exports.parseWithZod = parse.parseWithZod; | ||
exports.refine = parse.refine; |
@@ -6,3 +6,3 @@ { | ||
"license": "MIT", | ||
"version": "1.0.0-pre.6", | ||
"version": "1.0.0-pre.7", | ||
"main": "index.js", | ||
@@ -29,3 +29,3 @@ "module": "index.mjs", | ||
"dependencies": { | ||
"@conform-to/dom": "1.0.0-pre.6" | ||
"@conform-to/dom": "1.0.0-pre.7" | ||
}, | ||
@@ -32,0 +32,0 @@ "peerDependencies": { |
@@ -1,5 +0,5 @@ | ||
import { type FormControl, type Submission } from '@conform-to/dom'; | ||
import { type IssueData, type RefinementCtx, type ZodTypeAny, type ZodErrorMap, type input, type output, type ZodIssue } from 'zod'; | ||
import { type Intent, type Submission } from '@conform-to/dom'; | ||
import { type ZodTypeAny, type ZodErrorMap, type input, type output, type ZodIssue } from 'zod'; | ||
export declare function parseWithZod<Schema extends ZodTypeAny>(payload: FormData | URLSearchParams, options: { | ||
schema: Schema | ((control: FormControl | null) => Schema); | ||
schema: Schema | ((intent: Intent | null) => Schema); | ||
async?: false; | ||
@@ -9,3 +9,3 @@ errorMap?: ZodErrorMap; | ||
export declare function parseWithZod<Schema extends ZodTypeAny, FormError>(payload: FormData | URLSearchParams, options: { | ||
schema: Schema | ((control: FormControl | null) => Schema); | ||
schema: Schema | ((intent: Intent | null) => Schema); | ||
async?: false; | ||
@@ -16,3 +16,3 @@ errorMap?: ZodErrorMap; | ||
export declare function parseWithZod<Schema extends ZodTypeAny>(payload: FormData | URLSearchParams, options: { | ||
schema: Schema | ((control: FormControl | null) => Schema); | ||
schema: Schema | ((intent: Intent | null) => Schema); | ||
async: true; | ||
@@ -22,3 +22,3 @@ errorMap?: ZodErrorMap; | ||
export declare function parseWithZod<Schema extends ZodTypeAny, FormError>(payload: FormData | URLSearchParams, options: { | ||
schema: Schema | ((control: FormControl | null) => Schema); | ||
schema: Schema | ((intent: Intent | null) => Schema); | ||
async: true; | ||
@@ -28,27 +28,5 @@ errorMap?: ZodErrorMap; | ||
}): Promise<Submission<input<Schema>, FormError, output<Schema>>>; | ||
/** | ||
* A helper function to define a custom constraint on a superRefine check. | ||
* Mainly used for async validation. | ||
* | ||
* @see https://conform.guide/api/zod#refine | ||
*/ | ||
export declare function refine(ctx: RefinementCtx, options: { | ||
/** | ||
* A validate function. If the function returns `undefined`, | ||
* it will fallback to server validation. | ||
*/ | ||
validate: () => boolean | Promise<boolean> | undefined; | ||
/** | ||
* Define when the validation should be run. If the value is `false`, | ||
* the validation will be skipped. | ||
*/ | ||
when?: boolean; | ||
/** | ||
* The message displayed when the validation fails. | ||
*/ | ||
message: string; | ||
/** | ||
* The path set to the zod issue. | ||
*/ | ||
path?: IssueData['path']; | ||
}): void | Promise<void>; | ||
export declare const conformZodMessage: { | ||
VALIDATION_SKIPPED: string; | ||
VALIDATION_UNDEFINED: string; | ||
}; |
56
parse.js
@@ -6,3 +6,2 @@ 'use strict'; | ||
var dom = require('@conform-to/dom'); | ||
var zod = require('zod'); | ||
var coercion = require('./coercion.js'); | ||
@@ -15,5 +14,5 @@ | ||
switch (issue.message) { | ||
case '__undefined__': | ||
case conformZodMessage.VALIDATION_UNDEFINED: | ||
return null; | ||
case '__skipped__': | ||
case conformZodMessage.VALIDATION_SKIPPED: | ||
result[name] = null; | ||
@@ -43,5 +42,5 @@ break; | ||
return dom.parse(payload, { | ||
resolve(payload, control) { | ||
resolve(payload, intent) { | ||
var errorMap = options.errorMap; | ||
var schema = coercion.enableTypeCoercion(typeof options.schema === 'function' ? options.schema(control) : options.schema); | ||
var schema = coercion.enableTypeCoercion(typeof options.schema === 'function' ? options.schema(intent) : options.schema); | ||
var resolveSubmission = result => { | ||
@@ -62,45 +61,8 @@ var _options$formatError; | ||
} | ||
var conformZodMessage = { | ||
VALIDATION_SKIPPED: '__skipped__', | ||
VALIDATION_UNDEFINED: '__undefined__' | ||
}; | ||
/** | ||
* A helper function to define a custom constraint on a superRefine check. | ||
* Mainly used for async validation. | ||
* | ||
* @see https://conform.guide/api/zod#refine | ||
*/ | ||
function refine(ctx, options) { | ||
if (typeof options.when !== 'undefined' && !options.when) { | ||
ctx.addIssue({ | ||
code: zod.ZodIssueCode.custom, | ||
message: '__skipped__', | ||
path: options.path | ||
}); | ||
return; | ||
} | ||
// Run the validation | ||
var result = options.validate(); | ||
if (typeof result === 'undefined') { | ||
// Validate only if the constraint is defined | ||
ctx.addIssue({ | ||
code: zod.ZodIssueCode.custom, | ||
message: '__undefined__', | ||
path: options.path, | ||
fatal: true | ||
}); | ||
return; | ||
} | ||
var reportInvalid = valid => { | ||
if (valid) { | ||
return; | ||
} | ||
ctx.addIssue({ | ||
code: zod.ZodIssueCode.custom, | ||
message: options.message, | ||
path: options.path | ||
}); | ||
}; | ||
return typeof result === 'boolean' ? reportInvalid(result) : result.then(reportInvalid); | ||
} | ||
exports.conformZodMessage = conformZodMessage; | ||
exports.parseWithZod = parseWithZod; | ||
exports.refine = refine; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
37182
785
33
+ Added@conform-to/dom@1.0.0-pre.7(transitive)
- Removed@conform-to/dom@1.0.0-pre.6(transitive)
Updated@conform-to/dom@1.0.0-pre.7