@conform-to/zod
Advanced tools
Comparing version 0.3.0-pre.0 to 0.3.0
import { type Schema } from '@conform-to/dom'; | ||
import * as z from 'zod'; | ||
export declare function resolve<Source extends z.ZodTypeAny>(source: Source): Schema<z.infer<Source>, Source>; | ||
export declare function ifNonEmptyString(fn: (value: string) => unknown): (value: unknown) => unknown; |
45
index.js
@@ -37,4 +37,4 @@ 'use strict'; | ||
for (var [key, value] of Object.entries(data)) { | ||
result[key] = cleanup(value); | ||
for (var [key, _value] of Object.entries(data)) { | ||
result[key] = cleanup(_value); | ||
} | ||
@@ -147,15 +147,22 @@ | ||
function resolve(source) { | ||
var shape = getSchemaShape(source); | ||
if (!shape) { | ||
throw new Error('Unknown schema provided; The schema must have an object shape'); | ||
} | ||
return { | ||
source, | ||
constraint: Object.fromEntries(Object.entries(shape).map(_ref => { | ||
var [key, def] = _ref; | ||
return [key, inferConstraint(def)]; | ||
})), | ||
constraint: new Proxy({}, { | ||
get(_target, key) { | ||
if (typeof key !== 'string') { | ||
return; | ||
} | ||
var shape = getSchemaShape(source); | ||
var schema = shape === null || shape === void 0 ? void 0 : shape[key]; | ||
if (!schema) { | ||
return {}; | ||
} | ||
return inferConstraint(schema); | ||
} | ||
}), | ||
validate(form, submitter) { | ||
@@ -200,3 +207,17 @@ var payload = dom.getFormData(form, submitter); | ||
} | ||
function ifNonEmptyString(fn) { | ||
return value => { | ||
if (typeof value !== 'string') { | ||
return value; | ||
} | ||
if (value === '') { | ||
return undefined; | ||
} | ||
return fn(value); | ||
}; | ||
} | ||
exports.ifNonEmptyString = ifNonEmptyString; | ||
exports.resolve = resolve; |
@@ -13,4 +13,4 @@ import { objectSpread2 as _objectSpread2 } from './_virtual/_rollupPluginBabelHelpers.js'; | ||
for (var [key, value] of Object.entries(data)) { | ||
result[key] = cleanup(value); | ||
for (var [key, _value] of Object.entries(data)) { | ||
result[key] = cleanup(_value); | ||
} | ||
@@ -123,15 +123,22 @@ | ||
function resolve(source) { | ||
var shape = getSchemaShape(source); | ||
if (!shape) { | ||
throw new Error('Unknown schema provided; The schema must have an object shape'); | ||
} | ||
return { | ||
source, | ||
constraint: Object.fromEntries(Object.entries(shape).map(_ref => { | ||
var [key, def] = _ref; | ||
return [key, inferConstraint(def)]; | ||
})), | ||
constraint: new Proxy({}, { | ||
get(_target, key) { | ||
if (typeof key !== 'string') { | ||
return; | ||
} | ||
var shape = getSchemaShape(source); | ||
var schema = shape === null || shape === void 0 ? void 0 : shape[key]; | ||
if (!schema) { | ||
return {}; | ||
} | ||
return inferConstraint(schema); | ||
} | ||
}), | ||
validate(form, submitter) { | ||
@@ -176,3 +183,16 @@ var payload = getFormData(form, submitter); | ||
} | ||
function ifNonEmptyString(fn) { | ||
return value => { | ||
if (typeof value !== 'string') { | ||
return value; | ||
} | ||
export { resolve }; | ||
if (value === '') { | ||
return undefined; | ||
} | ||
return fn(value); | ||
}; | ||
} | ||
export { ifNonEmptyString, resolve }; |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.3.0-pre.0", | ||
"version": "0.3.0", | ||
"main": "index.js", | ||
@@ -18,3 +18,3 @@ "module": "module/index.js", | ||
"peerDependencies": { | ||
"@conform-to/dom": "0.3.0-pre.0", | ||
"@conform-to/dom": "0.3.0", | ||
"zod": "^3.0.0" | ||
@@ -21,0 +21,0 @@ }, |
@@ -8,2 +8,3 @@ # @conform-to/zod | ||
- [resolve](#resolve) | ||
- [ifNonEmptyString](#ifNonEmptyString) | ||
@@ -93,1 +94,35 @@ ### resolve | ||
``` | ||
### ifNonEmptyString | ||
As `zod` does not have specific logic for handling form data, there are some common cases need to be handled by the users. For example, | ||
1. it does not treat empty string as invalid for a requried field | ||
2. it has no type coercion support (e.g. '1' -> 1) | ||
The zod schema resolver currently does an extra cleanup step to transform empty string to undefined internally. But users are still required to do convert the data to their desired type themselves. | ||
```tsx | ||
import { z } from 'zod'; | ||
import { resolve, ifNonEmptyString } from '@conform-to/zod'; | ||
const schema = resolve( | ||
z.object({ | ||
// No preprocess is needed for string as empty string | ||
// is already converted to undefined by the resolver | ||
text: z.string({ required_error: 'This field is required' }), | ||
// Cast to number manually | ||
number: z.preprocess( | ||
ifNonEmptyString(Number), | ||
z.number({ required_error: 'This field is required' }), | ||
), | ||
// This is how you will do it without the helper | ||
date: z.preprocess( | ||
(value) => (typeof value === 'string' ? new Date(value) : value), | ||
z.date({ required_error: 'This field is required' }), | ||
), | ||
}), | ||
); | ||
``` |
18725
418
127