Skip to content

Commit 17a3a55

Browse files
committed
Add Custom Type Checking Error Messages With Yup as a javascript til
1 parent ba4b629 commit 17a3a55

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
1010
For a steady stream of TILs from a variety of rocketeers, checkout
1111
[til.hashrocket.com](https://til.hashrocket.com/).
1212

13-
_635 TILs and counting..._
13+
_636 TILs and counting..._
1414

1515
---
1616

@@ -227,6 +227,7 @@ _635 TILs and counting..._
227227
- [Computed Property Names In ES6](javascript/computed-property-names-in-es6.md)
228228
- [Create An Array Containing 1 To N](javascript/create-an-array-containing-1-to-n.md)
229229
- [Create Bootstrapped Apps With Yarn](javascript/create-bootstrapped-apps-with-yarn.md)
230+
- [Custom Type Checking Error Messages With Yup](javascript/custom-type-checking-error-messages-with-yup.md)
230231
- [Default And Named Exports From The Same Module](javascript/default-and-named-exports-from-the-same-module.md)
231232
- [Destructuring The Rest Of An Array](javascript/destructuring-the-rest-of-an-array.md)
232233
- [Enable ES7 Transforms With react-rails](javascript/enable-es7-transforms-with-react-rails.md)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Custom Type Checking Error Messages With Yup
2+
3+
In [Yup Schemas Are Validated
4+
Asynchronously](https://github.com/jbranchaud/til/blob/master/javascript/yup-schemas-are-validated-asynchronously.md),
5+
I showed how to create a simple schema that allows you to enforce that a
6+
value is a number.
7+
8+
```javascript
9+
const numSchema = yup.number();
10+
```
11+
12+
If we use this schema to validate something that isn't a number, Yup will
13+
provide a lengthy default message. Here is what we get if I validate against
14+
`'hey'`:
15+
16+
> this must be a `number` type, but the final value was: `NaN` (cast from
17+
> the value `"hey"`).
18+
19+
This value isn't necessarily suitable for displaying to a user. We can
20+
customize the type checking error message by redefining our schema with the
21+
`typeError()` function:
22+
23+
```javascript
24+
const numSchema = yup.number().typeError("Invalid number");
25+
```

0 commit comments

Comments
 (0)