@@ -232,15 +232,25 @@ describe("Type", () => {
232232 } ) ;
233233 } ) ;
234234 describe ( "chain" , ( ) => {
235- it ( "changes the output type to the function's return type" , ( ) => {
235+ it ( "changes the output type to the given function's return type" , ( ) => {
236236 const _t = v . number ( ) . chain ( ( n ) => v . ok ( String ( n ) ) ) ;
237237 expectTypeOf < v . Infer < typeof _t > > ( ) . toEqualTypeOf < string > ( ) ;
238238 } ) ;
239- it ( "infers literals when possible" , ( ) => {
239+
240+ it ( "changes the output type to given type's output type" , ( ) => {
241+ const _t = v
242+ . number ( )
243+ . map ( ( n ) => String ( n ) )
244+ . chain ( v . literal ( "1" ) ) ;
245+ expectTypeOf < v . Infer < typeof _t > > ( ) . toEqualTypeOf < "1" > ( ) ;
246+ } ) ;
247+
248+ it ( "infers literals as the given function's output type when possible" , ( ) => {
240249 const _t = v . number ( ) . chain ( ( ) => ( { ok : true , value : "test" } ) ) ;
241250 expectTypeOf < v . Infer < typeof _t > > ( ) . toEqualTypeOf < "test" > ( ) ;
242251 } ) ;
243- it ( "passes in the parsed value" , ( ) => {
252+
253+ it ( "passes in the parsed value to the given function" , ( ) => {
244254 let value : unknown ;
245255 const t = v . number ( ) . chain ( ( n ) => {
246256 value = n ;
@@ -249,7 +259,20 @@ describe("Type", () => {
249259 t . parse ( 1000 ) ;
250260 expect ( value ) . to . equal ( 1000 ) ;
251261 } ) ;
252- it ( "passes in normalized parse options" , ( ) => {
262+
263+ it ( "passes in the parsed value to the given type" , ( ) => {
264+ let value : unknown ;
265+ const t = v . number ( ) . chain (
266+ v . unknown ( ) . chain ( ( n ) => {
267+ value = n ;
268+ return v . ok ( "test" ) ;
269+ } ) ,
270+ ) ;
271+ t . parse ( 1000 ) ;
272+ expect ( value ) . to . equal ( 1000 ) ;
273+ } ) ;
274+
275+ it ( "passes in normalized parse options to the given function" , ( ) => {
253276 let options : unknown ;
254277 const t = v . number ( ) . chain ( ( n , opts ) => {
255278 options = opts ;
@@ -264,11 +287,36 @@ describe("Type", () => {
264287 t . parse ( 1 , { mode : "strict" } ) ;
265288 expect ( options ) . to . deep . equal ( { mode : "strict" } ) ;
266289 } ) ;
267- it ( "passes on the success value" , ( ) => {
290+
291+ it ( "propagates parse options to the given type" , ( ) => {
292+ let options : unknown ;
293+ const t = v . number ( ) . chain (
294+ v . unknown ( ) . chain ( ( n , opts ) => {
295+ options = opts ;
296+ return v . ok ( "test" ) ;
297+ } ) ,
298+ ) ;
299+ t . parse ( 1 , { mode : "strict" } ) ;
300+ expect ( options ) . to . deep . equal ( { mode : "strict" } ) ;
301+ t . parse ( 1 , { mode : "strip" } ) ;
302+ expect ( options ) . to . deep . equal ( { mode : "strip" } ) ;
303+ t . parse ( 1 , { mode : "passthrough" } ) ;
304+ expect ( options ) . to . deep . equal ( { mode : "passthrough" } ) ;
305+ t . parse ( 1 , { mode : "strict" } ) ;
306+ expect ( options ) . to . deep . equal ( { mode : "strict" } ) ;
307+ } ) ;
308+
309+ it ( "passes on the success value from the given function" , ( ) => {
268310 const t = v . number ( ) . chain ( ( ) => v . ok ( "test" ) ) ;
269311 expect ( t . parse ( 1 ) ) . to . equal ( "test" ) ;
270312 } ) ;
271- it ( "fails on error result" , ( ) => {
313+
314+ it ( "passes on the success value from the given type" , ( ) => {
315+ const t = v . number ( ) . chain ( v . unknown ( ) . map ( ( ) => "test" ) ) ;
316+ expect ( t . parse ( 1 ) ) . to . equal ( "test" ) ;
317+ } ) ;
318+
319+ it ( "fails on error result from the given function" , ( ) => {
272320 const t = v . number ( ) . chain ( ( ) => v . err ( ) ) ;
273321 expect ( ( ) => t . parse ( 1 ) )
274322 . to . throw ( v . ValitaError )
@@ -277,7 +325,19 @@ describe("Type", () => {
277325 code : "custom_error" ,
278326 } ) ;
279327 } ) ;
280- it ( "allows passing in a custom error message" , ( ) => {
328+
329+ it ( "fails on error result from the given type" , ( ) => {
330+ const t = v . number ( ) . chain ( v . string ( ) ) ;
331+ expect ( ( ) => t . parse ( 1 ) )
332+ . to . throw ( v . ValitaError )
333+ . with . nested . property ( "issues[0]" )
334+ . that . deep . includes ( {
335+ code : "invalid_type" ,
336+ expected : [ "string" ] ,
337+ } ) ;
338+ } ) ;
339+
340+ it ( "allows passing in a custom error message from the given function" , ( ) => {
281341 const t = v . number ( ) . chain ( ( ) => v . err ( "test" ) ) ;
282342 expect ( ( ) => t . parse ( 1 ) )
283343 . to . throw ( v . ValitaError )
@@ -287,7 +347,8 @@ describe("Type", () => {
287347 error : "test" ,
288348 } ) ;
289349 } ) ;
290- it ( "allows passing in a custom error message in an object" , ( ) => {
350+
351+ it ( "allows passing in a custom error message in an object from the given function" , ( ) => {
291352 const t = v . number ( ) . chain ( ( ) => v . err ( { message : "test" } ) ) ;
292353 expect ( ( ) => t . parse ( 1 ) )
293354 . to . throw ( v . ValitaError )
@@ -297,7 +358,8 @@ describe("Type", () => {
297358 error : { message : "test" } ,
298359 } ) ;
299360 } ) ;
300- it ( "allows passing in an error path" , ( ) => {
361+
362+ it ( "allows passing in an error path from the given function" , ( ) => {
301363 const t = v . number ( ) . chain ( ( ) => v . err ( { path : [ "test" ] } ) ) ;
302364 expect ( ( ) => t . parse ( 1 ) )
303365 . to . throw ( v . ValitaError )
@@ -307,13 +369,16 @@ describe("Type", () => {
307369 path : [ "test" ] ,
308370 } ) ;
309371 } ) ;
372+
310373 it ( "runs multiple chains in order" , ( ) => {
311374 const t = v
312375 . string ( )
313376 . chain ( ( s ) => v . ok ( s + "b" ) )
314- . chain ( ( s ) => v . ok ( s + "c" ) ) ;
315- expect ( t . parse ( "a" ) ) . to . equal ( "abc" ) ;
377+ . chain ( ( s ) => v . ok ( s + "c" ) )
378+ . chain ( v . string ( ) . map ( ( s ) => s + "d" ) ) ;
379+ expect ( t . parse ( "a" ) ) . to . equal ( "abcd" ) ;
316380 } ) ;
381+
317382 it ( "works together with .try()" , ( ) => {
318383 const s = v . string ( ) ;
319384 const t = v . unknown ( ) . chain ( ( x ) => s . try ( x ) ) ;
@@ -322,6 +387,7 @@ describe("Type", () => {
322387 expect ( ( ) => t . parse ( 1 ) ) . to . throw ( v . ValitaError ) ;
323388 } ) ;
324389 } ) ;
390+
325391 describe ( "optional" , ( ) => {
326392 it ( "returns an Optional" , ( ) => {
327393 expectTypeOf ( v . unknown ( ) . optional ( ) ) . toExtend < v . Optional > ( ) ;
0 commit comments