@conform-to/zod
Advanced tools
Comparing version 0.7.4 to 0.8.0-pre.0
@@ -1,52 +0,3 @@ | ||
import { type FieldsetConstraint, type Submission } from '@conform-to/dom'; | ||
import * as z from 'zod'; | ||
export declare function getFieldsetConstraint<Source extends z.ZodTypeAny>(source: Source): FieldsetConstraint<z.input<Source>>; | ||
export declare function parse<Schema extends z.ZodTypeAny>(payload: FormData | URLSearchParams, config: { | ||
schema: Schema | ((intent: string) => Schema); | ||
acceptMultipleErrors?: ({ name, intent, payload, }: { | ||
name: string; | ||
intent: string; | ||
payload: Record<string, any>; | ||
}) => boolean; | ||
async?: false; | ||
errorMap?: z.ZodErrorMap; | ||
stripEmptyValue?: boolean; | ||
}): Submission<z.output<Schema>>; | ||
export declare function parse<Schema extends z.ZodTypeAny>(payload: FormData | URLSearchParams, config: { | ||
schema: Schema | ((intent: string) => Schema); | ||
acceptMultipleErrors?: ({ name, intent, payload, }: { | ||
name: string; | ||
intent: string; | ||
payload: Record<string, any>; | ||
}) => boolean; | ||
async: true; | ||
errorMap?: z.ZodErrorMap; | ||
stripEmptyValue?: boolean; | ||
}): 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; | ||
export { getConstraint as getFieldsetConstraint } from './constraint'; | ||
export { parse, refine } from './parse'; | ||
export { ifNonEmptyString } from './coercion'; |
230
index.js
@@ -5,227 +5,11 @@ 'use strict'; | ||
var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js'); | ||
var dom = require('@conform-to/dom'); | ||
var z = require('zod'); | ||
var constraint = require('./constraint.js'); | ||
var parse = require('./parse.js'); | ||
var coercion = require('./coercion.js'); | ||
function _interopNamespace(e) { | ||
if (e && e.__esModule) return e; | ||
var n = Object.create(null); | ||
if (e) { | ||
Object.keys(e).forEach(function (k) { | ||
if (k !== 'default') { | ||
var d = Object.getOwnPropertyDescriptor(e, k); | ||
Object.defineProperty(n, k, d.get ? d : { | ||
enumerable: true, | ||
get: function () { return e[k]; } | ||
}); | ||
} | ||
}); | ||
} | ||
n["default"] = e; | ||
return Object.freeze(n); | ||
} | ||
var z__namespace = /*#__PURE__*/_interopNamespace(z); | ||
function getFieldsetConstraint(source) { | ||
function inferConstraint(schema) { | ||
var constraint = {}; | ||
if (schema instanceof z__namespace.ZodEffects) { | ||
constraint = _rollupPluginBabelHelpers.objectSpread2({}, inferConstraint(schema.innerType())); | ||
} else if (schema instanceof z__namespace.ZodPipeline) { | ||
constraint = _rollupPluginBabelHelpers.objectSpread2({}, inferConstraint(schema._def.out)); | ||
} else if (schema instanceof z__namespace.ZodOptional) { | ||
constraint = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, inferConstraint(schema.unwrap())), {}, { | ||
required: false | ||
}); | ||
} else if (schema instanceof z__namespace.ZodDefault) { | ||
constraint = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, inferConstraint(schema.removeDefault())), {}, { | ||
required: false | ||
}); | ||
} else if (schema instanceof z__namespace.ZodArray) { | ||
constraint = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, inferConstraint(schema.element)), {}, { | ||
multiple: true | ||
}); | ||
} else if (schema instanceof z__namespace.ZodString) { | ||
for (var check of schema._def.checks) { | ||
switch (check.kind) { | ||
case 'min': | ||
if (!constraint.minLength || constraint.minLength < check.value) { | ||
constraint.minLength = check.value; | ||
} | ||
break; | ||
case 'max': | ||
if (!constraint.maxLength || constraint.maxLength > check.value) { | ||
constraint.maxLength = check.value; | ||
} | ||
break; | ||
case 'regex': | ||
if (!constraint.pattern) { | ||
constraint.pattern = check.regex.source; | ||
} | ||
break; | ||
} | ||
} | ||
} else if (schema instanceof z__namespace.ZodNumber) { | ||
for (var _check of schema._def.checks) { | ||
switch (_check.kind) { | ||
case 'min': | ||
if (!constraint.min || constraint.min < _check.value) { | ||
constraint.min = _check.value; | ||
} | ||
break; | ||
case 'max': | ||
if (!constraint.max || constraint.max > _check.value) { | ||
constraint.max = _check.value; | ||
} | ||
break; | ||
} | ||
} | ||
} else if (schema instanceof z__namespace.ZodEnum) { | ||
constraint.pattern = schema.options.map(option => | ||
// To escape unsafe characters on regex | ||
option.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')).join('|'); | ||
} | ||
if (typeof constraint.required === 'undefined') { | ||
constraint.required = true; | ||
} | ||
return constraint; | ||
} | ||
var keys = ['required', 'minLength', 'maxLength', 'min', 'max', 'step', 'multiple', 'pattern']; | ||
function resolveFieldsetConstraint(schema) { | ||
if (schema instanceof z__namespace.ZodObject) { | ||
var result = {}; | ||
for (var [key, def] of Object.entries(schema.shape)) { | ||
// @ts-expect-error | ||
result[key] = inferConstraint(def); | ||
} | ||
return result; | ||
} | ||
if (schema instanceof z__namespace.ZodEffects) { | ||
return resolveFieldsetConstraint(schema.innerType()); | ||
} else if (schema instanceof z__namespace.ZodOptional) { | ||
return resolveFieldsetConstraint(schema.unwrap()); | ||
} else if (schema instanceof z__namespace.ZodIntersection) { | ||
return _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, resolveFieldsetConstraint(schema._def.left)), resolveFieldsetConstraint(schema._def.right)); | ||
} else if (schema instanceof z__namespace.ZodUnion || schema instanceof z__namespace.ZodDiscriminatedUnion) { | ||
var options = schema.options; | ||
return options.map(resolveFieldsetConstraint).reduce((prev, next) => { | ||
var list = new Set([...Object.keys(prev), ...Object.keys(next)]); | ||
var result = {}; | ||
for (var name of list) { | ||
// @ts-expect-error | ||
var prevConstraint = prev[name]; | ||
// @ts-expect-error | ||
var nextConstraint = next[name]; | ||
if (prevConstraint && nextConstraint) { | ||
result[name] = {}; | ||
for (var _key of keys) { | ||
if (typeof prevConstraint[_key] !== 'undefined' && typeof nextConstraint[_key] !== 'undefined' && prevConstraint[_key] === nextConstraint[_key]) { | ||
result[name][_key] = prevConstraint[_key]; | ||
} | ||
} | ||
} else { | ||
result[name] = _rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2(_rollupPluginBabelHelpers.objectSpread2({}, prevConstraint), nextConstraint), {}, { | ||
required: false | ||
}); | ||
} | ||
} | ||
return result; | ||
}); | ||
} | ||
return {}; | ||
} | ||
return resolveFieldsetConstraint(source); | ||
} | ||
function parse(payload, config) { | ||
return dom.parse(payload, { | ||
stripEmptyValue: config.stripEmptyValue, | ||
resolve(payload, intent) { | ||
var schema = typeof config.schema === 'function' ? config.schema(intent) : config.schema; | ||
var resolveResult = result => { | ||
if (result.success) { | ||
return { | ||
value: result.data | ||
}; | ||
} | ||
return { | ||
error: result.error.errors.reduce((result, e) => { | ||
var _config$acceptMultipl; | ||
var name = dom.getName(e.path); | ||
if (typeof result[name] === 'undefined') { | ||
result[name] = e.message; | ||
} else if ((_config$acceptMultipl = config.acceptMultipleErrors) !== null && _config$acceptMultipl !== void 0 && _config$acceptMultipl.call(config, { | ||
name, | ||
intent, | ||
payload | ||
})) { | ||
result[name] = [].concat(result[name], e.message); | ||
} | ||
return result; | ||
}, {}) | ||
}; | ||
}; | ||
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 (typeof options.when !== 'undefined' && !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) { | ||
return value => { | ||
if (typeof value !== 'string') { | ||
return value; | ||
} | ||
if (value === '') { | ||
return undefined; | ||
} | ||
return fn(value); | ||
}; | ||
} | ||
exports.getFieldsetConstraint = getFieldsetConstraint; | ||
exports.ifNonEmptyString = ifNonEmptyString; | ||
exports.parse = parse; | ||
exports.refine = refine; | ||
exports.getFieldsetConstraint = constraint.getConstraint; | ||
exports.parse = parse.parse; | ||
exports.refine = parse.refine; | ||
exports.ifNonEmptyString = coercion.ifNonEmptyString; |
@@ -6,3 +6,3 @@ { | ||
"license": "MIT", | ||
"version": "0.7.4", | ||
"version": "0.8.0-pre.0", | ||
"main": "index.js", | ||
@@ -29,3 +29,3 @@ "module": "index.mjs", | ||
"peerDependencies": { | ||
"@conform-to/dom": "0.7.4", | ||
"@conform-to/dom": "0.8.0-pre.0", | ||
"zod": "^3.21.0" | ||
@@ -32,0 +32,0 @@ }, |
@@ -59,3 +59,2 @@ # @conform-to/zod | ||
schema, | ||
stripEmptyValue: true, | ||
}); | ||
@@ -72,3 +71,3 @@ }, | ||
```tsx | ||
import { useForm } from '@conform-to/react'; | ||
import { useForm, report } from '@conform-to/react'; | ||
import { parse } from '@conform-to/zod'; | ||
@@ -87,5 +86,2 @@ import { z } from 'zod'; | ||
// Recommended: this will be the default in the future | ||
stripEmptyValue: true, | ||
// If the schema definition includes async validation | ||
@@ -96,3 +92,3 @@ async: true, | ||
if (!submission.value || submission.intent !== 'submit') { | ||
return submission; | ||
return report(submission); | ||
} | ||
@@ -99,0 +95,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
38560
17
849
128
1