|
| 1 | +# unstable_coerceFormValue |
| 2 | + |
| 3 | +A helper that enhances the schema with extra preprocessing steps to strip empty value and coerce form value to the expected type. |
| 4 | + |
| 5 | +```ts |
| 6 | +const enhancedSchema = coerceFormValue(schema, options); |
| 7 | +``` |
| 8 | + |
| 9 | +The following rules will be applied by default: |
| 10 | + |
| 11 | +1. If the value is an empty string / file, pass `undefined` to the schema |
| 12 | +2. If the schema is `z.number()`, trim the value and cast it with the `Number` constructor |
| 13 | +3. If the schema is `z.boolean()`, treat the value as `true` if it equals to `on` (Browser default `value` of a checkbox / radio button) |
| 14 | +4. If the schema is `z.date()`, cast the value with the `Date` constructor |
| 15 | +5. If the schema is `z.bigint()`, trim the value and cast it with the `BigInt` constructor |
| 16 | + |
| 17 | +## Parameters |
| 18 | + |
| 19 | +### `schema` |
| 20 | + |
| 21 | +The zod schema to be enhanced. |
| 22 | + |
| 23 | +### `options.defaultCoercion` |
| 24 | + |
| 25 | +Optional. Set it if you want to [override the default behavior](#override-default-behavior). |
| 26 | + |
| 27 | +### `options.defineCoercion` |
| 28 | + |
| 29 | +Optional. Use it to [define custom coercion](#define-custom-coercion) for a specific schema. |
| 30 | + |
| 31 | +## Example |
| 32 | + |
| 33 | +```ts |
| 34 | +import { parseWithZod, unstable_coerceFormValue as coerceFormValue } from '@conform-to/zod'; |
| 35 | +import { useForm } from '@conform-to/react'; |
| 36 | +import { z } from 'zod'; |
| 37 | +import { jsonSchema } from './jsonSchema'; |
| 38 | + |
| 39 | +const schema = coerceFormValue( |
| 40 | + z.object({ |
| 41 | + ref: z.string() |
| 42 | + date: z.date(), |
| 43 | + amount: z.number(), |
| 44 | + confirm: z.boolean(), |
| 45 | + }), |
| 46 | +); |
| 47 | + |
| 48 | +function Example() { |
| 49 | + const [form, fields] = useForm({ |
| 50 | + onValidate({ formData }) { |
| 51 | + return parseWithZod(formData, { |
| 52 | + schema, |
| 53 | + defaultTypeCoercion: false, |
| 54 | + }); |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + // ... |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +## Tips |
| 63 | + |
| 64 | +### Override default behavior |
| 65 | + |
| 66 | +You can override the default coercion by specifying the `defaultCoercion` mapping in the options. |
| 67 | + |
| 68 | +```ts |
| 69 | +const schema = coerceFormValue( |
| 70 | + z.object({ |
| 71 | + // ... |
| 72 | + }), |
| 73 | + { |
| 74 | + defaultCoercion: { |
| 75 | + // Trim the value for all string-based fields |
| 76 | + // e.g. `z.string()`, `z.number()` or `z.boolean()` |
| 77 | + string: (value) => { |
| 78 | + if (typeof value !== 'string') { |
| 79 | + return value; |
| 80 | + } |
| 81 | + |
| 82 | + const result = value.trim(); |
| 83 | + |
| 84 | + // Treat it as `undefined` if the value is empty |
| 85 | + if (result === '') { |
| 86 | + return undefined; |
| 87 | + } |
| 88 | + |
| 89 | + return result; |
| 90 | + }, |
| 91 | + |
| 92 | + // Override the default coercion with `z.number()` |
| 93 | + number: (value) => { |
| 94 | + // Pass the value as is if it's not a string |
| 95 | + if (typeof value !== 'string') { |
| 96 | + return value; |
| 97 | + } |
| 98 | + |
| 99 | + // Trim and remove commas before casting it to number |
| 100 | + return Number(value.trim().replace(/,/g, '')); |
| 101 | + }, |
| 102 | + |
| 103 | + // Disable coercion for `z.boolean()` |
| 104 | + boolean: false, |
| 105 | + }, |
| 106 | + }, |
| 107 | +); |
| 108 | +``` |
| 109 | + |
| 110 | +### Default values |
| 111 | + |
| 112 | +`coerceFormValue` will always strip empty values to `undefined`. If you need a default value, use `.transform()` to define a fallback value that will be returned instead. |
| 113 | + |
| 114 | +```ts |
| 115 | +const schema = z.object({ |
| 116 | + foo: z.string().optional(), // string | undefined |
| 117 | + bar: z |
| 118 | + .string() |
| 119 | + .optional() |
| 120 | + .transform((value) => value ?? ''), // string |
| 121 | + baz: z |
| 122 | + .string() |
| 123 | + .optional() |
| 124 | + .transform((value) => value ?? null), // string | null |
| 125 | +}); |
| 126 | +``` |
| 127 | + |
| 128 | +### Define custom coercion |
| 129 | + |
| 130 | +You can customize coercion for a specific schema by setting the `customize` option. |
| 131 | + |
| 132 | +```ts |
| 133 | +import { |
| 134 | + parseWithZod, |
| 135 | + unstable_coerceFormValue as coerceFormValue, |
| 136 | +} from '@conform-to/zod'; |
| 137 | +import { useForm } from '@conform-to/react'; |
| 138 | +import { z } from 'zod'; |
| 139 | +import { json } from './schema'; |
| 140 | + |
| 141 | +const metadata = z.object({ |
| 142 | + number: z.number(), |
| 143 | + confirmed: z.boolean(), |
| 144 | +}); |
| 145 | + |
| 146 | +const schema = coerceFormValue( |
| 147 | + z.object({ |
| 148 | + ref: z.string(), |
| 149 | + metadata, |
| 150 | + }), |
| 151 | + { |
| 152 | + customize(type) { |
| 153 | + // Customize how the `metadata` field value is coerced |
| 154 | + if (type === metadata) { |
| 155 | + return (value) => { |
| 156 | + if (typeof value !== 'string') { |
| 157 | + return value; |
| 158 | + } |
| 159 | + |
| 160 | + // Parse the value as JSON |
| 161 | + return JSON.parse(value); |
| 162 | + }; |
| 163 | + } |
| 164 | + |
| 165 | + // Return `null` to keep the default behavior |
| 166 | + return null; |
| 167 | + }, |
| 168 | + }, |
| 169 | +); |
| 170 | +``` |
0 commit comments