@conform-to/zod
Advanced tools
Comparing version 0.7.0-pre.2 to 0.7.0-pre.3
@@ -12,2 +12,3 @@ import { type FieldsetConstraint, type Submission } from '@conform-to/dom'; | ||
async?: false; | ||
errorMap?: z.ZodErrorMap; | ||
}): Submission<z.output<Schema>>; | ||
@@ -22,3 +23,30 @@ export declare function parse<Schema extends z.ZodTypeAny>(payload: FormData | URLSearchParams, config: { | ||
async: true; | ||
errorMap?: z.ZodErrorMap; | ||
}): Promise<Submission<z.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: z.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?: z.IssueData['path']; | ||
}): void | Promise<void>; | ||
export declare function ifNonEmptyString(fn: (value: string) => unknown): (value: unknown) => unknown; |
47
index.js
@@ -164,6 +164,50 @@ 'use strict'; | ||
}; | ||
return config.async ? schema.safeParseAsync(payload).then(resolveResult) : resolveResult(schema.safeParse(payload)); | ||
return config.async ? schema.safeParseAsync(payload, { | ||
errorMap: config.errorMap | ||
}).then(resolveResult) : resolveResult(schema.safeParse(payload, { | ||
errorMap: config.errorMap | ||
})); | ||
} | ||
}); | ||
} | ||
/** | ||
* 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 (!options.when) { | ||
ctx.addIssue({ | ||
code: z__namespace.ZodIssueCode.custom, | ||
message: dom.VALIDATION_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: z__namespace.ZodIssueCode.custom, | ||
message: dom.VALIDATION_UNDEFINED, | ||
path: options.path | ||
}); | ||
return; | ||
} | ||
var reportInvalid = valid => { | ||
if (valid) { | ||
return; | ||
} | ||
ctx.addIssue({ | ||
code: z__namespace.ZodIssueCode.custom, | ||
message: options.message, | ||
path: options.path | ||
}); | ||
}; | ||
return typeof result === 'boolean' ? reportInvalid(result) : result.then(reportInvalid); | ||
} | ||
function ifNonEmptyString(fn) { | ||
@@ -184,1 +228,2 @@ return value => { | ||
exports.parse = parse; | ||
exports.refine = refine; |
@@ -6,3 +6,3 @@ { | ||
"license": "MIT", | ||
"version": "0.7.0-pre.2", | ||
"version": "0.7.0-pre.3", | ||
"main": "index.js", | ||
@@ -29,3 +29,3 @@ "module": "index.mjs", | ||
"peerDependencies": { | ||
"@conform-to/dom": "0.7.0-pre.2", | ||
"@conform-to/dom": "0.7.0-pre.3", | ||
"zod": "^3.21.0" | ||
@@ -32,0 +32,0 @@ }, |
@@ -93,1 +93,35 @@ # @conform-to/zod | ||
``` | ||
### refine | ||
A helper function to define a custom constraint on a superRefine check. This is mainly used to setup async validation. | ||
```tsx | ||
import { refine } from '@conform-to/zod'; | ||
function createSchema( | ||
intent: string, | ||
constraints: { | ||
// The validation will only be implemented on server side | ||
isEmailUnique?: (email) => Promise<boolean>; | ||
} = {}, | ||
) { | ||
return z.object({ | ||
email: z | ||
.string() | ||
.min(1, 'Email is required') | ||
.email('Email is invalid') | ||
.superRefine((email, ctx) => | ||
refine(ctx, { | ||
// It fallbacks to server validation when it returns an undefined value | ||
validate: () => constraints.isEmailUnique?.(email), | ||
// This makes it validate only when the user is submitting the form | ||
// or updating the email | ||
when: intent === 'submit' || intent === 'validate/email', | ||
message: 'Email is already used', | ||
}), | ||
), | ||
// ... | ||
}); | ||
} | ||
``` |
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
25499
570
127