@@ -14,6 +14,7 @@ const boolCasesBasic: Case<
14
14
Format . Values . BoolValue ,
15
15
never
16
16
> [ ] = [
17
+ boolFromBoolean , //needed due to strictBooleans mode
17
18
boolFromString ,
18
19
boolFromBoxedPrimitive ,
19
20
boolFromCodecBoolValue ,
@@ -27,10 +28,30 @@ export const boolCases: Case<
27
28
Format . Types . BoolType ,
28
29
Format . Values . BoolValue ,
29
30
never
30
- > [ ] = [
31
- boolFromTypeValueInput ,
32
- ...boolCasesBasic
33
- ] ;
31
+ > [ ] = [ boolFromTypeValueInput , ...boolCasesBasic ] ;
32
+
33
+ function * boolFromBoolean (
34
+ dataType : Format . Types . BoolType ,
35
+ input : unknown ,
36
+ wrapOptions : WrapOptions
37
+ ) : Generator < never , Format . Values . BoolValue , WrapResponse > {
38
+ if ( typeof input !== "boolean" ) {
39
+ throw new TypeMismatchError (
40
+ dataType ,
41
+ input ,
42
+ wrapOptions . name ,
43
+ 1 ,
44
+ "Input was not a boolean"
45
+ ) ;
46
+ }
47
+ return {
48
+ type : dataType ,
49
+ kind : "value" as const ,
50
+ value : {
51
+ asBoolean : input
52
+ }
53
+ } ;
54
+ }
34
55
35
56
function * boolFromString (
36
57
dataType : Format . Types . BoolType ,
@@ -46,8 +67,21 @@ function* boolFromString(
46
67
"Input was not a string"
47
68
) ;
48
69
}
49
- //strings are true unless they're falsy or the case-insensitive string "false"
50
- const asBoolean = Boolean ( input ) && input . toLowerCase ( ) !== "false" ;
70
+ const lowerCasedInput = input . toLowerCase ( ) ;
71
+ if (
72
+ wrapOptions . strictBooleans &&
73
+ ! [ "true" , "false" , "1" , "0" ] . includes ( lowerCasedInput )
74
+ ) {
75
+ throw new TypeMismatchError (
76
+ dataType ,
77
+ input ,
78
+ wrapOptions . name ,
79
+ 5 ,
80
+ "Input was not 'true', 'false', '1', or '0'"
81
+ ) ;
82
+ }
83
+ //strings are true unless they're falsy or the case-insensitive strings "false" or "0"
84
+ const asBoolean = Boolean ( input ) && ! [ "false" , "0" ] . includes ( lowerCasedInput ) ;
51
85
return {
52
86
type : dataType ,
53
87
kind : "value" as const ,
@@ -72,7 +106,12 @@ function* boolFromBoxedPrimitive(
72
106
) ;
73
107
}
74
108
//unbox and try again
75
- return yield * wrapWithCases ( dataType , input . valueOf ( ) , wrapOptions , boolCases ) ;
109
+ return yield * wrapWithCases (
110
+ dataType ,
111
+ input . valueOf ( ) ,
112
+ wrapOptions ,
113
+ boolCases
114
+ ) ;
76
115
}
77
116
78
117
function * boolFromCodecBoolValue (
@@ -215,9 +254,7 @@ function* boolFromCodecUdvtValue(
215
254
"Input was not a wrapped result"
216
255
) ;
217
256
}
218
- if (
219
- input . type . typeClass !== "userDefinedValueType"
220
- ) {
257
+ if ( input . type . typeClass !== "userDefinedValueType" ) {
221
258
throw new TypeMismatchError (
222
259
dataType ,
223
260
input ,
@@ -252,9 +289,7 @@ function* boolFromCodecUdvtError(
252
289
"Input was not a wrapped result"
253
290
) ;
254
291
}
255
- if (
256
- input . type . typeClass !== "userDefinedValueType"
257
- ) {
292
+ if ( input . type . typeClass !== "userDefinedValueType" ) {
258
293
throw new TypeMismatchError (
259
294
dataType ,
260
295
input ,
@@ -282,7 +317,11 @@ function* boolFromCodecUdvtError(
282
317
Messages . errorResultMessage
283
318
) ;
284
319
}
285
- return yield * boolFromCodecBoolError ( dataType , input . error . error , wrapOptions ) ;
320
+ return yield * boolFromCodecBoolError (
321
+ dataType ,
322
+ input . error . error ,
323
+ wrapOptions
324
+ ) ;
286
325
}
287
326
288
327
function * boolFromOther (
@@ -315,6 +354,16 @@ function* boolFromOther(
315
354
"Input was a type/value pair"
316
355
) ;
317
356
}
357
+ //...and also we don't do this case if strictBooleans is turned on
358
+ if ( wrapOptions . strictBooleans ) {
359
+ throw new TypeMismatchError (
360
+ dataType ,
361
+ input ,
362
+ wrapOptions . name ,
363
+ 2 ,
364
+ "Input was neither a boolean nor a boolean string"
365
+ ) ;
366
+ }
318
367
const asBoolean = Boolean ( input ) ;
319
368
return {
320
369
type : dataType ,
0 commit comments