You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guides/03-coercing-data.md
+6-8
Original file line number
Diff line number
Diff line change
@@ -50,24 +50,22 @@ const User = defaulted(
50
50
51
51
We've already covered default values, but sometimes you'll need to create coercions that aren't just defaulted `undefined` values, but instead transforming the input data from one format to another.
52
52
53
-
For example, maybe you want to ensure that any string is trimmed before passing it into the validator. To do that you can define a custom coercion:
53
+
For example, maybe you want to ensure that a number is parsed from a string before passing it into the validator. To do that you can define a custom coercion:
This allows you to customize how lenient you want to be in accepting data with your structs.
55
+
The second argument to `coerce` is a struct narrowing the types of input values you want to try coercion. In the example above, the coercion functionn will only ever be called when the input is a string—booleans would ignore coercion and fail normally.
46
56
47
-
> 🤖 Note that the `value` argument passed to coercion handlers is of type `unknown`! This is because it has yet to be validated, so it could still be anything. Make sure your coercion functions guard against unknown types.
57
+
> 🤖 If you want to run coercion for any type of input, use the `unknown()` struct to run it in all cases.
0 commit comments