File tree Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Expand file tree Collapse file tree 2 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
10
10
For a steady stream of TILs from a variety of rocketeers, checkout
11
11
[ til.hashrocket.com] ( https://til.hashrocket.com/ ) .
12
12
13
- _ 635 TILs and counting..._
13
+ _ 636 TILs and counting..._
14
14
15
15
---
16
16
@@ -227,6 +227,7 @@ _635 TILs and counting..._
227
227
- [ Computed Property Names In ES6] ( javascript/computed-property-names-in-es6.md )
228
228
- [ Create An Array Containing 1 To N] ( javascript/create-an-array-containing-1-to-n.md )
229
229
- [ 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 )
230
231
- [ Default And Named Exports From The Same Module] ( javascript/default-and-named-exports-from-the-same-module.md )
231
232
- [ Destructuring The Rest Of An Array] ( javascript/destructuring-the-rest-of-an-array.md )
232
233
- [ Enable ES7 Transforms With react-rails] ( javascript/enable-es7-transforms-with-react-rails.md )
Original file line number Diff line number Diff line change
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
+ ```
You can’t perform that action at this time.
0 commit comments