Commit 73858fe 1 parent dbf3d9f commit 73858fe Copy full SHA for 73858fe
File tree 4 files changed +35
-12
lines changed
4 files changed +35
-12
lines changed Original file line number Diff line number Diff line change 1
- import typeOf from 'type-name' ;
2
1
import has from 'lodash/has' ;
3
2
4
3
import { mixed as locale } from './locale' ;
@@ -133,15 +132,23 @@ SchemaType.prototype = {
133
132
return this
134
133
} ,
135
134
136
- cast ( value , opts = { } ) {
137
- let schema = this . resolve ( opts )
138
-
139
- let result = schema . _cast ( value , opts ) ;
140
-
141
- if ( opts . assert !== false && ! this . isType ( result ) ) {
135
+ cast ( value , options = { } ) {
136
+ let resolvedSchema = this . resolve ( options )
137
+ let result = resolvedSchema . _cast ( value , options ) ;
138
+
139
+ if (
140
+ value !== undefined &&
141
+ options . assert !== false &&
142
+ resolvedSchema . isType ( result ) !== true
143
+ ) {
144
+ let formattedValue = JSON . stringify ( value ) ;
145
+ let formattedResult = JSON . stringify ( result ) ;
142
146
throw new TypeError (
143
- `Expected ${ opts . path || 'field' } to be type: "${ this . _type } ". ` +
144
- `Got "${ typeOf ( value ) } " instead.`
147
+ `The value of ${ options . path || 'field' } could not be cast to a value ` +
148
+ `that satisfies the schema type: "${ resolvedSchema . _type } ". \n\n` +
149
+ `attempted value: ${ JSON . stringify ( value ) } \n` +
150
+ ( ( formattedResult !== formattedValue )
151
+ ? `result of cast: ${ JSON . stringify ( result ) } ` : '' )
145
152
) ;
146
153
}
147
154
Original file line number Diff line number Diff line change 1
- import typeOf from 'type-name' ;
2
1
3
2
export let castAndShouldFail = ( schema , value ) => {
4
3
( ( ) => schema . cast ( value ) )
5
4
. should . throw (
6
5
TypeError ,
7
- new RegExp ( `Got " ${ typeOf ( value ) } " instead` , 'gi' )
6
+ / T h e v a l u e o f ( . + ) c o u l d n o t b e c a s t t o a v a l u e t h a t s a t i s f i e s t h e s c h e m a t y p e / gi
8
7
)
9
8
}
10
9
Original file line number Diff line number Diff line change @@ -19,7 +19,6 @@ describe('String types', function(){
19
19
[ null , null , schema . nullable ( ) ]
20
20
] ,
21
21
invalid : [
22
- undefined ,
23
22
null ,
24
23
]
25
24
} )
Original file line number Diff line number Diff line change @@ -12,6 +12,24 @@ describe('Yup', function(){
12
12
require ( '../lib' )
13
13
} )
14
14
15
+ it ( 'cast should not assert on undefined' , ( ) => {
16
+ ( ( ) => string ( ) . cast ( undefined ) )
17
+ . should . not . throw ( )
18
+ } )
19
+
20
+ it ( 'cast should assert on undefined cast results' , ( ) => {
21
+ ( ( ) => string ( ) . transform ( ( ) => undefined ) . cast ( 'foo' ) )
22
+ . should . throw ( )
23
+ } )
24
+
25
+ it ( 'cast should respect assert option' , ( ) => {
26
+ ( ( ) => string ( ) . cast ( null ) )
27
+ . should . throw ( ) ;
28
+
29
+ ( ( ) => string ( ) . cast ( null , { assert : false } ) )
30
+ . should . not . throw ( )
31
+ } )
32
+
15
33
it ( 'should do settled' , function ( ) {
16
34
return Promise . all ( [
17
35
You can’t perform that action at this time.
0 commit comments